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

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

telegram assumes poll, if request_poll is not omitted #327

Closed sdrissen closed 4 years ago

sdrissen commented 4 years ago

I work with the develop-branch and try to make KeyboardButtons with text only, but it always prompt a poll.

I tried the following in types.go: (explanation below)

type KeyboardButton struct {
    Text            string                 `json:"text"`
    RequestContact  bool                   `json:"request_contact"`
    RequestLocation bool                   `json:"request_location"`
    RequestPoll     KeyboardButtonPollType `json:"request_poll,omitempty"`
}

// KeyboardButtonPollType represents type of a poll, which is allowed to
// be created and sent when the corresponding button is pressed.
type KeyboardButtonPollType struct {
    Type string `json:"type,omitempty"`
}

As you see, I added omitempty to RequestPoll in KeyboardButton and to Type in KeyboardButtonPollType.

But it has no use:

Endpoint: sendMessage, params: map[reply_markup:
{
    "keyboard": [
        [
            {
                "text": "DUMMYtitle",
                "request_contact": false,
                "request_location": false,
                "request_poll": {}
            }
        ]
    ],
    "resize_keyboard": true,
    "one_time_keyboard": false,
    "selective": false
}

The telegram bot API doc says about Type in KeyboardButtonPollType:

type | String | Optional. If quiz is passed, the user will be allowed to create only polls in the quiz mode. If regular is passed, only regular polls will be allowed. Otherwise, the user will be allowed to create a poll of any type.

I think the "Otherwise, the user will be allowed to create a poll of any type" is matched here, because request_poll is not omitted, and therefore the Type-String is neither quiz or regular.

By the way, why don't you omit every empty and optional parameters?

sdrissen commented 4 years ago

Nevermind, I solved it

KeyboardButtonPollType must be a pointer, to make omitempty working. I am not sure, if this make something worse. But for the moment, it works for me.

Please change the file types.go, if you think, that my solution would work for everyone. And if not, let me know, how you would solve it.

// KeyboardButton is a button within a custom keyboard.
type KeyboardButton struct {
    Text            string                  `json:"text"`
    RequestContact  bool                    `json:"request_contact"`
    RequestLocation bool                    `json:"request_location"`
    RequestPoll     *KeyboardButtonPollType `json:"request_poll,omitempty"`
}

// KeyboardButtonPollType represents type of a poll, which is allowed to
// be created and sent when the corresponding button is pressed.
type KeyboardButtonPollType struct {
    Type string `json:"type,omitempty"`
}
Syfaro commented 4 years ago

Yes, that's how I would fix it. Thank you for opening this issue, it's now fixed in https://github.com/go-telegram-bot-api/telegram-bot-api/commit/774f1e72e7647a5e78d4d3345f54581249bbf110.