go-telegram-bot-api / telegram-bot-api

Golang bindings for the Telegram Bot API
https://go-telegram-bot-api.dev
MIT License
5.62k stars 868 forks source link

TbAPI.Send(tbapi.RestrictChatMemberConfig{}) results in error on successful call #591

Closed paskal closed 1 year ago

paskal commented 1 year ago

Steps to reproduce:

  1. git clone git@github.com:paskal/super-bot.git -b paskal/telegram_v5
  2. in docker-compose.yml fill TELEGRAM_GROUP with the ID of the supergroup (500+ members), TELEGRAM_TOKEN with token of the bot which has admin rights in that group
  3. docker-compose up --build
  4. from any user, write wtf? in that chat

Expected result: the user is banned temporarily by the bot, with no errors from calling the restrictChatMember API. It works like that in v4, code:

func (l *TelegramListener) banUser(duration time.Duration, chatID int64, userID int) error {
    resp, err := l.TbAPI.RestrictChatMember(tbapi.RestrictChatMemberConfig{
        ChatMemberConfig: tbapi.ChatMemberConfig{
            ChatID: chatID,
            UserID: userID,
        },
        UntilDate:             time.Now().Add(duration).Unix(),
        CanSendMessages:       new(bool),
        CanSendMediaMessages:  new(bool),
        CanSendOtherMessages:  new(bool),
        CanAddWebPagePreviews: new(bool),
    })
    if err != nil {
        return err
    }

    if !resp.Ok {
        return fmt.Errorf("response is not Ok: %v", string(resp.Result))
    }

    return nil
}

Observed result: the user is banned by the bot, but there is an error json: cannot unmarshal bool into Go value of type tgbotapi.Message. That started after migration from v4 to v5, code:

func (l *TelegramListener) banUser(duration time.Duration, chatID int64, userID int64) error {
    _, err := l.TbAPI.Send(tbapi.RestrictChatMemberConfig{
        ChatMemberConfig: tbapi.ChatMemberConfig{
            ChatID: chatID,
            UserID: userID,
        },
        UntilDate: time.Now().Add(duration).Unix(),
        Permissions: &tbapi.ChatPermissions{
            CanSendMessages:       false,
            CanSendMediaMessages:  false,
            CanSendOtherMessages:  false,
            CanAddWebPagePreviews: false,
        },
    })
    if err != nil {
        return err
    }

    return nil
}

Logs:

2022/10/02 14:34:29 Endpoint: restrictChatMember, params: map[chat_id:-1001358715913 permissions:{} until_date:1664743896 user_id:107408621]
2022/10/02 14:34:29.433 [DEBUG] {events/telegram.go:129 events.(*TelegramListener).Do} ban initiated for {Text:[@Paskal](tg://user?id=107408621) получает бан на 1ч 17мин 7сек Send:true Pin:false Unpin:false Preview:false BanInterval:1h17m7s User:{ID:107408621 Username:Paskal DisplayName:User Name}}
2022/10/02 14:34:29 Endpoint: restrictChatMember, response: {"ok":true,"result":true}
2022/10/02 14:34:29.595 [ERROR] {events/telegram.go:131 events.(*TelegramListener).Do} can't ban {ID:107408621 Username:Paskal DisplayName:User Name} on bot response, json: cannot unmarshal bool into Go value of type tgbotapi.Message
ltunc commented 1 year ago

The restrictChatMember endpoint don't return a Message object. Maybe using BotAPI.Request() method will suit better for this case? It also receives Chattable as argument but returns ApiResponse

paskal commented 1 year ago

It solves the issue, thanks a lot! I propose to update the documentation of RestrictChatMemberConfig to reflect that.