2017-11-17 06:20:36 +00:00
|
|
|
package telebot
|
|
|
|
|
2018-09-14 09:26:33 +00:00
|
|
|
import (
|
2019-01-28 00:11:10 +00:00
|
|
|
"encoding/json"
|
2018-09-14 09:26:33 +00:00
|
|
|
"fmt"
|
2020-02-29 12:34:33 +00:00
|
|
|
"path/filepath"
|
2018-09-14 09:26:33 +00:00
|
|
|
"strconv"
|
|
|
|
)
|
2017-11-17 06:20:36 +00:00
|
|
|
|
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
|
|
|
|
2018-12-15 23:54:13 +00:00
|
|
|
msg, err := b.sendObject(&p.File, "photo", params, nil)
|
2017-11-17 06:20:36 +00:00
|
|
|
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
|
2019-09-30 16:00:10 +00:00
|
|
|
p.Caption = msg.Caption
|
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{
|
2018-09-14 09:26:33 +00:00
|
|
|
"chat_id": to.Recipient(),
|
|
|
|
"caption": a.Caption,
|
|
|
|
"performer": a.Performer,
|
|
|
|
"title": a.Title,
|
2019-09-30 15:57:54 +00:00
|
|
|
"file_name": a.FileName,
|
2018-09-14 09:26:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if a.Duration != 0 {
|
|
|
|
params["duration"] = strconv.Itoa(a.Duration)
|
2017-11-17 06:20:36 +00:00
|
|
|
}
|
2018-09-14 09:26:33 +00:00
|
|
|
|
2017-11-26 07:57:58 +00:00
|
|
|
embedSendOptions(params, opt)
|
2017-11-17 06:20:36 +00:00
|
|
|
|
2019-12-22 05:07:00 +00:00
|
|
|
msg, err := b.sendObject(&a.File, "audio", params, thumbnailToFilemap(a.Thumbnail))
|
2017-11-17 06:20:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-03 23:29:24 +00:00
|
|
|
if msg.Audio != nil {
|
|
|
|
msg.Audio.File.stealRef(&a.File)
|
|
|
|
*a = *msg.Audio
|
2019-09-30 16:00:10 +00:00
|
|
|
a.Caption = msg.Caption
|
2019-01-03 23:29:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if msg.Document != nil {
|
|
|
|
msg.Document.File.stealRef(&a.File)
|
|
|
|
a.File = msg.Document.File
|
|
|
|
}
|
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 (d *Document) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
|
|
|
|
params := map[string]string{
|
2019-09-30 15:57:54 +00:00
|
|
|
"chat_id": to.Recipient(),
|
|
|
|
"caption": d.Caption,
|
|
|
|
"file_name": d.FileName,
|
2017-11-17 06:20:36 +00:00
|
|
|
}
|
2018-09-14 09:26:33 +00:00
|
|
|
|
|
|
|
if d.FileSize != 0 {
|
|
|
|
params["file_size"] = strconv.Itoa(d.FileSize)
|
|
|
|
}
|
|
|
|
|
2017-11-26 07:57:58 +00:00
|
|
|
embedSendOptions(params, opt)
|
2017-11-17 06:20:36 +00:00
|
|
|
|
2018-12-15 23:54:13 +00:00
|
|
|
msg, err := b.sendObject(&d.File, "document", params, thumbnailToFilemap(d.Thumbnail))
|
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
|
2019-09-30 16:00:10 +00:00
|
|
|
d.Caption = msg.Caption
|
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 (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
|
|
|
|
2018-12-15 23:54:13 +00:00
|
|
|
msg, err := b.sendObject(&s.File, "sticker", params, nil)
|
2017-11-17 06:20:36 +00:00
|
|
|
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{
|
2019-09-30 15:57:54 +00:00
|
|
|
"chat_id": to.Recipient(),
|
|
|
|
"caption": v.Caption,
|
|
|
|
"file_name": v.FileName,
|
2017-11-17 06:20:36 +00:00
|
|
|
}
|
2018-09-14 09:26:33 +00:00
|
|
|
|
|
|
|
if v.Duration != 0 {
|
|
|
|
params["duration"] = strconv.Itoa(v.Duration)
|
|
|
|
}
|
|
|
|
if v.Width != 0 {
|
|
|
|
params["width"] = strconv.Itoa(v.Width)
|
|
|
|
}
|
|
|
|
if v.Height != 0 {
|
|
|
|
params["height"] = strconv.Itoa(v.Height)
|
|
|
|
}
|
|
|
|
if v.SupportsStreaming {
|
|
|
|
params["supports_streaming"] = "true"
|
|
|
|
}
|
|
|
|
|
2017-11-26 07:57:58 +00:00
|
|
|
embedSendOptions(params, opt)
|
2017-11-17 06:20:36 +00:00
|
|
|
|
2018-12-15 23:54:13 +00:00
|
|
|
msg, err := b.sendObject(&v.File, "video", params, thumbnailToFilemap(v.Thumbnail))
|
2017-11-17 06:20:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-18 18:12:29 +00:00
|
|
|
if vid := msg.Video; vid != nil {
|
|
|
|
vid.File.stealRef(&v.File)
|
|
|
|
*v = *vid
|
2019-09-30 16:00:10 +00:00
|
|
|
v.Caption = msg.Caption
|
2018-07-18 18:12:29 +00:00
|
|
|
} else if doc := msg.Document; doc != nil {
|
|
|
|
// If video has no sound, Telegram can turn it into Document (GIF)
|
|
|
|
doc.File.stealRef(&v.File)
|
|
|
|
|
|
|
|
v.Caption = doc.Caption
|
|
|
|
v.MIME = doc.MIME
|
|
|
|
v.Thumbnail = doc.Thumbnail
|
|
|
|
}
|
2017-11-17 06:20:36 +00:00
|
|
|
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
2020-02-29 12:34:33 +00:00
|
|
|
// Send delivers animation through bot b to recipient.
|
|
|
|
// @see https://core.telegram.org/bots/api#sendanimation
|
|
|
|
func (a *Animation) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": to.Recipient(),
|
|
|
|
"caption": a.Caption,
|
|
|
|
"file_name": a.FileName,
|
|
|
|
}
|
|
|
|
|
|
|
|
if a.Duration != 0 {
|
|
|
|
params["duration"] = strconv.Itoa(a.Duration)
|
|
|
|
}
|
|
|
|
if a.Width != 0 {
|
|
|
|
params["width"] = strconv.Itoa(a.Width)
|
|
|
|
}
|
|
|
|
if a.Height != 0 {
|
|
|
|
params["height"] = strconv.Itoa(a.Height)
|
|
|
|
}
|
|
|
|
|
|
|
|
// file_name is required, without file_name GIFs sent as document
|
|
|
|
if params["file_name"] == "" && a.File.OnDisk() {
|
|
|
|
params["file_name"] = filepath.Base(a.File.FileLocal)
|
|
|
|
}
|
|
|
|
|
|
|
|
embedSendOptions(params, opt)
|
|
|
|
|
|
|
|
msg, err := b.sendObject(&a.File, "animation", params, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.Animation.File.stealRef(&a.File)
|
|
|
|
*a = *msg.Animation
|
|
|
|
a.Caption = msg.Caption
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2018-09-14 09:26:33 +00:00
|
|
|
|
|
|
|
if v.Duration != 0 {
|
|
|
|
params["duration"] = strconv.Itoa(v.Duration)
|
|
|
|
}
|
|
|
|
|
2017-11-26 07:57:58 +00:00
|
|
|
embedSendOptions(params, opt)
|
2017-11-17 13:10:18 +00:00
|
|
|
|
2018-12-15 23:54:13 +00:00
|
|
|
msg, err := b.sendObject(&v.File, "voice", params, nil)
|
2017-11-17 13:10:18 +00:00
|
|
|
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
|
|
|
}
|
2018-09-14 09:26:33 +00:00
|
|
|
|
|
|
|
if v.Duration != 0 {
|
|
|
|
params["duration"] = strconv.Itoa(v.Duration)
|
|
|
|
}
|
|
|
|
if v.Length != 0 {
|
|
|
|
params["length"] = strconv.Itoa(v.Length)
|
|
|
|
}
|
|
|
|
|
2017-11-26 07:57:58 +00:00
|
|
|
embedSendOptions(params, opt)
|
2017-11-17 13:10:18 +00:00
|
|
|
|
2018-12-15 23:54:13 +00:00
|
|
|
msg, err := b.sendObject(&v.File, "videoNote", params, thumbnailToFilemap(v.Thumbnail))
|
2017-11-17 13:10:18 +00:00
|
|
|
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-26 09:15:11 +00:00
|
|
|
"chat_id": to.Recipient(),
|
|
|
|
"latitude": fmt.Sprintf("%f", x.Lat),
|
|
|
|
"longitude": fmt.Sprintf("%f", x.Lng),
|
2019-09-17 07:05:21 +00:00
|
|
|
"live_period": strconv.Itoa(x.LivePeriod),
|
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
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("sendLocation", params)
|
2017-11-17 06:20:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractMessage(data)
|
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 (v *Venue) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
|
|
|
|
params := map[string]string{
|
2018-09-14 09:26:33 +00:00
|
|
|
"chat_id": to.Recipient(),
|
|
|
|
"latitude": fmt.Sprintf("%f", v.Location.Lat),
|
|
|
|
"longitude": fmt.Sprintf("%f", v.Location.Lng),
|
|
|
|
"title": v.Title,
|
|
|
|
"address": v.Address,
|
|
|
|
"foursquare_id": v.FoursquareID,
|
|
|
|
"foursquare_type": v.FoursquareType,
|
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
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("sendVenue", params)
|
2017-11-17 06:20:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractMessage(data)
|
2017-11-17 06:20:36 +00:00
|
|
|
}
|
2019-01-28 00:11:10 +00:00
|
|
|
|
|
|
|
// Send delivers media through bot b to recipient.
|
|
|
|
func (i *Invoice) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
|
|
|
|
prices, _ := json.Marshal(i.Prices)
|
|
|
|
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": to.Recipient(),
|
|
|
|
"title": i.Title,
|
|
|
|
"description": i.Description,
|
|
|
|
"start_parameter": i.Start,
|
|
|
|
"payload": i.Payload,
|
|
|
|
"provider_token": i.Token,
|
|
|
|
"currency": i.Currency,
|
|
|
|
"prices": string(prices),
|
|
|
|
}
|
|
|
|
embedSendOptions(params, opt)
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("sendInvoice", params)
|
2019-01-28 00:11:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractMessage(data)
|
2019-01-28 00:11:10 +00:00
|
|
|
}
|
2020-03-30 21:02:28 +00:00
|
|
|
|
2020-04-24 14:52:15 +00:00
|
|
|
// Send delivers poll through bot b to recipient.
|
2020-03-30 21:02:28 +00:00
|
|
|
func (p *Poll) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": to.Recipient(),
|
|
|
|
"question": p.Question,
|
2020-04-24 14:52:15 +00:00
|
|
|
"type": string(p.Type),
|
|
|
|
"is_closed": strconv.FormatBool(p.Closed),
|
|
|
|
"is_anonymous": strconv.FormatBool(p.Anonymous),
|
|
|
|
"allows_multiple_answers": strconv.FormatBool(p.MultipleAnswers),
|
|
|
|
"correct_option_id": strconv.Itoa(p.CorrectOption),
|
|
|
|
}
|
|
|
|
if p.Explanation != "" {
|
|
|
|
params["explanation"] = p.Explanation
|
|
|
|
params["explanation_parse_mode"] = p.ParseMode
|
|
|
|
}
|
|
|
|
if p.OpenPeriod != 0 {
|
|
|
|
params["open_period"] = strconv.Itoa(p.OpenPeriod)
|
|
|
|
} else if p.CloseUnixdate != 0 {
|
|
|
|
params["close_date"] = strconv.FormatInt(p.CloseUnixdate, 10)
|
2020-03-30 21:02:28 +00:00
|
|
|
}
|
|
|
|
embedSendOptions(params, opt)
|
|
|
|
|
|
|
|
var options []string
|
|
|
|
for _, o := range p.Options {
|
|
|
|
options = append(options, o.Text)
|
|
|
|
}
|
|
|
|
opts, _ := json.Marshal(options)
|
|
|
|
params["options"] = string(opts)
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("sendPoll", params)
|
2020-03-30 21:02:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractMessage(data)
|
2020-03-30 21:02:28 +00:00
|
|
|
}
|
2020-04-25 13:25:20 +00:00
|
|
|
|
|
|
|
// Send delivers dice through bot b to recipient
|
2020-04-25 12:31:16 +00:00
|
|
|
func (d *Dice) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
|
2020-04-25 11:41:48 +00:00
|
|
|
params := map[string]string{
|
2020-04-25 12:31:16 +00:00
|
|
|
"chat_id": to.Recipient(),
|
2020-04-25 12:59:53 +00:00
|
|
|
"emoji": string(d.Type),
|
2020-04-25 11:41:48 +00:00
|
|
|
}
|
2020-04-25 12:31:16 +00:00
|
|
|
embedSendOptions(params, opt)
|
2020-04-25 13:25:20 +00:00
|
|
|
|
2020-04-25 11:41:48 +00:00
|
|
|
data, err := b.Raw("sendDice", params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-04-25 13:25:20 +00:00
|
|
|
|
2020-04-25 11:41:48 +00:00
|
|
|
return extractMessage(data)
|
2020-04-25 12:31:16 +00:00
|
|
|
}
|