mirror of https://github.com/tucnak/telebot
Introducing Sendable interface (see #93.)
This commit refactors lots of duplicated code from bot.go, so instead of having N redundant SendX methods, now it's the responsibility of all Sendable objects to implement Send(..) instead. Impl in types_send.go, 150 LOC only!pull/108/head
parent
127bf18d79
commit
ef59af6db7
@ -0,0 +1,146 @@
|
||||
package telebot
|
||||
|
||||
import "fmt"
|
||||
|
||||
func (b *Bot) sendText(to Recipient, text string, opt *SendOptions) (*Message, error) {
|
||||
params := map[string]string{
|
||||
"chat_id": to.Destination(),
|
||||
"text": text,
|
||||
}
|
||||
embedSendOptions(params, opt)
|
||||
|
||||
respJSON, err := b.sendCommand("sendMessage", params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return extractMsgResponse(respJSON)
|
||||
}
|
||||
|
||||
func (p *Photo) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
|
||||
params := map[string]string{
|
||||
"chat_id": to.Destination(),
|
||||
"caption": p.Caption,
|
||||
}
|
||||
|
||||
embedSendOptions(params, opt)
|
||||
|
||||
msg, err := b.sendObject(&p.File, "photo", params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
thumbnails := msg.Photo
|
||||
fname := p.filename
|
||||
p.File = thumbnails[len(thumbnails)-1].File
|
||||
p.filename = fname
|
||||
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
func (a *Audio) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
|
||||
params := map[string]string{
|
||||
"chat_id": to.Destination(),
|
||||
}
|
||||
embedSendOptions(params, opt)
|
||||
|
||||
msg, err := b.sendObject(&a.File, "audio", params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fname := a.filename
|
||||
*a = *msg.Audio
|
||||
a.filename = fname
|
||||
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
func (d *Document) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
|
||||
params := map[string]string{
|
||||
"chat_id": to.Destination(),
|
||||
}
|
||||
embedSendOptions(params, opt)
|
||||
|
||||
msg, err := b.sendObject(&d.File, "audio", params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fname := d.filename
|
||||
*d = *msg.Document
|
||||
d.filename = fname
|
||||
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
func (s *Sticker) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
|
||||
params := map[string]string{
|
||||
"chat_id": to.Destination(),
|
||||
}
|
||||
embedSendOptions(params, opt)
|
||||
|
||||
msg, err := b.sendObject(&s.File, "sticker", params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fname := s.filename
|
||||
*s = *msg.Sticker
|
||||
s.filename = fname
|
||||
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
func (v *Video) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
|
||||
params := map[string]string{
|
||||
"chat_id": to.Destination(),
|
||||
}
|
||||
embedSendOptions(params, opt)
|
||||
|
||||
msg, err := b.sendObject(&v.File, "video", params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fname := v.filename
|
||||
*v = *msg.Video
|
||||
v.filename = fname
|
||||
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
func (x *Location) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
|
||||
params := map[string]string{
|
||||
"chat_id": to.Destination(),
|
||||
"latitude": fmt.Sprintf("%f", x.Latitude),
|
||||
"longitude": fmt.Sprintf("%f", x.Longitude),
|
||||
}
|
||||
embedSendOptions(params, opt)
|
||||
|
||||
respJSON, err := b.sendCommand("sendLocation", params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return extractMsgResponse(respJSON)
|
||||
}
|
||||
|
||||
func (v *Venue) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
|
||||
params := map[string]string{
|
||||
"chat_id": to.Destination(),
|
||||
"latitude": fmt.Sprintf("%f", v.Location.Latitude),
|
||||
"longitude": fmt.Sprintf("%f", v.Location.Longitude),
|
||||
"title": v.Title,
|
||||
"address": v.Address,
|
||||
"foursquare_id": v.FoursquareID,
|
||||
}
|
||||
embedSendOptions(params, opt)
|
||||
|
||||
respJSON, err := b.sendCommand("sendLocation", params)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return extractMsgResponse(respJSON)
|
||||
}
|
@ -0,0 +1,85 @@
|
||||
package telebot
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func extractMsgResponse(respJSON []byte) (*Message, error) {
|
||||
var resp struct {
|
||||
Ok bool
|
||||
Result *Message
|
||||
Description string
|
||||
}
|
||||
|
||||
err := json.Unmarshal(respJSON, &resp)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "bad response json")
|
||||
}
|
||||
|
||||
if !resp.Ok {
|
||||
return nil, errors.Errorf("api error: %s", resp.Description)
|
||||
}
|
||||
|
||||
return resp.Result, nil
|
||||
}
|
||||
|
||||
func extractOptions(how []interface{}) *SendOptions {
|
||||
var options *SendOptions
|
||||
|
||||
for _, object := range how {
|
||||
switch option := object.(type) {
|
||||
case *SendOptions:
|
||||
options = option
|
||||
break
|
||||
|
||||
case *ReplyMarkup:
|
||||
if options == nil {
|
||||
options = &SendOptions{}
|
||||
}
|
||||
options.ReplyMarkup = option
|
||||
break
|
||||
|
||||
default:
|
||||
panic(fmt.Sprintf("telebot: %v is not a send-option", option))
|
||||
}
|
||||
}
|
||||
|
||||
return options
|
||||
}
|
||||
|
||||
func embedSendOptions(params map[string]string, opt *SendOptions) {
|
||||
if opt == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if opt.ReplyTo.ID != 0 {
|
||||
params["reply_to_message_id"] = strconv.Itoa(opt.ReplyTo.ID)
|
||||
}
|
||||
|
||||
if opt.DisableWebPagePreview {
|
||||
params["disable_web_page_preview"] = "true"
|
||||
}
|
||||
|
||||
if opt.DisableNotification {
|
||||
params["disable_notification"] = "true"
|
||||
}
|
||||
|
||||
if opt.ParseMode != ModeDefault {
|
||||
params["parse_mode"] = string(opt.ParseMode)
|
||||
}
|
||||
|
||||
if opt.ReplyMarkup != nil {
|
||||
forceReply := opt.ReplyMarkup.ForceReply
|
||||
customKeyboard := (opt.ReplyMarkup.CustomKeyboard != nil)
|
||||
inlineKeyboard := opt.ReplyMarkup.InlineKeyboard != nil
|
||||
hiddenKeyboard := opt.ReplyMarkup.HideCustomKeyboard
|
||||
if forceReply || customKeyboard || hiddenKeyboard || inlineKeyboard {
|
||||
replyMarkup, _ := json.Marshal(opt.ReplyMarkup)
|
||||
params["reply_markup"] = string(replyMarkup)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue