From fb0e91451ec1c6c510d3efcbcaf0274ea7e0c137 Mon Sep 17 00:00:00 2001 From: Ilya Kowalewski Date: Mon, 6 Jul 2015 16:46:19 +0300 Subject: [PATCH] Significant API change, send options added --- README.md | 17 +++++++++++++++-- api.go | 19 +++++++++++++++++++ bot.go | 43 ++++++++++++++++++++++++++++++++++++------- options.go | 31 +++++++++++++++++++++++++++++++ telebot.go | 2 +- 5 files changed, 102 insertions(+), 10 deletions(-) create mode 100644 options.go diff --git a/README.md b/README.md index c4865f7..74f14e8 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ func main() { for message := range messages { if message.Text == "/hi" { bot.SendMessage(message.Chat, - "Hello, "+message.Sender.FirstName+"!") + "Hello, "+message.Sender.FirstName+"!", nil) } } } @@ -41,5 +41,18 @@ if err != nil { // Next time you send &boom, telebot won't issue // an upload, but would re-use existing file. -err = bot.SendAudio(recipient, &boom) +err = bot.SendAudio(recipient, &boom, nil) +``` + +Sometimes you might want to send a little bit complicated messages, with some optional parameters: + +```go +// Send a selective force reply message. +bot.SendMessage(user, "pong", &telebot.SendOptions{ + ForceReply: telebot.ForceReply{ + Require: true, + Selective: true, + }, + }, +) ``` diff --git a/api.go b/api.go index 3509c67..af08047 100644 --- a/api.go +++ b/api.go @@ -82,6 +82,25 @@ func sendFile(method, token, name, path string, params url.Values) ([]byte, erro return json, nil } +func embedSendOptions(params *url.Values, options *SendOptions) { + if params == nil || options == nil { + return + } + + if options.ReplyTo.Id != 0 { + params.Set("reply_to_message_id", strconv.Itoa(options.ReplyTo.Id)) + } + + if options.DisableWebPagePreview { + params.Set("disable_web_page_preview", "true") + } + + if options.ForceReply.Require { + forceReply, _ := json.Marshal(options.ForceReply) + params.Set("reply_markup", string(forceReply)) + } +} + func getMe(token string) (User, error) { me_json, err := sendCommand("getMe", token, url.Values{}) if err != nil { diff --git a/bot.go b/bot.go index e27b48b..43fdfc9 100644 --- a/bot.go +++ b/bot.go @@ -57,10 +57,15 @@ func (b Bot) Listen(subscription chan<- Message, interval time.Duration) { } // SendMessage sends a text message to recipient. -func (b Bot) SendMessage(recipient User, message string) error { +func (b Bot) SendMessage(recipient User, message string, options *SendOptions) error { params := url.Values{} params.Set("chat_id", strconv.Itoa(recipient.Id)) params.Set("text", message) + + if options != nil { + embedSendOptions(¶ms, options) + } + response_json, err := sendCommand("sendMessage", b.Token, params) if err != nil { return err @@ -118,11 +123,15 @@ func (b Bot) ForwardMessage(recipient User, message Message) error { // the Telegram servers, so sending the same photo object // again, won't issue a new upload, but would make a use // of existing file on Telegram servers. -func (b Bot) SendPhoto(recipient User, photo *Photo) error { +func (b Bot) SendPhoto(recipient User, photo *Photo, options *SendOptions) error { params := url.Values{} params.Set("chat_id", strconv.Itoa(recipient.Id)) params.Set("caption", photo.Caption) + if options != nil { + embedSendOptions(¶ms, options) + } + var response_json []byte var err error @@ -165,10 +174,14 @@ func (b Bot) SendPhoto(recipient User, photo *Photo) error { // the Telegram servers, so sending the same audio object // again, won't issue a new upload, but would make a use // of existing file on Telegram servers. -func (b Bot) SendAudio(recipient User, audio *Audio) error { +func (b Bot) SendAudio(recipient User, audio *Audio, options *SendOptions) error { params := url.Values{} params.Set("chat_id", strconv.Itoa(recipient.Id)) + if options != nil { + embedSendOptions(¶ms, options) + } + var response_json []byte var err error @@ -210,10 +223,14 @@ func (b Bot) SendAudio(recipient User, audio *Audio) error { // the Telegram servers, so sending the same document object // again, won't issue a new upload, but would make a use // of existing file on Telegram servers. -func (b Bot) SendDocument(recipient User, doc *Document) error { +func (b Bot) SendDocument(recipient User, doc *Document, options *SendOptions) error { params := url.Values{} params.Set("chat_id", strconv.Itoa(recipient.Id)) + if options != nil { + embedSendOptions(¶ms, options) + } + var response_json []byte var err error @@ -255,10 +272,14 @@ func (b Bot) SendDocument(recipient User, doc *Document) error { // the Telegram servers, so sending the same sticker object // again, won't issue a new upload, but would make a use // of existing file on Telegram servers. -func (b *Bot) SendSticker(recipient User, sticker *Sticker) error { +func (b *Bot) SendSticker(recipient User, sticker *Sticker, options *SendOptions) error { params := url.Values{} params.Set("chat_id", strconv.Itoa(recipient.Id)) + if options != nil { + embedSendOptions(¶ms, options) + } + var response_json []byte var err error @@ -300,10 +321,14 @@ func (b *Bot) SendSticker(recipient User, sticker *Sticker) error { // the Telegram servers, so sending the same video object // again, won't issue a new upload, but would make a use // of existing file on Telegram servers. -func (b Bot) SendVideo(recipient User, video *Video) error { +func (b Bot) SendVideo(recipient User, video *Video, options *SendOptions) error { params := url.Values{} params.Set("chat_id", strconv.Itoa(recipient.Id)) + if options != nil { + embedSendOptions(¶ms, options) + } + var response_json []byte var err error @@ -345,12 +370,16 @@ func (b Bot) SendVideo(recipient User, video *Video) error { // the Telegram servers, so sending the same video object // again, won't issue a new upload, but would make a use // of existing file on Telegram servers. -func (b Bot) SendLocation(recipient User, geo *Location) error { +func (b Bot) SendLocation(recipient User, geo *Location, options *SendOptions) error { params := url.Values{} params.Set("chat_id", strconv.Itoa(recipient.Id)) params.Set("latitude", fmt.Sprintf("%f", geo.Latitude)) params.Set("longitude", fmt.Sprintf("%f", geo.Longitude)) + if options != nil { + embedSendOptions(¶ms, options) + } + response_json, err := sendCommand("sendLocation", b.Token, params) if err != nil { diff --git a/options.go b/options.go new file mode 100644 index 0000000..0b43f6e --- /dev/null +++ b/options.go @@ -0,0 +1,31 @@ +package telebot + +// SendOptions represents a set of custom options that could +// be appled to messages sent. +type SendOptions struct { + // If the message is a reply, original message. + ReplyTo Message + + // See ForceReply struct definition. + ForceReply ForceReply + + // For text messages, disables previews for links in this message. + DisableWebPagePreview bool +} + +// ForceReply forces Telegram clients to display +// a reply interface to the user (act as if the user +// has selected the bot‘s message and tapped "Reply"). +type ForceReply struct { + // Enable if intended. + Require bool `json:"force_reply"` + + // Use this param if you want to force reply from + // specific users only. + // + // Targets: + // 1) Users that are @mentioned in the text of the Message object; + // 2) If the bot's message is a reply (has SendOptions.ReplyTo), + // sender of the original message. + Selective bool `json:"selective"` +} diff --git a/telebot.go b/telebot.go index 910426c..64b9e1c 100644 --- a/telebot.go +++ b/telebot.go @@ -20,7 +20,7 @@ // for message := range messages { // if message.Text == "/hi" { // bot.SendMessage(message.Chat, -// "Hello, "+message.Sender.FirstName+"!") +// "Hello, "+message.Sender.FirstName+"!", nil) // } // } // }