Significant API change, send options added

pull/3/head
Ilya Kowalewski 9 years ago
parent 8c49aeb84c
commit fb0e91451e

@ -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,
},
},
)
```

@ -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 {

@ -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(&params, 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(&params, 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(&params, 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(&params, 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(&params, 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(&params, 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(&params, options)
}
response_json, err := sendCommand("sendLocation", b.Token, params)
if err != nil {

@ -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 bots 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"`
}

@ -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)
// }
// }
// }

Loading…
Cancel
Save