diff --git a/README.md b/README.md index 4f3880a..9d852ca 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,7 @@ go get -u gopkg.in/tucnak/telebot.v2 - [Editable](#editable) - [Keyboards](#keyboards) - [Inline mode](#inline-mode) + - [Poll](#poll) * [Contributing](#contributing) * [Donate](#donate) * [License](#license) @@ -427,6 +428,23 @@ There's not much to talk about really. It also supports some form of authenticat through deep-linking. For that, use fields `SwitchPMText` and `SwitchPMParameter` of `QueryResponse`. + +## Poll +Creating polls is just as easy. You just need to create an object and use the `Send` method. +You can also use the `AddAnswers` method instead of explicitly specifying +a slice of answers to a question and without `append`. +``` +poll := &tb.Poll{ + Question: "my question", + Type: "regular", + IsClosed: false, + IsAnonymous: true, +} +poll.AddAnswers("answer 1", "answer 2") + +b.Send(m.Sender, poll) +``` + # Contributing 1. Fork it diff --git a/bot.go b/bot.go index 87377dc..fbcd05a 100644 --- a/bot.go +++ b/bot.go @@ -1590,49 +1590,3 @@ func (b *Bot) DeleteStickerFromSet(sticker string) error { return extractOkResponse(respJSON) } - -// SendPoll sends poll to chat -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) -} diff --git a/sendable.go b/sendable.go index 4a58cfe..aca8e84 100644 --- a/sendable.go +++ b/sendable.go @@ -316,3 +316,30 @@ func (i *Invoice) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) return extractMsgResponse(respJSON) } + +func (p *Poll) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) { + params := map[string]string{ + "chat_id": to.Recipient(), + "question": p.Question, + "type": p.Type, + "allows_multiple_answers": strconv.FormatBool(p.AllowsMultipleAnswers), + "correct_option_id": strconv.Itoa(p.CorrectOptionID), + "is_anonymous": strconv.FormatBool(p.IsAnonymous), + "is_closed": strconv.FormatBool(p.IsClosed), + } + embedSendOptions(params, opt) + + var options []string + for _, o := range p.Options { + options = append(options, o.Text) + } + opts, _ := json.Marshal(options) + params["options"] = string(opts) + + respJSON, err := b.Raw("sendPoll", params) + if err != nil { + return nil, err + } + + return extractMsgResponse(respJSON) +}