tucnak / telebot

Telebot is a Telegram bot framework in Go.
MIT License
3.77k stars 440 forks source link

ChatByUsername telegram: chat not found (400) #701

Closed poikl246 closed 6 days ago

poikl246 commented 1 week ago

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 commented 1 week ago

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 commented 1 week ago

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 commented 1 week ago

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 commented 6 days ago

@im-trisha

Okay, thank you very much.

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