Skip to content

Commit

Permalink
Implement sendPoll method
Browse files Browse the repository at this point in the history
  • Loading branch information
setval committed Mar 30, 2020
1 parent c239db7 commit 69f0836
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 2 deletions.
88 changes: 88 additions & 0 deletions bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ type Update struct {
Query *Query `json:"inline_query,omitempty"`
ChosenInlineResult *ChosenInlineResult `json:"chosen_inline_result,omitempty"`
PreCheckoutQuery *PreCheckoutQuery `json:"pre_checkout_query,omitempty"`
Poll *Poll `json:"poll,omitempty"`
PollAnswer *PollAnswer `json:"poll_answer,omitempty"`
}

// ChosenInlineResult represents a result of an inline query that was chosen
Expand Down Expand Up @@ -416,6 +418,46 @@ func (b *Bot) incomingUpdate(upd *Update) {
}
return
}

if upd.Poll != nil {
if handler, ok := b.handlers[OnPoll]; ok {
if handler, ok := handler.(func(*Poll)); ok {
// i'm not 100% sure that any of the values
// won't be cached, so I pass them all in:
go func(b *Bot, handler func(*Poll),
r *Poll) {
if b.reporter == nil {
defer b.deferDebug()
}
handler(r)
}(b, handler, upd.Poll)

} else {
panic("telebot: poll handler is bad")
}
}
return
}

if upd.PollAnswer != nil {
if handler, ok := b.handlers[OnPollAnswer]; ok {
if handler, ok := handler.(func(*PollAnswer)); ok {
// i'm not 100% sure that any of the values
// won't be cached, so I pass them all in:
go func(b *Bot, handler func(*PollAnswer),
r *PollAnswer) {
if b.reporter == nil {
defer b.deferDebug()
}
handler(r)
}(b, handler, upd.PollAnswer)

} else {
panic("telebot: poll answer handler is bad")
}
}
return
}
}

func (b *Bot) handle(end string, m *Message) bool {
Expand Down Expand Up @@ -1548,3 +1590,49 @@ func (b *Bot) DeleteStickerFromSet(sticker string) error {

return extractOkResponse(respJSON)
}

// DeleteStickerFromSet deletes sticker from set created by the bot.
func (b *Bot) SendPoll(to Recipient, poll *Poll, options *PollOptions) (*Message, error) {
params := map[string]string{
"chat_id": to.Recipient(),
"question": poll.Question,
"type": poll.Type,
"allows_multiple_answers": strconv.FormatBool(poll.AllowsMultipleAnswers),
"correct_option_id": strconv.Itoa(poll.CorrectOptionID),
"is_anonymous": strconv.FormatBool(poll.IsAnonymous),
"is_closed": strconv.FormatBool(poll.IsClosed),
}

if poll.Type == "" {
params["type"] = "regular"
}

if poll.Options != nil {
var options []string
for _, opt := range poll.Options {
options = append(options, opt.Text)
}
opts, _ := json.Marshal(options)
params["options"] = string(opts)
}

if options != nil {
if options.DisableNotification {
params["disable_notification"] = "true"
}
if options.ReplyToMessageID != nil {
params["reply_to_message_id"] = strconv.Itoa(*options.ReplyToMessageID)
}
if options.ReplyMarkup != nil {
markup, _ := json.Marshal(options.ReplyMarkup)
params["reply_markup"] = string(markup)
}
}

respJSON, err := b.Raw("sendPoll", params)
if err != nil {
return nil, err
}

return extractMsgResponse(respJSON)
}
7 changes: 7 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,10 @@ type InlineKeyboardMarkup struct {
// an Array of KeyboardButton objects.
InlineKeyboard [][]InlineButton `json:"inline_keyboard,omitempty"`
}

// PollOptions represents arguments for sendPoll method.
type PollOptions struct {
DisableNotification bool `json:"disable_notification"`
ReplyToMessageID *int `json:"reply_to_message_id"`
ReplyMarkup *ReplyMarkup `json:"reply_markup"`
}
10 changes: 8 additions & 2 deletions poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ type Poll struct {

// PollOption object represents a option of a poll
type PollOption struct {
Text string
VoterCount int
Text string `json:"text"`
VoterCount int `json:"voter_count"`
}

// PollAnswer object represents an answer of a user in a non-anonymous poll.
Expand All @@ -39,3 +39,9 @@ func (p *Poll) IsRegular() bool {
func (p *Poll) IsQuiz() bool {
return p.Type == "quiz"
}

func (p *Poll) AddAnswers(text ...string) {
for _, t := range text {
p.Options = append(p.Options, PollOption{Text: t})
}
}
10 changes: 10 additions & 0 deletions telebot.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ const (
//
// Handler: func(*PreCheckoutQuery)
OnCheckout = "\apre_checkout_query"

// Will fire on Poll.
//
// Handler: func(*Poll)
OnPoll = "\apoll"

// Will fire on PollAnswer.
//
// Handler: func(*PollAnswer)
OnPollAnswer = "\apoll_answer"
)

// ChatAction is a client-side status indicating bot activity.
Expand Down

0 comments on commit 69f0836

Please sign in to comment.