2017-11-17 06:20:36 +00:00
|
|
|
package telebot
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
2017-11-18 13:06:20 +00:00
|
|
|
// Recipient is any possible endpoint you can send
|
|
|
|
// messages to: either user, group or a channel.
|
|
|
|
type Recipient interface {
|
|
|
|
// Must return legit Telegram chat_id or username
|
|
|
|
Recipient() string
|
|
|
|
}
|
2017-11-17 06:20:36 +00:00
|
|
|
|
2017-11-18 13:06:20 +00:00
|
|
|
// Sendable is any object that can send itself.
|
|
|
|
//
|
|
|
|
// This is pretty cool, since it lets bots implement
|
|
|
|
// custom Sendables for complex kind of media or
|
|
|
|
// chat objects spanning across multiple messages.
|
|
|
|
type Sendable interface {
|
|
|
|
Send(*Bot, Recipient, *SendOptions) (*Message, error)
|
2017-11-17 06:20:36 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 15:16:39 +00:00
|
|
|
// Send delivers media through bot b to recipient.
|
2017-11-17 06:20:36 +00:00
|
|
|
func (p *Photo) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
|
|
|
|
params := map[string]string{
|
2017-11-18 14:41:23 +00:00
|
|
|
"chat_id": to.Recipient(),
|
2017-11-17 06:20:36 +00:00
|
|
|
"caption": p.Caption,
|
|
|
|
}
|
|
|
|
|
2017-11-26 07:57:58 +00:00
|
|
|
embedSendOptions(params, opt)
|
2017-11-17 06:20:36 +00:00
|
|
|
|
|
|
|
msg, err := b.sendObject(&p.File, "photo", params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-18 18:33:20 +00:00
|
|
|
msg.Photo.File.stealRef(&p.File)
|
2017-11-18 18:16:16 +00:00
|
|
|
*p = *msg.Photo
|
2017-11-17 06:20:36 +00:00
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2017-11-19 15:16:39 +00:00
|
|
|
// Send delivers media through bot b to recipient.
|
2017-11-17 06:20:36 +00:00
|
|
|
func (a *Audio) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
|
|
|
|
params := map[string]string{
|
2017-11-18 14:41:23 +00:00
|
|
|
"chat_id": to.Recipient(),
|
2017-11-17 13:10:18 +00:00
|
|
|
"caption": a.Caption,
|
2017-11-17 06:20:36 +00:00
|
|
|
}
|
2017-11-26 07:57:58 +00:00
|
|
|
embedSendOptions(params, opt)
|
2017-11-17 06:20:36 +00:00
|
|
|
|
|
|
|
msg, err := b.sendObject(&a.File, "audio", params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-18 18:33:20 +00:00
|
|
|
msg.Audio.File.stealRef(&a.File)
|
2017-11-17 06:20:36 +00:00
|
|
|
*a = *msg.Audio
|
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2017-11-19 15:16:39 +00:00
|
|
|
// Send delivers media through bot b to recipient.
|
2017-11-17 06:20:36 +00:00
|
|
|
func (d *Document) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
|
|
|
|
params := map[string]string{
|
2017-11-18 14:41:23 +00:00
|
|
|
"chat_id": to.Recipient(),
|
2017-11-17 13:10:18 +00:00
|
|
|
"caption": d.Caption,
|
2017-11-17 06:20:36 +00:00
|
|
|
}
|
2017-11-26 07:57:58 +00:00
|
|
|
embedSendOptions(params, opt)
|
2017-11-17 06:20:36 +00:00
|
|
|
|
2017-11-17 06:28:41 +00:00
|
|
|
msg, err := b.sendObject(&d.File, "document", params)
|
2017-11-17 06:20:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-18 18:33:20 +00:00
|
|
|
msg.Document.File.stealRef(&d.File)
|
2017-11-17 06:20:36 +00:00
|
|
|
*d = *msg.Document
|
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2017-11-19 15:16:39 +00:00
|
|
|
// Send delivers media through bot b to recipient.
|
2017-11-17 06:20:36 +00:00
|
|
|
func (s *Sticker) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
|
|
|
|
params := map[string]string{
|
2017-11-18 14:41:23 +00:00
|
|
|
"chat_id": to.Recipient(),
|
2017-11-17 06:20:36 +00:00
|
|
|
}
|
2017-11-26 07:57:58 +00:00
|
|
|
embedSendOptions(params, opt)
|
2017-11-17 06:20:36 +00:00
|
|
|
|
|
|
|
msg, err := b.sendObject(&s.File, "sticker", params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-18 18:33:20 +00:00
|
|
|
msg.Sticker.File.stealRef(&s.File)
|
2017-11-17 06:20:36 +00:00
|
|
|
*s = *msg.Sticker
|
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2017-11-19 15:16:39 +00:00
|
|
|
// Send delivers media through bot b to recipient.
|
2017-11-17 06:20:36 +00:00
|
|
|
func (v *Video) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
|
|
|
|
params := map[string]string{
|
2017-11-18 14:41:23 +00:00
|
|
|
"chat_id": to.Recipient(),
|
2017-11-17 13:10:18 +00:00
|
|
|
"caption": v.Caption,
|
2017-11-17 06:20:36 +00:00
|
|
|
}
|
2017-11-26 07:57:58 +00:00
|
|
|
embedSendOptions(params, opt)
|
2017-11-17 06:20:36 +00:00
|
|
|
|
|
|
|
msg, err := b.sendObject(&v.File, "video", params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-18 18:33:20 +00:00
|
|
|
msg.Video.File.stealRef(&v.File)
|
2017-11-17 06:20:36 +00:00
|
|
|
*v = *msg.Video
|
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2017-11-19 15:16:39 +00:00
|
|
|
// Send delivers media through bot b to recipient.
|
2017-11-17 13:10:18 +00:00
|
|
|
func (v *Voice) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
|
|
|
|
params := map[string]string{
|
2017-11-18 14:41:23 +00:00
|
|
|
"chat_id": to.Recipient(),
|
2017-11-17 13:10:18 +00:00
|
|
|
}
|
2017-11-26 07:57:58 +00:00
|
|
|
embedSendOptions(params, opt)
|
2017-11-17 13:10:18 +00:00
|
|
|
|
|
|
|
msg, err := b.sendObject(&v.File, "voice", params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-18 18:33:20 +00:00
|
|
|
msg.Voice.File.stealRef(&v.File)
|
2017-11-17 13:10:18 +00:00
|
|
|
*v = *msg.Voice
|
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2017-11-19 15:16:39 +00:00
|
|
|
// Send delivers media through bot b to recipient.
|
2017-11-17 13:10:18 +00:00
|
|
|
func (v *VideoNote) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
|
|
|
|
params := map[string]string{
|
2017-11-18 14:41:23 +00:00
|
|
|
"chat_id": to.Recipient(),
|
2017-11-17 13:10:18 +00:00
|
|
|
}
|
2017-11-26 07:57:58 +00:00
|
|
|
embedSendOptions(params, opt)
|
2017-11-17 13:10:18 +00:00
|
|
|
|
|
|
|
msg, err := b.sendObject(&v.File, "videoNote", params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-18 18:33:20 +00:00
|
|
|
msg.VideoNote.File.stealRef(&v.File)
|
2017-11-17 13:10:18 +00:00
|
|
|
*v = *msg.VideoNote
|
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2017-11-19 15:16:39 +00:00
|
|
|
// Send delivers media through bot b to recipient.
|
2017-11-17 06:20:36 +00:00
|
|
|
func (x *Location) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
|
|
|
|
params := map[string]string{
|
2017-11-18 14:41:23 +00:00
|
|
|
"chat_id": to.Recipient(),
|
2017-11-17 13:10:18 +00:00
|
|
|
"latitude": fmt.Sprintf("%f", x.Lat),
|
|
|
|
"longitude": fmt.Sprintf("%f", x.Lng),
|
2017-11-17 06:20:36 +00:00
|
|
|
}
|
2017-11-26 07:57:58 +00:00
|
|
|
embedSendOptions(params, opt)
|
2017-11-17 06:20:36 +00:00
|
|
|
|
|
|
|
respJSON, err := b.sendCommand("sendLocation", params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return extractMsgResponse(respJSON)
|
|
|
|
}
|
|
|
|
|
2017-11-19 15:16:39 +00:00
|
|
|
// Send delivers media through bot b to recipient.
|
2017-11-17 06:20:36 +00:00
|
|
|
func (v *Venue) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
|
|
|
|
params := map[string]string{
|
2017-11-18 14:41:23 +00:00
|
|
|
"chat_id": to.Recipient(),
|
2017-11-17 13:10:18 +00:00
|
|
|
"latitude": fmt.Sprintf("%f", v.Location.Lat),
|
|
|
|
"longitude": fmt.Sprintf("%f", v.Location.Lng),
|
2017-11-17 06:20:36 +00:00
|
|
|
"title": v.Title,
|
|
|
|
"address": v.Address,
|
|
|
|
"foursquare_id": v.FoursquareID,
|
|
|
|
}
|
2017-11-26 07:57:58 +00:00
|
|
|
embedSendOptions(params, opt)
|
2017-11-17 06:20:36 +00:00
|
|
|
|
2017-11-17 06:28:41 +00:00
|
|
|
respJSON, err := b.sendCommand("sendVenue", params)
|
2017-11-17 06:20:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return extractMsgResponse(respJSON)
|
|
|
|
}
|