feat(configs, types): add poll support

pull/230/head
jmen 6 years ago
parent ec221ba9ea
commit 74810f9641
  1. 13
      bot.go
  2. 73
      configs.go
  3. 16
      types.go

@ -25,8 +25,8 @@ type BotAPI struct {
Debug bool `json:"debug"` Debug bool `json:"debug"`
Buffer int `json:"buffer"` Buffer int `json:"buffer"`
Self User `json:"-"` Self User `json:"-"`
Client *http.Client `json:"-"` Client *http.Client `json:"-"`
shutdownChannel chan interface{} shutdownChannel chan interface{}
} }
@ -43,9 +43,9 @@ func NewBotAPI(token string) (*BotAPI, error) {
// It requires a token, provided by @BotFather on Telegram. // It requires a token, provided by @BotFather on Telegram.
func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) { func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) {
bot := &BotAPI{ bot := &BotAPI{
Token: token, Token: token,
Client: client, Client: client,
Buffer: 100, Buffer: 100,
shutdownChannel: make(chan interface{}), shutdownChannel: make(chan interface{}),
} }
@ -490,7 +490,7 @@ func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (UpdatesChannel, error) {
return return
default: default:
} }
updates, err := bot.GetUpdates(config) updates, err := bot.GetUpdates(config)
if err != nil { if err != nil {
log.Println(err) log.Println(err)
@ -927,7 +927,6 @@ func (bot *BotAPI) SetChatTitle(config SetChatTitleConfig) (APIResponse, error)
} }
bot.debugLog(config.method(), v, nil) bot.debugLog(config.method(), v, nil)
return bot.MakeRequest(config.method(), v) return bot.MakeRequest(config.method(), v)
} }

@ -1262,3 +1262,76 @@ func (config DeleteChatPhotoConfig) values() (url.Values, error) {
return v, nil 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
}

@ -37,6 +37,7 @@ type Update struct {
CallbackQuery *CallbackQuery `json:"callback_query"` CallbackQuery *CallbackQuery `json:"callback_query"`
ShippingQuery *ShippingQuery `json:"shipping_query"` ShippingQuery *ShippingQuery `json:"shipping_query"`
PreCheckoutQuery *PreCheckoutQuery `json:"pre_checkout_query"` PreCheckoutQuery *PreCheckoutQuery `json:"pre_checkout_query"`
Poll *Poll `json:"poll"`
} }
// UpdatesChannel is the channel for getting updates. // UpdatesChannel is the channel for getting updates.
@ -132,9 +133,9 @@ func (c Chat) ChatConfig() ChatConfig {
type Message struct { type Message struct {
MessageID int `json:"message_id"` MessageID int `json:"message_id"`
From *User `json:"from"` // optional From *User `json:"from"` // optional
Date int `json:"date"` Date int `json:"chat"`
Chat *Chat `json:"chat"` ForwardFrom *User `json:"date"`
ForwardFrom *User `json:"forward_from"` // optional Chat *Chat `json:"forward_from"` // optional
ForwardFromChat *Chat `json:"forward_from_chat"` // optional ForwardFromChat *Chat `json:"forward_from_chat"` // optional
ForwardFromMessageID int `json:"forward_from_message_id"` // optional ForwardFromMessageID int `json:"forward_from_message_id"` // optional
ForwardDate int `json:"forward_date"` // optional ForwardDate int `json:"forward_date"` // optional
@ -169,6 +170,7 @@ type Message struct {
Invoice *Invoice `json:"invoice"` // optional Invoice *Invoice `json:"invoice"` // optional
SuccessfulPayment *SuccessfulPayment `json:"successful_payment"` // optional SuccessfulPayment *SuccessfulPayment `json:"successful_payment"` // optional
PassportData *PassportData `json:"passport_data,omitempty"` // optional PassportData *PassportData `json:"passport_data,omitempty"` // optional
Poll *Poll `json:"poll"` // optional
} }
// Time converts the message timestamp into a Time. // Time converts the message timestamp into a Time.
@ -904,3 +906,11 @@ type Error struct {
func (e Error) Error() string { func (e Error) Error() string {
return e.Message 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"`
}