Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChatByUsername telegram: chat not found (400) #701

Closed
poikl246 opened this issue Jun 22, 2024 · 4 comments
Closed

ChatByUsername telegram: chat not found (400) #701

poikl246 opened this issue Jun 22, 2024 · 4 comments

Comments

@poikl246
Copy link

poikl246 commented Jun 22, 2024

Hello, please advise.

Task:
Need to get the username of a user from admin and then ban him.

How I see this solution
Take the Ban command and pass the username to it via ChatMember

func (*Bot) Ban 
func (b *Bot) Ban(chat *Chat, member *ChatMember, revokeMessages ...bool) error
Ban will ban user from chat until `member.RestrictedUntil`.

So it goes something like this

	b.Handle("/s", func(c tele.Context) error {
		user := &tele.User{Username: "@y123"}

		err := b.Ban(c.Chat(), &tele.ChatMember{User: user})
		log.Println(err)
		return c.Send("Hello world!")

	})

but that's how we get the error
telegram: Bad Request: invalid user_id specified (400)

I also tried using BanSenderChat

type Test struct {
	User string
}

func (t Test) Recipient() string {
	return t.User
}
	b.Handle("/s", func(c tele.Context) error {
		user := Test{User: "@Nikqwerty123"}

		err := b.BanSenderChat(c.Chat(), user)
		log.Println(err)
		return c.Send("Hello world!")

	})

but I'm getting the error again
telegram: Bad Request: sender_chat_id is not a valid Integer (400)

after which I just switched to using ChatByUsername

There was a lot of hope for this method, as it seems to give data including user data. But it doesn't work at all.

	b.Handle("/s", func(c tele.Context) error {
		cha, err := b.ChatByUsername("@Nikq")
		log.Println(cha, err)
		return c.Send("Hello world!")

	})

on any attempts to pass different data to the method I always get this error

<nil> telegram: chat not found (400)


Can you please tell me what I'm doing wrong?
Thank you in advance, I really hope for your answer)

Don't throw slippers at me, I'm a beginner.

@poikl246
Copy link
Author

I also tried using Entities()
But it comes up with an empty User structure

for _, element := range c.Entities() {
	log.Println(element.Type)
	if element.Type == "mention" {
		log.Println(element.User)
	}
}

@im-trisha
Copy link

Hello @poikl246, I'm afraid you can't do this, as telegram makes bots unable to get an user's id (Or any information whatsoever) by its username.

What you are searching is an userbot, you can create a local api that does the job for you, I suggest the "plug-n-play" solution provided here:

https://github.com/Poolitzer/usernameToChatAPI

You will just need to create a tiny wrapper for the api.

Hope this answers your question!

@im-trisha
Copy link

im-trisha commented Jun 28, 2024

Another solution is to have a database that contains every user's username and their id.

This would however lead to unexpected problems if you don't update the database in the right time. For example:

Neither of them sends an update to the bot after changing their usernames. Now, if you try to /ban @usera, even if you want to ban @userb you will instead ban the original owner of @usera (now @UserABC)

@poikl246
Copy link
Author

@im-trisha

Okay, thank you very much.

It's very sad that the standard doesn't have this functionality(
Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants