From 74810f9641e86a881e1fc93f3ebad45a17b84f9a Mon Sep 17 00:00:00 2001 From: jmen Date: Fri, 19 Apr 2019 18:39:19 +0300 Subject: [PATCH] feat(configs, types): add poll support --- bot.go | 13 +++++----- configs.go | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ types.go | 16 +++++++++--- 3 files changed, 92 insertions(+), 10 deletions(-) diff --git a/bot.go b/bot.go index 85e16c1..7260a28 100644 --- a/bot.go +++ b/bot.go @@ -25,8 +25,8 @@ type BotAPI struct { Debug bool `json:"debug"` Buffer int `json:"buffer"` - Self User `json:"-"` - Client *http.Client `json:"-"` + Self User `json:"-"` + Client *http.Client `json:"-"` shutdownChannel chan interface{} } @@ -43,9 +43,9 @@ func NewBotAPI(token string) (*BotAPI, error) { // It requires a token, provided by @BotFather on Telegram. func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) { bot := &BotAPI{ - Token: token, - Client: client, - Buffer: 100, + Token: token, + Client: client, + Buffer: 100, shutdownChannel: make(chan interface{}), } @@ -490,7 +490,7 @@ func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (UpdatesChannel, error) { return default: } - + updates, err := bot.GetUpdates(config) if err != nil { log.Println(err) @@ -927,7 +927,6 @@ func (bot *BotAPI) SetChatTitle(config SetChatTitleConfig) (APIResponse, error) } bot.debugLog(config.method(), v, nil) - return bot.MakeRequest(config.method(), v) } diff --git a/configs.go b/configs.go index 181d4e4..0562d99 100644 --- a/configs.go +++ b/configs.go @@ -1262,3 +1262,76 @@ func (config DeleteChatPhotoConfig) values() (url.Values, error) { return v, nil } + +// SendPollConfig contains information about the poll that will be sent. +type SendPollConfig struct { + ChatID int64 + Question string + Options []string + DisableNotification bool + ReplyToMessageID int64 + ReplyMarkup interface{} +} + +func (config SendPollConfig) method() string { + return "sendPoll" +} + +func (config SendPollConfig) values() (url.Values, error) { + v := url.Values{} + + options, err := json.Marshal(config.Options) + if err != nil { + return v, err + } + + v.Add("chat_id", strconv.FormatInt(config.ChatID, 10)) + v.Add("question", config.Question) + v.Add("options", string(options)) + + if config.DisableNotification != false { + v.Add("disable_notification", strconv.FormatBool(config.DisableNotification)) + } + if config.ReplyToMessageID != 0 { + v.Add("reply_to_message_id", strconv.FormatInt(config.ReplyToMessageID, 10)) + } + if config.ReplyMarkup != nil { + data, err := json.Marshal(config.ReplyMarkup) + if err != nil { + return v, err + } + + v.Add("reply_markup", string(data)) + } + + return v, nil +} + +//StopPollConfig contains information about the poll that will be stopped. +type StopPollConfig struct { + ChatID int64 + MessageID int64 + ReplyMarkup interface{} +} + +func (config StopPollConfig) method() string { + return "stopPoll" +} + +func (config StopPollConfig) values() (url.Values, error) { + v := url.Values{} + + v.Add("chat_id", strconv.FormatInt(config.ChatID, 10)) + v.Add("message_id", strconv.FormatInt(config.MessageID, 10)) + + if config.ReplyMarkup != nil { + data, err := json.Marshal(config.ReplyMarkup) + if err != nil { + return v, err + } + + v.Add("reply_markup", string(data)) + } + + return v, nil +} diff --git a/types.go b/types.go index c6cd642..1aa578b 100644 --- a/types.go +++ b/types.go @@ -37,6 +37,7 @@ type Update struct { CallbackQuery *CallbackQuery `json:"callback_query"` ShippingQuery *ShippingQuery `json:"shipping_query"` PreCheckoutQuery *PreCheckoutQuery `json:"pre_checkout_query"` + Poll *Poll `json:"poll"` } // UpdatesChannel is the channel for getting updates. @@ -132,9 +133,9 @@ func (c Chat) ChatConfig() ChatConfig { type Message struct { MessageID int `json:"message_id"` From *User `json:"from"` // optional - Date int `json:"date"` - Chat *Chat `json:"chat"` - ForwardFrom *User `json:"forward_from"` // optional + Date int `json:"chat"` + ForwardFrom *User `json:"date"` + Chat *Chat `json:"forward_from"` // optional ForwardFromChat *Chat `json:"forward_from_chat"` // optional ForwardFromMessageID int `json:"forward_from_message_id"` // optional ForwardDate int `json:"forward_date"` // optional @@ -169,6 +170,7 @@ type Message struct { Invoice *Invoice `json:"invoice"` // optional SuccessfulPayment *SuccessfulPayment `json:"successful_payment"` // optional PassportData *PassportData `json:"passport_data,omitempty"` // optional + Poll *Poll `json:"poll"` // optional } // Time converts the message timestamp into a Time. @@ -904,3 +906,11 @@ type Error struct { func (e Error) Error() string { return e.Message } + +// Poll contains information about a poll. +type Poll struct { + ID string `json:"id"` + Question string `json:"question"` + Options []string `json:"options"` + IsClosed bool `json:"is_closed"` +}