2015-06-26 07:34:10 +00:00
|
|
|
package telebot
|
|
|
|
|
|
|
|
import (
|
2015-07-03 18:54:40 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2017-11-24 15:58:40 +00:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2017-11-21 02:22:45 +00:00
|
|
|
"regexp"
|
2015-07-03 18:54:40 +00:00
|
|
|
"strconv"
|
2017-11-21 02:22:45 +00:00
|
|
|
"strings"
|
2017-08-15 13:44:01 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2015-06-26 07:34:10 +00:00
|
|
|
)
|
|
|
|
|
2015-07-03 18:54:40 +00:00
|
|
|
// NewBot does try to build a Bot with token `token`, which
|
|
|
|
// is a secret API key assigned to particular bot.
|
2017-11-20 23:41:39 +00:00
|
|
|
func NewBot(pref Settings) (*Bot, error) {
|
|
|
|
if pref.Updates == 0 {
|
|
|
|
pref.Updates = 100
|
2017-08-15 15:00:22 +00:00
|
|
|
}
|
2017-08-15 13:44:01 +00:00
|
|
|
|
2018-04-18 13:42:18 +00:00
|
|
|
client := pref.Client
|
|
|
|
if client == nil {
|
|
|
|
client = http.DefaultClient
|
|
|
|
}
|
|
|
|
|
2018-12-12 22:45:03 +00:00
|
|
|
if pref.URL == "" {
|
|
|
|
pref.URL = DefaultApiURL
|
|
|
|
}
|
|
|
|
|
2017-11-20 23:41:39 +00:00
|
|
|
bot := &Bot{
|
2018-06-30 20:34:04 +00:00
|
|
|
Token: pref.Token,
|
2018-12-12 22:45:03 +00:00
|
|
|
URL: pref.URL,
|
2018-06-30 20:34:04 +00:00
|
|
|
Updates: make(chan Update, pref.Updates),
|
|
|
|
Poller: pref.Poller,
|
2017-11-21 01:50:44 +00:00
|
|
|
|
|
|
|
handlers: make(map[string]interface{}),
|
2017-11-27 12:52:16 +00:00
|
|
|
stop: make(chan struct{}),
|
2017-11-27 15:58:41 +00:00
|
|
|
reporter: pref.Reporter,
|
2018-04-18 18:17:03 +00:00
|
|
|
client: client,
|
2015-07-03 18:54:40 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 23:41:39 +00:00
|
|
|
user, err := bot.getMe()
|
2017-11-18 13:06:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-11-21 01:50:44 +00:00
|
|
|
bot.Me = user
|
2017-11-20 23:41:39 +00:00
|
|
|
return bot, nil
|
|
|
|
}
|
|
|
|
|
2017-11-21 02:22:45 +00:00
|
|
|
// Bot represents a separate Telegram bot instance.
|
|
|
|
type Bot struct {
|
|
|
|
Me *User
|
|
|
|
Token string
|
2018-12-12 22:45:03 +00:00
|
|
|
URL string
|
2017-11-21 02:22:45 +00:00
|
|
|
Updates chan Update
|
|
|
|
Poller Poller
|
|
|
|
|
|
|
|
handlers map[string]interface{}
|
2017-11-27 15:58:41 +00:00
|
|
|
reporter func(error)
|
2017-11-26 03:40:49 +00:00
|
|
|
stop chan struct{}
|
2018-04-18 13:42:18 +00:00
|
|
|
client *http.Client
|
2017-11-21 02:22:45 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 23:41:39 +00:00
|
|
|
// Settings represents a utility struct for passing certain
|
|
|
|
// properties of a bot around and is required to make bots.
|
|
|
|
type Settings struct {
|
2018-12-12 22:45:03 +00:00
|
|
|
// Telegram API Url
|
|
|
|
URL string
|
|
|
|
|
2017-11-20 23:41:39 +00:00
|
|
|
// Telegram token
|
|
|
|
Token string
|
|
|
|
|
2017-11-21 02:22:45 +00:00
|
|
|
// Updates channel capacity
|
|
|
|
Updates int // Default: 100
|
2017-11-20 23:41:39 +00:00
|
|
|
|
|
|
|
// Poller is the provider of Updates.
|
|
|
|
Poller Poller
|
2017-11-27 15:58:41 +00:00
|
|
|
|
|
|
|
// Reporter is a callback function that will get called
|
|
|
|
// on any panics recovered from endpoint handlers.
|
|
|
|
Reporter func(error)
|
2018-04-18 13:42:18 +00:00
|
|
|
|
|
|
|
// HTTP Client used to make requests to telegram api
|
|
|
|
Client *http.Client
|
2017-11-18 13:06:20 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 00:00:58 +00:00
|
|
|
// Update object represents an incoming update.
|
|
|
|
type Update struct {
|
|
|
|
ID int `json:"update_id"`
|
|
|
|
|
2019-07-22 18:10:18 +00:00
|
|
|
Message *Message `json:"message,omitempty"`
|
|
|
|
EditedMessage *Message `json:"edited_message,omitempty"`
|
|
|
|
ChannelPost *Message `json:"channel_post,omitempty"`
|
|
|
|
EditedChannelPost *Message `json:"edited_channel_post,omitempty"`
|
|
|
|
Callback *Callback `json:"callback_query,omitempty"`
|
|
|
|
Query *Query `json:"inline_query,omitempty"`
|
2017-12-26 22:46:50 +00:00
|
|
|
ChosenInlineResult *ChosenInlineResult `json:"chosen_inline_result,omitempty"`
|
2019-07-22 18:10:18 +00:00
|
|
|
PreCheckoutQuery *PreCheckoutQuery `json:"pre_checkout_query,omitempty"`
|
2020-03-30 16:21:06 +00:00
|
|
|
Poll *Poll `json:"poll,omitempty"`
|
|
|
|
PollAnswer *PollAnswer `json:"poll_answer,omitempty"`
|
2017-12-26 01:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ChosenInlineResult represents a result of an inline query that was chosen
|
|
|
|
// by the user and sent to their chat partner.
|
|
|
|
type ChosenInlineResult struct {
|
2020-04-06 13:04:25 +00:00
|
|
|
From User `json:"from"`
|
|
|
|
Location *Location `json:"location,omitempty"`
|
|
|
|
ResultID string `json:"result_id"`
|
|
|
|
Query string `json:"query"`
|
|
|
|
MessageID string `json:"inline_message_id"` // inline messages only!
|
2017-11-21 00:00:58 +00:00
|
|
|
}
|
|
|
|
|
2019-01-28 00:11:10 +00:00
|
|
|
type PreCheckoutQuery struct {
|
|
|
|
Sender *User `json:"from"`
|
2019-03-29 14:25:35 +00:00
|
|
|
ID string `json:"id"`
|
2019-01-28 00:11:10 +00:00
|
|
|
Currency string `json:"currency"`
|
|
|
|
Payload string `json:"invoice_payload"`
|
2019-03-29 14:25:35 +00:00
|
|
|
Total int `json:"total_amount"`
|
2019-01-28 00:11:10 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 02:22:45 +00:00
|
|
|
// Handle lets you set the handler for some command name or
|
|
|
|
// one of the supported endpoints.
|
|
|
|
//
|
2017-11-21 03:08:06 +00:00
|
|
|
// Example:
|
|
|
|
//
|
2020-04-06 08:40:31 +00:00
|
|
|
// b.Handle("/help", func (m *tb.Message) {})
|
|
|
|
// b.Handle(tb.OnText, func (m *tb.Message) {})
|
|
|
|
// b.Handle(tb.OnQuery, func (q *tb.Query) {})
|
2017-11-21 03:08:06 +00:00
|
|
|
//
|
2020-04-06 08:40:31 +00:00
|
|
|
// // make a hook for one of your preserved (by-pointer) inline buttons.
|
|
|
|
// b.Handle(&inlineButton, func (c *tb.Callback) {})
|
2017-11-26 02:33:28 +00:00
|
|
|
//
|
|
|
|
func (b *Bot) Handle(endpoint interface{}, handler interface{}) {
|
|
|
|
switch end := endpoint.(type) {
|
|
|
|
case string:
|
|
|
|
b.handlers[end] = handler
|
|
|
|
case CallbackEndpoint:
|
2017-12-10 18:51:43 +00:00
|
|
|
b.handlers[end.CallbackUnique()] = handler
|
2017-11-27 14:19:42 +00:00
|
|
|
default:
|
|
|
|
panic("telebot: unsupported endpoint")
|
2017-11-26 02:33:28 +00:00
|
|
|
}
|
2017-11-21 02:22:45 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 02:33:28 +00:00
|
|
|
var (
|
2020-04-05 17:23:51 +00:00
|
|
|
cmdRx = regexp.MustCompile(`^(/\w+)(@(\w+))?(\s|$)(.+)?`)
|
2017-12-10 22:32:08 +00:00
|
|
|
cbackRx = regexp.MustCompile(`^\f(\w+)(\|(.+))?$`)
|
2017-11-26 02:33:28 +00:00
|
|
|
)
|
2017-11-21 02:22:45 +00:00
|
|
|
|
2017-11-21 00:00:58 +00:00
|
|
|
// Start brings bot into motion by consuming incoming
|
|
|
|
// updates (see Bot.Updates channel).
|
|
|
|
func (b *Bot) Start() {
|
2017-11-21 01:50:44 +00:00
|
|
|
if b.Poller == nil {
|
|
|
|
panic("telebot: can't start without a poller")
|
|
|
|
}
|
|
|
|
|
2020-04-06 09:57:18 +00:00
|
|
|
stop := make(chan struct{})
|
|
|
|
go b.Poller.Poll(b, b.Updates, stop)
|
2017-11-21 02:22:45 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
// handle incoming updates
|
|
|
|
case upd := <-b.Updates:
|
|
|
|
b.incomingUpdate(&upd)
|
2017-11-26 01:26:07 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
// call to stop polling
|
|
|
|
case <-b.stop:
|
2020-04-06 09:57:18 +00:00
|
|
|
stop <- struct{}{}
|
2017-11-21 02:55:53 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
// polling has stopped
|
2020-04-06 09:57:18 +00:00
|
|
|
case <-stop:
|
2017-11-27 12:52:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-21 02:22:45 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
func (b *Bot) incomingUpdate(upd *Update) {
|
|
|
|
if upd.Message != nil {
|
|
|
|
m := upd.Message
|
2017-11-23 02:13:15 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
if m.PinnedMessage != nil {
|
|
|
|
b.handle(OnPinned, m)
|
|
|
|
return
|
|
|
|
}
|
2017-11-23 02:13:15 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
// Commands
|
|
|
|
if m.Text != "" {
|
|
|
|
// Filtering malicious messsages
|
|
|
|
if m.Text[0] == '\a' {
|
|
|
|
return
|
2017-11-23 02:13:15 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
match := cmdRx.FindAllStringSubmatch(m.Text, -1)
|
2017-11-27 14:56:22 +00:00
|
|
|
if match != nil {
|
2017-11-27 15:58:41 +00:00
|
|
|
// Syntax: "</command>@<bot> <payload>"
|
2017-11-27 14:56:22 +00:00
|
|
|
|
2020-04-06 19:27:58 +00:00
|
|
|
command, botName := match[0][1], match[0][3]
|
2017-11-27 14:56:22 +00:00
|
|
|
if botName != "" && !strings.EqualFold(b.Me.Username, botName) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-06 19:27:58 +00:00
|
|
|
m.Payload = match[0][5]
|
2017-11-27 15:58:41 +00:00
|
|
|
if b.handle(command, m) {
|
|
|
|
return
|
2017-11-27 14:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 15:58:41 +00:00
|
|
|
// 1:1 satisfaction
|
|
|
|
if b.handle(m.Text, m) {
|
|
|
|
return
|
2017-11-26 01:26:07 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
b.handle(OnText, m)
|
|
|
|
return
|
|
|
|
}
|
2017-11-26 01:26:07 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
if b.handleMedia(m) {
|
|
|
|
return
|
|
|
|
}
|
2017-11-26 01:26:07 +00:00
|
|
|
|
2017-12-08 11:09:32 +00:00
|
|
|
wasAdded := (m.UserJoined != nil && m.UserJoined.ID == b.Me.ID) ||
|
|
|
|
(m.UsersJoined != nil && isUserInList(b.Me, m.UsersJoined))
|
2017-11-27 12:52:16 +00:00
|
|
|
if m.GroupCreated || m.SuperGroupCreated || wasAdded {
|
|
|
|
b.handle(OnAddedToGroup, m)
|
|
|
|
return
|
|
|
|
}
|
2017-11-26 01:26:07 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
if m.UserJoined != nil {
|
|
|
|
b.handle(OnUserJoined, m)
|
|
|
|
return
|
|
|
|
}
|
2017-11-26 01:26:07 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
if m.UsersJoined != nil {
|
|
|
|
for _, user := range m.UsersJoined {
|
|
|
|
m.UserJoined = &user
|
|
|
|
b.handle(OnUserJoined, m)
|
2017-11-26 01:26:07 +00:00
|
|
|
}
|
2017-11-27 12:52:16 +00:00
|
|
|
return
|
|
|
|
}
|
2017-11-26 01:26:07 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
if m.UserLeft != nil {
|
|
|
|
b.handle(OnUserLeft, m)
|
|
|
|
return
|
|
|
|
}
|
2017-11-26 01:26:07 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
if m.NewGroupTitle != "" {
|
|
|
|
b.handle(OnNewGroupTitle, m)
|
|
|
|
return
|
2017-11-21 02:22:45 +00:00
|
|
|
}
|
2017-11-21 00:00:58 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
if m.NewGroupPhoto != nil {
|
|
|
|
b.handle(OnNewGroupPhoto, m)
|
|
|
|
return
|
2017-11-21 02:22:45 +00:00
|
|
|
}
|
2017-11-21 00:00:58 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
if m.GroupPhotoDeleted {
|
|
|
|
b.handle(OnGroupPhotoDeleted, m)
|
|
|
|
return
|
2017-11-21 00:00:58 +00:00
|
|
|
}
|
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
if m.MigrateTo != 0 {
|
|
|
|
if handler, ok := b.handlers[OnMigration]; ok {
|
2020-04-06 19:27:58 +00:00
|
|
|
handler, ok := handler.(func(int64, int64))
|
|
|
|
if !ok {
|
2017-11-27 15:58:41 +00:00
|
|
|
panic("telebot: migration handler is bad")
|
2017-11-27 12:52:16 +00:00
|
|
|
}
|
2020-04-06 19:27:58 +00:00
|
|
|
|
|
|
|
go func(b *Bot, handler func(int64, int64), from, to int64) {
|
|
|
|
if b.reporter == nil {
|
|
|
|
defer b.deferDebug()
|
|
|
|
}
|
|
|
|
handler(from, to)
|
|
|
|
}(b, handler, m.Chat.ID, m.MigrateTo)
|
2017-11-27 12:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return
|
2017-11-21 00:00:58 +00:00
|
|
|
}
|
2017-11-27 12:52:16 +00:00
|
|
|
}
|
2017-11-26 02:33:28 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
if upd.EditedMessage != nil {
|
|
|
|
b.handle(OnEdited, upd.EditedMessage)
|
|
|
|
return
|
|
|
|
}
|
2017-11-26 02:33:28 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
if upd.ChannelPost != nil {
|
2020-04-06 11:22:01 +00:00
|
|
|
m := upd.ChannelPost
|
|
|
|
|
|
|
|
if m.PinnedMessage != nil {
|
|
|
|
b.handle(OnPinned, m)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
b.handle(OnChannelPost, upd.ChannelPost)
|
|
|
|
return
|
|
|
|
}
|
2017-11-26 02:33:28 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
if upd.EditedChannelPost != nil {
|
|
|
|
b.handle(OnEditedChannelPost, upd.EditedChannelPost)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if upd.Callback != nil {
|
|
|
|
if upd.Callback.Data != "" {
|
2020-03-29 12:59:27 +00:00
|
|
|
if upd.Callback.MessageID != "" {
|
|
|
|
upd.Callback.Message = &Message{
|
2020-04-06 19:27:58 +00:00
|
|
|
// InlineID indicates that message
|
|
|
|
// is inline so we have only its id
|
2020-03-29 12:59:27 +00:00
|
|
|
InlineID: upd.Callback.MessageID,
|
|
|
|
}
|
|
|
|
}
|
2020-03-29 00:49:17 +00:00
|
|
|
|
2020-03-29 12:59:27 +00:00
|
|
|
data := upd.Callback.Data
|
2017-11-27 12:52:16 +00:00
|
|
|
if data[0] == '\f' {
|
|
|
|
match := cbackRx.FindAllStringSubmatch(data, -1)
|
|
|
|
if match != nil {
|
2017-12-10 22:32:08 +00:00
|
|
|
unique, payload := match[0][1], match[0][3]
|
2017-11-27 12:52:16 +00:00
|
|
|
|
|
|
|
if handler, ok := b.handlers["\f"+unique]; ok {
|
2020-04-06 19:27:58 +00:00
|
|
|
handler, ok := handler.(func(*Callback))
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Errorf("telebot: %s callback handler is bad", unique))
|
2017-11-27 12:52:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 19:27:58 +00:00
|
|
|
upd.Callback.Data = payload
|
|
|
|
go func(b *Bot, handler func(*Callback), c *Callback) {
|
|
|
|
if b.reporter == nil {
|
|
|
|
defer b.deferDebug()
|
|
|
|
}
|
|
|
|
handler(c)
|
|
|
|
}(b, handler, upd.Callback)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2017-11-26 02:33:28 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-27 12:52:16 +00:00
|
|
|
}
|
2017-11-26 02:33:28 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
if handler, ok := b.handlers[OnCallback]; ok {
|
2020-04-06 19:27:58 +00:00
|
|
|
handler, ok := handler.(func(*Callback))
|
|
|
|
if !ok {
|
2017-11-27 15:58:41 +00:00
|
|
|
panic("telebot: callback handler is bad")
|
2017-11-21 02:22:45 +00:00
|
|
|
}
|
2020-04-06 19:27:58 +00:00
|
|
|
|
|
|
|
go func(b *Bot, handler func(*Callback), c *Callback) {
|
|
|
|
if b.reporter == nil {
|
|
|
|
defer b.deferDebug()
|
|
|
|
}
|
|
|
|
handler(c)
|
|
|
|
}(b, handler, upd.Callback)
|
2017-11-21 02:22:45 +00:00
|
|
|
}
|
2020-04-06 19:27:58 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
return
|
|
|
|
}
|
2017-11-21 02:22:45 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
if upd.Query != nil {
|
|
|
|
if handler, ok := b.handlers[OnQuery]; ok {
|
2020-04-06 19:27:58 +00:00
|
|
|
handler, ok := handler.(func(*Query))
|
|
|
|
if !ok {
|
2017-11-27 15:58:41 +00:00
|
|
|
panic("telebot: query handler is bad")
|
2017-11-21 02:22:45 +00:00
|
|
|
}
|
2020-04-06 19:27:58 +00:00
|
|
|
|
|
|
|
go func(b *Bot, handler func(*Query), q *Query) {
|
|
|
|
if b.reporter == nil {
|
|
|
|
defer b.deferDebug()
|
|
|
|
}
|
|
|
|
handler(q)
|
|
|
|
}(b, handler, upd.Query)
|
2017-11-21 00:00:58 +00:00
|
|
|
}
|
2020-04-06 19:27:58 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
return
|
2017-11-21 00:00:58 +00:00
|
|
|
}
|
2017-12-26 01:07:36 +00:00
|
|
|
|
|
|
|
if upd.ChosenInlineResult != nil {
|
|
|
|
if handler, ok := b.handlers[OnChosenInlineResult]; ok {
|
2020-04-06 19:27:58 +00:00
|
|
|
handler, ok := handler.(func(*ChosenInlineResult))
|
|
|
|
if !ok {
|
2017-12-26 01:07:36 +00:00
|
|
|
panic("telebot: chosen inline result handler is bad")
|
|
|
|
}
|
2020-04-06 19:27:58 +00:00
|
|
|
|
|
|
|
go func(b *Bot, handler func(*ChosenInlineResult), r *ChosenInlineResult) {
|
|
|
|
if b.reporter == nil {
|
|
|
|
defer b.deferDebug()
|
|
|
|
}
|
|
|
|
handler(r)
|
|
|
|
}(b, handler, upd.ChosenInlineResult)
|
2017-12-26 01:07:36 +00:00
|
|
|
}
|
2020-04-06 19:27:58 +00:00
|
|
|
|
2017-12-26 01:07:36 +00:00
|
|
|
return
|
|
|
|
}
|
2019-01-28 00:11:10 +00:00
|
|
|
|
|
|
|
if upd.PreCheckoutQuery != nil {
|
|
|
|
if handler, ok := b.handlers[OnCheckout]; ok {
|
2020-04-06 19:27:58 +00:00
|
|
|
handler, ok := handler.(func(*PreCheckoutQuery))
|
|
|
|
if !ok {
|
|
|
|
panic("telebot: pre checkout query handler is bad")
|
2019-01-28 00:11:10 +00:00
|
|
|
}
|
2020-04-06 19:27:58 +00:00
|
|
|
|
|
|
|
go func(b *Bot, handler func(*PreCheckoutQuery), pre *PreCheckoutQuery) {
|
|
|
|
if b.reporter == nil {
|
|
|
|
defer b.deferDebug()
|
|
|
|
}
|
|
|
|
handler(pre)
|
|
|
|
}(b, handler, upd.PreCheckoutQuery)
|
2019-01-28 00:11:10 +00:00
|
|
|
}
|
2020-04-06 19:27:58 +00:00
|
|
|
|
2019-01-28 00:11:10 +00:00
|
|
|
return
|
|
|
|
}
|
2020-03-30 16:21:06 +00:00
|
|
|
|
|
|
|
if upd.Poll != nil {
|
|
|
|
if handler, ok := b.handlers[OnPoll]; ok {
|
2020-04-06 19:27:58 +00:00
|
|
|
handler, ok := handler.(func(*Poll))
|
|
|
|
if !ok {
|
2020-03-30 16:21:06 +00:00
|
|
|
panic("telebot: poll handler is bad")
|
|
|
|
}
|
2020-04-06 19:27:58 +00:00
|
|
|
|
|
|
|
go func(b *Bot, handler func(*Poll), p *Poll) {
|
|
|
|
if b.reporter == nil {
|
|
|
|
defer b.deferDebug()
|
|
|
|
}
|
|
|
|
handler(p)
|
|
|
|
}(b, handler, upd.Poll)
|
2020-03-30 16:21:06 +00:00
|
|
|
}
|
2020-04-06 19:27:58 +00:00
|
|
|
|
2020-03-30 16:21:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if upd.PollAnswer != nil {
|
|
|
|
if handler, ok := b.handlers[OnPollAnswer]; ok {
|
2020-04-06 19:27:58 +00:00
|
|
|
handler, ok := handler.(func(*PollAnswer))
|
|
|
|
if !ok {
|
2020-03-30 16:21:06 +00:00
|
|
|
panic("telebot: poll answer handler is bad")
|
|
|
|
}
|
2020-04-06 19:27:58 +00:00
|
|
|
|
|
|
|
go func(b *Bot, handler func(*PollAnswer), pa *PollAnswer) {
|
|
|
|
if b.reporter == nil {
|
|
|
|
defer b.deferDebug()
|
|
|
|
}
|
|
|
|
handler(pa)
|
|
|
|
}(b, handler, upd.PollAnswer)
|
2020-03-30 16:21:06 +00:00
|
|
|
}
|
2020-04-06 19:27:58 +00:00
|
|
|
|
2020-03-30 16:21:06 +00:00
|
|
|
return
|
|
|
|
}
|
2017-11-21 00:00:58 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 15:24:07 +00:00
|
|
|
func (b *Bot) handle(end string, m *Message) bool {
|
2020-04-06 19:27:58 +00:00
|
|
|
if handler, ok := b.handlers[end]; ok {
|
|
|
|
handler, ok := handler.(func(*Message))
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Errorf("telebot: %s handler is bad", end))
|
|
|
|
}
|
2017-11-24 15:24:07 +00:00
|
|
|
|
2017-11-27 15:58:41 +00:00
|
|
|
go func(b *Bot, handler func(*Message), m *Message) {
|
2019-03-29 14:25:35 +00:00
|
|
|
if b.reporter == nil {
|
|
|
|
defer b.deferDebug()
|
|
|
|
}
|
2017-11-27 15:58:41 +00:00
|
|
|
handler(m)
|
|
|
|
}(b, handler, m)
|
|
|
|
|
2017-11-24 15:24:07 +00:00
|
|
|
return true
|
|
|
|
}
|
2017-11-26 02:33:28 +00:00
|
|
|
|
|
|
|
return false
|
2017-11-24 15:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bot) handleMedia(m *Message) bool {
|
2020-04-06 19:27:58 +00:00
|
|
|
switch {
|
|
|
|
case m.Photo != nil:
|
2017-11-24 15:24:07 +00:00
|
|
|
b.handle(OnPhoto, m)
|
2020-04-06 19:27:58 +00:00
|
|
|
case m.Voice != nil:
|
2018-04-24 15:46:07 +00:00
|
|
|
b.handle(OnVoice, m)
|
2020-04-06 19:27:58 +00:00
|
|
|
case m.Audio != nil:
|
2017-11-24 15:24:07 +00:00
|
|
|
b.handle(OnAudio, m)
|
2020-04-06 19:27:58 +00:00
|
|
|
case m.Document != nil:
|
2017-11-24 15:24:07 +00:00
|
|
|
b.handle(OnDocument, m)
|
2020-04-06 19:27:58 +00:00
|
|
|
case m.Sticker != nil:
|
2017-11-24 15:24:07 +00:00
|
|
|
b.handle(OnSticker, m)
|
2020-04-06 19:27:58 +00:00
|
|
|
case m.Video != nil:
|
2017-11-24 15:24:07 +00:00
|
|
|
b.handle(OnVideo, m)
|
2020-04-06 19:27:58 +00:00
|
|
|
case m.VideoNote != nil:
|
2017-11-24 15:24:07 +00:00
|
|
|
b.handle(OnVideoNote, m)
|
2020-04-06 19:27:58 +00:00
|
|
|
case m.Contact != nil:
|
2017-11-24 15:24:07 +00:00
|
|
|
b.handle(OnContact, m)
|
2020-04-06 19:27:58 +00:00
|
|
|
case m.Location != nil:
|
2017-11-24 15:24:07 +00:00
|
|
|
b.handle(OnLocation, m)
|
2020-04-06 19:27:58 +00:00
|
|
|
case m.Venue != nil:
|
2017-11-24 15:24:07 +00:00
|
|
|
b.handle(OnVenue, m)
|
2020-04-06 19:27:58 +00:00
|
|
|
default:
|
|
|
|
return false
|
2017-11-24 15:24:07 +00:00
|
|
|
}
|
2020-04-06 19:27:58 +00:00
|
|
|
return true
|
2017-11-24 15:24:07 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 03:40:49 +00:00
|
|
|
// Stop gracefully shuts the poller down.
|
|
|
|
func (b *Bot) Stop() {
|
2017-11-27 12:52:16 +00:00
|
|
|
b.stop <- struct{}{}
|
2017-11-26 03:40:49 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:29:44 +00:00
|
|
|
// Send accepts 2+ arguments, starting with destination chat, followed by
|
|
|
|
// some Sendable (or string!) and optional send options.
|
|
|
|
//
|
2018-10-11 11:40:16 +00:00
|
|
|
// Note: since most arguments are of type interface{}, but have pointer
|
2020-03-29 12:59:27 +00:00
|
|
|
// method receivers, make sure to pass them by-pointer, NOT by-value.
|
2017-11-17 14:29:44 +00:00
|
|
|
//
|
|
|
|
// What is a send option exactly? It can be one of the following types:
|
|
|
|
//
|
2017-11-21 03:08:06 +00:00
|
|
|
// - *SendOptions (the actual object accepted by Telegram API)
|
|
|
|
// - *ReplyMarkup (a component of SendOptions)
|
|
|
|
// - Option (a shorcut flag for popular options)
|
|
|
|
// - ParseMode (HTML, Markdown, etc)
|
2017-11-17 14:29:44 +00:00
|
|
|
//
|
|
|
|
// This function will panic upon unsupported payloads and options!
|
2017-11-19 01:44:31 +00:00
|
|
|
func (b *Bot) Send(to Recipient, what interface{}, options ...interface{}) (*Message, error) {
|
|
|
|
sendOpts := extractOptions(options)
|
2015-07-06 13:46:19 +00:00
|
|
|
|
2017-11-17 06:20:36 +00:00
|
|
|
switch object := what.(type) {
|
|
|
|
case string:
|
2017-11-19 01:44:31 +00:00
|
|
|
return b.sendText(to, object, sendOpts)
|
2017-11-17 06:20:36 +00:00
|
|
|
case Sendable:
|
2017-11-19 01:44:31 +00:00
|
|
|
return object.Send(b, to, sendOpts)
|
2017-11-17 06:20:36 +00:00
|
|
|
default:
|
2018-10-11 11:40:16 +00:00
|
|
|
return nil, errors.New("telebot: unsupported sendable")
|
2015-07-03 18:54:40 +00:00
|
|
|
}
|
2015-06-26 16:12:54 +00:00
|
|
|
}
|
2015-07-02 09:05:50 +00:00
|
|
|
|
2017-11-25 14:22:13 +00:00
|
|
|
// SendAlbum is used when sending multiple instances of media as a single
|
|
|
|
// message (so-called album).
|
2017-11-17 14:29:44 +00:00
|
|
|
//
|
2017-11-25 14:22:13 +00:00
|
|
|
// From all existing options, it only supports telebot.Silent.
|
|
|
|
func (b *Bot) SendAlbum(to Recipient, a Album, options ...interface{}) ([]Message, error) {
|
|
|
|
media := make([]string, len(a))
|
2018-10-11 11:39:07 +00:00
|
|
|
files := make(map[string]File)
|
2017-11-25 14:22:13 +00:00
|
|
|
|
|
|
|
for i, x := range a {
|
2018-09-15 12:15:08 +00:00
|
|
|
var mediaRepr string
|
|
|
|
var jsonRepr []byte
|
2017-11-25 14:22:13 +00:00
|
|
|
|
2018-09-15 12:15:08 +00:00
|
|
|
f := x.MediaFile()
|
2017-11-25 14:22:13 +00:00
|
|
|
|
2020-03-29 12:59:27 +00:00
|
|
|
switch {
|
|
|
|
case f.InCloud():
|
2017-11-25 14:22:13 +00:00
|
|
|
mediaRepr = f.FileID
|
2020-03-29 12:59:27 +00:00
|
|
|
case f.FileURL != "":
|
2017-11-25 14:22:13 +00:00
|
|
|
mediaRepr = f.FileURL
|
2020-03-29 12:59:27 +00:00
|
|
|
case f.OnDisk() || f.FileReader != nil:
|
|
|
|
mediaRepr = "attach://" + strconv.Itoa(i)
|
2018-10-11 11:39:07 +00:00
|
|
|
files[strconv.Itoa(i)] = *f
|
2020-03-29 12:59:27 +00:00
|
|
|
default:
|
2017-11-25 14:22:13 +00:00
|
|
|
return nil, errors.Errorf(
|
|
|
|
"telebot: album entry #%d doesn't exist anywhere", i)
|
|
|
|
}
|
|
|
|
|
2018-09-15 12:15:08 +00:00
|
|
|
switch y := x.(type) {
|
|
|
|
case *Photo:
|
|
|
|
jsonRepr, _ = json.Marshal(struct {
|
2020-03-29 12:59:27 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
Media string `json:"media"`
|
|
|
|
Caption string `json:"caption,omitempty"`
|
|
|
|
ParseMode ParseMode `json:"parse_mode,omitempty"`
|
2018-09-15 12:15:08 +00:00
|
|
|
}{
|
|
|
|
"photo",
|
2020-03-29 00:49:17 +00:00
|
|
|
mediaRepr,
|
2020-03-29 12:59:27 +00:00
|
|
|
y.Caption,
|
|
|
|
y.ParseMode,
|
2018-09-15 12:15:08 +00:00
|
|
|
})
|
|
|
|
case *Video:
|
|
|
|
jsonRepr, _ = json.Marshal(struct {
|
|
|
|
Type string `json:"type"`
|
|
|
|
Caption string `json:"caption"`
|
|
|
|
Media string `json:"media"`
|
|
|
|
Width int `json:"width,omitempty"`
|
|
|
|
Height int `json:"height,omitempty"`
|
|
|
|
Duration int `json:"duration,omitempty"`
|
|
|
|
SupportsStreaming bool `json:"supports_streaming,omitempty"`
|
|
|
|
}{
|
|
|
|
"video",
|
|
|
|
y.Caption,
|
|
|
|
mediaRepr,
|
|
|
|
y.Width,
|
|
|
|
y.Height,
|
|
|
|
y.Duration,
|
|
|
|
y.SupportsStreaming,
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("telebot: album entry #%d is not valid", i)
|
|
|
|
}
|
2017-11-25 14:22:13 +00:00
|
|
|
|
|
|
|
media[i] = string(jsonRepr)
|
|
|
|
}
|
|
|
|
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": to.Recipient(),
|
|
|
|
"media": "[" + strings.Join(media, ",") + "]",
|
|
|
|
}
|
|
|
|
|
|
|
|
sendOpts := extractOptions(options)
|
2017-11-26 07:57:58 +00:00
|
|
|
embedSendOptions(params, sendOpts)
|
2017-11-25 14:22:13 +00:00
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.sendFiles("sendMediaGroup", files, params)
|
2017-11-26 02:33:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-11-25 14:22:13 +00:00
|
|
|
|
|
|
|
var resp struct {
|
|
|
|
Ok bool
|
|
|
|
Result []Message
|
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
err = json.Unmarshal(data, &resp)
|
2017-11-25 14:22:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "bad response json")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !resp.Ok {
|
|
|
|
return nil, errors.Errorf("api error: %s", resp.Description)
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:59:27 +00:00
|
|
|
for attachName := range files {
|
2017-11-25 14:22:13 +00:00
|
|
|
i, _ := strconv.Atoi(attachName)
|
|
|
|
|
|
|
|
var newID string
|
|
|
|
if resp.Result[i].Photo != nil {
|
|
|
|
newID = resp.Result[i].Photo.FileID
|
|
|
|
} else {
|
|
|
|
newID = resp.Result[i].Video.FileID
|
|
|
|
}
|
|
|
|
|
|
|
|
a[i].MediaFile().FileID = newID
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reply behaves just like Send() with an exception of "reply-to" indicator.
|
2017-11-19 01:44:31 +00:00
|
|
|
func (b *Bot) Reply(to *Message, what interface{}, options ...interface{}) (*Message, error) {
|
2017-11-20 23:41:39 +00:00
|
|
|
// This function will panic upon unsupported payloads and options!
|
2017-11-19 01:44:31 +00:00
|
|
|
sendOpts := extractOptions(options)
|
|
|
|
if sendOpts == nil {
|
|
|
|
sendOpts = &SendOptions{}
|
2015-07-03 18:54:40 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 01:44:31 +00:00
|
|
|
sendOpts.ReplyTo = to
|
2015-07-03 18:54:40 +00:00
|
|
|
|
2017-11-19 01:44:31 +00:00
|
|
|
return b.Send(to.Chat, what, sendOpts)
|
2015-07-02 09:05:50 +00:00
|
|
|
}
|
2015-07-02 18:39:39 +00:00
|
|
|
|
2017-11-17 14:29:44 +00:00
|
|
|
// Forward behaves just like Send() but of all options it
|
|
|
|
// only supports Silent (see Bots API).
|
|
|
|
//
|
|
|
|
// This function will panic upon unsupported payloads and options!
|
2017-11-19 01:44:31 +00:00
|
|
|
func (b *Bot) Forward(to Recipient, what *Message, options ...interface{}) (*Message, error) {
|
2016-06-26 14:05:37 +00:00
|
|
|
params := map[string]string{
|
2017-11-18 14:41:23 +00:00
|
|
|
"chat_id": to.Recipient(),
|
|
|
|
"from_chat_id": what.Chat.Recipient(),
|
2017-11-17 06:20:36 +00:00
|
|
|
"message_id": strconv.Itoa(what.ID),
|
2015-07-06 13:46:19 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 01:44:31 +00:00
|
|
|
sendOpts := extractOptions(options)
|
2017-11-26 07:57:58 +00:00
|
|
|
embedSendOptions(params, sendOpts)
|
2015-07-03 18:54:40 +00:00
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("forwardMessage", params)
|
2015-07-03 18:54:40 +00:00
|
|
|
if err != nil {
|
2017-01-12 00:48:11 +00:00
|
|
|
return nil, err
|
2015-07-03 18:54:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractMessage(data)
|
2016-05-24 04:43:16 +00:00
|
|
|
}
|
2016-07-04 10:56:53 +00:00
|
|
|
|
2017-11-19 01:44:31 +00:00
|
|
|
// Edit is magic, it lets you change already sent message.
|
|
|
|
//
|
|
|
|
// Use cases:
|
|
|
|
//
|
|
|
|
// b.Edit(msg, msg.Text, newMarkup)
|
|
|
|
// b.Edit(msg, "new <b>text</b>", tb.ModeHTML)
|
|
|
|
//
|
2017-11-26 09:15:11 +00:00
|
|
|
// // Edit live location:
|
|
|
|
// b.Edit(liveMsg, tb.Location{42.1337, 69.4242})
|
|
|
|
//
|
|
|
|
func (b *Bot) Edit(message Editable, what interface{}, options ...interface{}) (*Message, error) {
|
|
|
|
messageID, chatID := message.MessageSig()
|
2017-11-19 01:44:31 +00:00
|
|
|
|
2017-11-26 09:15:11 +00:00
|
|
|
params := map[string]string{}
|
|
|
|
|
|
|
|
switch v := what.(type) {
|
|
|
|
case string:
|
|
|
|
params["text"] = v
|
|
|
|
case Location:
|
|
|
|
params["latitude"] = fmt.Sprintf("%f", v.Lat)
|
|
|
|
params["longitude"] = fmt.Sprintf("%f", v.Lng)
|
|
|
|
default:
|
|
|
|
panic("telebot: unsupported what argument")
|
|
|
|
}
|
2017-11-19 01:44:31 +00:00
|
|
|
|
|
|
|
// if inline message
|
|
|
|
if chatID == 0 {
|
2017-12-26 22:39:15 +00:00
|
|
|
params["inline_message_id"] = messageID
|
2017-11-19 01:44:31 +00:00
|
|
|
} else {
|
|
|
|
params["chat_id"] = strconv.FormatInt(chatID, 10)
|
2017-12-26 22:39:15 +00:00
|
|
|
params["message_id"] = messageID
|
2017-11-19 01:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sendOpts := extractOptions(options)
|
2017-11-26 07:57:58 +00:00
|
|
|
embedSendOptions(params, sendOpts)
|
2017-11-19 01:44:31 +00:00
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("editMessageText", params)
|
2017-11-19 01:44:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractMessage(data)
|
2017-11-19 01:44:31 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:59:27 +00:00
|
|
|
// EditReplyMarkup used to edit reply markup of already sent message.
|
|
|
|
//
|
|
|
|
// On success, returns edited message object
|
|
|
|
func (b *Bot) EditReplyMarkup(message Editable, markup *ReplyMarkup) (*Message, error) {
|
|
|
|
messageID, chatID := message.MessageSig()
|
|
|
|
|
|
|
|
params := map[string]string{}
|
|
|
|
|
|
|
|
// if inline message
|
|
|
|
if chatID == 0 {
|
|
|
|
params["inline_message_id"] = messageID
|
|
|
|
} else {
|
|
|
|
params["chat_id"] = strconv.FormatInt(chatID, 10)
|
|
|
|
params["message_id"] = messageID
|
|
|
|
}
|
|
|
|
|
|
|
|
processButtons(markup.InlineKeyboard)
|
|
|
|
jsonMarkup, _ := json.Marshal(markup)
|
|
|
|
params["reply_markup"] = string(jsonMarkup)
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("editMessageReplyMarkup", params)
|
2020-03-29 12:59:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractMessage(data)
|
2020-03-29 12:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EditCaption used to edit already sent photo caption with known recipient and message id.
|
2017-11-19 01:44:31 +00:00
|
|
|
//
|
|
|
|
// On success, returns edited message object
|
2020-03-29 12:59:27 +00:00
|
|
|
func (b *Bot) EditCaption(message Editable, caption string, options ...interface{}) (*Message, error) {
|
|
|
|
messageID, chatID := message.MessageSig()
|
2017-11-19 01:44:31 +00:00
|
|
|
|
|
|
|
params := map[string]string{"caption": caption}
|
|
|
|
|
|
|
|
// if inline message
|
|
|
|
if chatID == 0 {
|
2017-12-26 22:39:15 +00:00
|
|
|
params["inline_message_id"] = messageID
|
2017-11-19 01:44:31 +00:00
|
|
|
} else {
|
|
|
|
params["chat_id"] = strconv.FormatInt(chatID, 10)
|
2017-12-26 22:39:15 +00:00
|
|
|
params["message_id"] = messageID
|
2017-11-19 01:44:31 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:59:27 +00:00
|
|
|
sendOpts := extractOptions(options)
|
|
|
|
embedSendOptions(params, sendOpts)
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("editMessageCaption", params)
|
2017-11-19 01:44:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractMessage(data)
|
2017-11-19 01:44:31 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:59:27 +00:00
|
|
|
// EditMedia used to edit already sent media with known recipient and message id.
|
2019-02-20 06:11:21 +00:00
|
|
|
//
|
|
|
|
// Use cases:
|
|
|
|
//
|
|
|
|
// bot.EditMedia(msg, &tb.Photo{File: tb.FromDisk("chicken.jpg")});
|
|
|
|
// bot.EditMedia(msg, &tb.Video{File: tb.FromURL("http://video.mp4")});
|
|
|
|
//
|
|
|
|
func (b *Bot) EditMedia(message Editable, inputMedia InputMedia, options ...interface{}) (*Message, error) {
|
2019-07-22 18:10:18 +00:00
|
|
|
var mediaRepr string
|
|
|
|
var jsonRepr []byte
|
|
|
|
var thumb *Photo
|
2019-02-20 06:11:21 +00:00
|
|
|
|
2019-07-22 18:10:18 +00:00
|
|
|
file := make(map[string]File)
|
2019-02-20 06:11:21 +00:00
|
|
|
|
2019-07-22 18:10:18 +00:00
|
|
|
f := inputMedia.MediaFile()
|
2020-03-29 12:59:27 +00:00
|
|
|
thumbAttachName := "thumb"
|
2019-02-20 06:11:21 +00:00
|
|
|
|
2020-03-29 12:59:27 +00:00
|
|
|
switch {
|
|
|
|
case f.InCloud():
|
2019-07-22 18:10:18 +00:00
|
|
|
mediaRepr = f.FileID
|
2020-03-29 12:59:27 +00:00
|
|
|
case f.FileURL != "":
|
2019-07-22 18:10:18 +00:00
|
|
|
mediaRepr = f.FileURL
|
2020-03-29 12:59:27 +00:00
|
|
|
case f.OnDisk() || f.FileReader != nil:
|
2019-07-22 18:10:18 +00:00
|
|
|
s := f.FileLocal
|
|
|
|
if f.FileReader != nil {
|
|
|
|
s = "0"
|
2019-02-20 06:11:21 +00:00
|
|
|
}
|
2020-03-29 12:59:27 +00:00
|
|
|
if s == thumbAttachName {
|
|
|
|
thumbAttachName = "thumb2"
|
|
|
|
}
|
2019-07-22 18:10:18 +00:00
|
|
|
mediaRepr = "attach://" + s
|
|
|
|
file[s] = *f
|
2020-03-29 12:59:27 +00:00
|
|
|
default:
|
2019-02-20 06:11:21 +00:00
|
|
|
return nil, errors.Errorf(
|
2019-07-22 18:10:18 +00:00
|
|
|
"telebot: can't edit media, it doesn't exist anywhere")
|
2019-02-20 06:11:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:59:27 +00:00
|
|
|
type FileJSON struct {
|
2019-02-20 06:11:21 +00:00
|
|
|
// All types.
|
2020-03-29 12:59:27 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
Caption string `json:"caption"`
|
|
|
|
Media string `json:"media"`
|
|
|
|
ParseMode ParseMode `json:"parse_mode,omitempty"`
|
2019-02-20 06:11:21 +00:00
|
|
|
|
|
|
|
// Video.
|
2019-07-22 18:10:18 +00:00
|
|
|
Width int `json:"width,omitempty"`
|
|
|
|
Height int `json:"height,omitempty"`
|
|
|
|
SupportsStreaming bool `json:"supports_streaming,omitempty"`
|
2019-02-20 06:11:21 +00:00
|
|
|
|
|
|
|
// Video and audio.
|
2019-07-22 18:10:18 +00:00
|
|
|
Duration int `json:"duration,omitempty"`
|
2019-02-20 06:11:21 +00:00
|
|
|
|
|
|
|
// Document.
|
2019-07-22 18:10:18 +00:00
|
|
|
FileName string `json:"file_name"`
|
2019-02-20 06:11:21 +00:00
|
|
|
|
|
|
|
// Document, video and audio.
|
2019-07-22 18:10:18 +00:00
|
|
|
Thumbnail string `json:"thumb,omitempty"`
|
|
|
|
MIME string `json:"mime_type,omitempty"`
|
2019-02-20 06:11:21 +00:00
|
|
|
|
|
|
|
// Audio.
|
2019-07-22 18:10:18 +00:00
|
|
|
Title string `json:"title,omitempty"`
|
|
|
|
Performer string `json:"performer,omitempty"`
|
2019-02-20 06:11:21 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:59:27 +00:00
|
|
|
resultMedia := &FileJSON{Media: mediaRepr}
|
|
|
|
|
|
|
|
sendOpts := extractOptions(options)
|
|
|
|
if sendOpts != nil {
|
|
|
|
resultMedia.ParseMode = sendOpts.ParseMode
|
|
|
|
}
|
2019-02-20 06:11:21 +00:00
|
|
|
|
|
|
|
switch y := inputMedia.(type) {
|
2019-07-22 18:10:18 +00:00
|
|
|
case *Photo:
|
|
|
|
resultMedia.Type = "photo"
|
|
|
|
resultMedia.Caption = y.Caption
|
|
|
|
case *Video:
|
|
|
|
resultMedia.Type = "video"
|
|
|
|
resultMedia.Caption = y.Caption
|
|
|
|
resultMedia.Width = y.Width
|
|
|
|
resultMedia.Height = y.Height
|
|
|
|
resultMedia.Duration = y.Duration
|
|
|
|
resultMedia.SupportsStreaming = y.SupportsStreaming
|
|
|
|
resultMedia.MIME = y.MIME
|
|
|
|
thumb = y.Thumbnail
|
|
|
|
if thumb != nil {
|
2020-03-29 12:59:27 +00:00
|
|
|
resultMedia.Thumbnail = "attach://" + thumbAttachName
|
2019-07-22 18:10:18 +00:00
|
|
|
}
|
|
|
|
case *Document:
|
|
|
|
resultMedia.Type = "document"
|
|
|
|
resultMedia.Caption = y.Caption
|
|
|
|
resultMedia.FileName = y.FileName
|
|
|
|
resultMedia.MIME = y.MIME
|
|
|
|
thumb = y.Thumbnail
|
|
|
|
if thumb != nil {
|
2020-03-29 12:59:27 +00:00
|
|
|
resultMedia.Thumbnail = "attach://" + thumbAttachName
|
2019-07-22 18:10:18 +00:00
|
|
|
}
|
|
|
|
case *Audio:
|
|
|
|
resultMedia.Type = "audio"
|
|
|
|
resultMedia.Caption = y.Caption
|
|
|
|
resultMedia.Duration = y.Duration
|
|
|
|
resultMedia.MIME = y.MIME
|
|
|
|
resultMedia.Title = y.Title
|
|
|
|
resultMedia.Performer = y.Performer
|
2020-03-29 12:59:27 +00:00
|
|
|
thumb = y.Thumbnail
|
|
|
|
if thumb != nil {
|
|
|
|
resultMedia.Thumbnail = "attach://" + thumbAttachName
|
|
|
|
}
|
2019-07-22 18:10:18 +00:00
|
|
|
default:
|
|
|
|
return nil, errors.Errorf("telebot: inputMedia entry is not valid")
|
2019-02-20 06:11:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 18:10:18 +00:00
|
|
|
messageID, chatID := message.MessageSig()
|
2019-02-20 06:11:21 +00:00
|
|
|
|
2019-07-22 18:10:18 +00:00
|
|
|
jsonRepr, _ = json.Marshal(resultMedia)
|
|
|
|
params := map[string]string{}
|
|
|
|
params["media"] = string(jsonRepr)
|
2019-02-20 06:11:21 +00:00
|
|
|
|
|
|
|
// If inline message.
|
|
|
|
if chatID == 0 {
|
2019-07-22 18:10:18 +00:00
|
|
|
params["inline_message_id"] = messageID
|
2019-02-20 06:11:21 +00:00
|
|
|
} else {
|
2019-07-22 18:10:18 +00:00
|
|
|
params["chat_id"] = strconv.FormatInt(chatID, 10)
|
|
|
|
params["message_id"] = messageID
|
2019-02-20 06:11:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if thumb != nil {
|
2020-03-29 12:59:27 +00:00
|
|
|
file[thumbAttachName] = *thumb.MediaFile()
|
2019-02-20 06:11:21 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 18:10:18 +00:00
|
|
|
embedSendOptions(params, sendOpts)
|
2019-02-20 06:11:21 +00:00
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.sendFiles("editMessageMedia", file, params)
|
2019-02-20 06:11:21 +00:00
|
|
|
if err != nil {
|
2019-07-22 18:10:18 +00:00
|
|
|
return nil, err
|
2019-02-20 06:11:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractMessage(data)
|
2019-02-20 06:11:21 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 14:29:44 +00:00
|
|
|
// Delete removes the message, including service messages,
|
|
|
|
// with the following limitations:
|
|
|
|
//
|
2017-11-21 03:08:06 +00:00
|
|
|
// * A message can only be deleted if it was sent less than 48 hours ago.
|
|
|
|
// * Bots can delete outgoing messages in groups and supergroups.
|
|
|
|
// * Bots granted can_post_messages permissions can delete outgoing
|
|
|
|
// messages in channels.
|
|
|
|
// * If the bot is an administrator of a group, it can delete any message there.
|
|
|
|
// * If the bot has can_delete_messages permission in a supergroup or a
|
|
|
|
// channel, it can delete any message there.
|
|
|
|
//
|
2017-11-19 01:44:31 +00:00
|
|
|
func (b *Bot) Delete(message Editable) error {
|
|
|
|
messageID, chatID := message.MessageSig()
|
|
|
|
|
2017-11-17 07:22:16 +00:00
|
|
|
params := map[string]string{
|
2017-11-19 01:44:31 +00:00
|
|
|
"chat_id": strconv.FormatInt(chatID, 10),
|
2017-12-26 22:39:15 +00:00
|
|
|
"message_id": messageID,
|
2017-11-17 07:22:16 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("deleteMessage", params)
|
2017-11-17 07:22:16 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractOk(data)
|
2017-11-17 07:22:16 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 14:08:23 +00:00
|
|
|
// Notify updates the chat action for recipient.
|
2015-07-06 16:13:08 +00:00
|
|
|
//
|
|
|
|
// Chat action is a status message that recipient would see where
|
|
|
|
// you typically see "Harry is typing" status message. The only
|
|
|
|
// difference is that bots' chat actions live only for 5 seconds
|
2020-03-29 12:59:27 +00:00
|
|
|
// and die just once the client receives a message from the bot.
|
2015-07-06 16:13:08 +00:00
|
|
|
//
|
|
|
|
// Currently, Telegram supports only a narrow range of possible
|
|
|
|
// actions, these are aligned as constants of this package.
|
2017-11-24 14:08:23 +00:00
|
|
|
func (b *Bot) Notify(recipient Recipient, action ChatAction) error {
|
2016-06-26 14:05:37 +00:00
|
|
|
params := map[string]string{
|
2017-11-18 14:41:23 +00:00
|
|
|
"chat_id": recipient.Recipient(),
|
2016-11-10 20:04:50 +00:00
|
|
|
"action": string(action),
|
2016-06-26 14:05:37 +00:00
|
|
|
}
|
2015-07-06 16:13:08 +00:00
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("sendChatAction", params)
|
2016-01-22 11:38:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractOk(data)
|
2015-07-06 16:13:08 +00:00
|
|
|
}
|
2016-06-26 14:05:37 +00:00
|
|
|
|
2019-01-28 00:11:10 +00:00
|
|
|
// Accept finalizes the deal.
|
|
|
|
func (b *Bot) Accept(query *PreCheckoutQuery, errorMessage ...string) error {
|
|
|
|
params := map[string]string{
|
|
|
|
"pre_checkout_query_id": query.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(errorMessage) == 0 {
|
|
|
|
params["ok"] = "True"
|
|
|
|
} else {
|
|
|
|
params["ok"] = "False"
|
|
|
|
params["error_message"] = errorMessage[0]
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("answerPreCheckoutQuery", params)
|
2019-01-28 00:11:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractOk(data)
|
2019-01-28 00:11:10 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 14:08:23 +00:00
|
|
|
// Answer sends a response for a given inline query. A query can only
|
|
|
|
// be responded to once, subsequent attempts to respond to the same query
|
2016-06-26 14:05:37 +00:00
|
|
|
// will result in an error.
|
2017-11-24 14:08:23 +00:00
|
|
|
func (b *Bot) Answer(query *Query, response *QueryResponse) error {
|
2017-11-29 20:14:25 +00:00
|
|
|
response.QueryID = query.ID
|
|
|
|
|
2017-12-26 22:39:15 +00:00
|
|
|
for _, result := range response.Results {
|
|
|
|
result.Process()
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("answerInlineQuery", response)
|
2016-06-26 14:05:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractOk(data)
|
2016-06-26 14:05:37 +00:00
|
|
|
}
|
2016-07-25 22:09:18 +00:00
|
|
|
|
2017-11-24 14:08:23 +00:00
|
|
|
// Respond sends a response for a given callback query. A callback can
|
2016-07-25 22:09:18 +00:00
|
|
|
// only be responded to once, subsequent attempts to respond to the same callback
|
|
|
|
// will result in an error.
|
2017-12-10 22:32:08 +00:00
|
|
|
//
|
|
|
|
// Example:
|
|
|
|
//
|
|
|
|
// bot.Respond(c)
|
|
|
|
// bot.Respond(c, response)
|
|
|
|
//
|
|
|
|
func (b *Bot) Respond(callback *Callback, responseOptional ...*CallbackResponse) error {
|
|
|
|
var response *CallbackResponse
|
|
|
|
if responseOptional == nil {
|
|
|
|
response = &CallbackResponse{}
|
|
|
|
} else {
|
|
|
|
response = responseOptional[0]
|
|
|
|
}
|
2016-07-25 22:09:18 +00:00
|
|
|
|
2017-12-10 22:32:08 +00:00
|
|
|
response.CallbackID = callback.ID
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("answerCallbackQuery", response)
|
2016-07-25 22:09:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractOk(data)
|
2016-07-25 22:09:18 +00:00
|
|
|
}
|
2016-09-27 12:04:13 +00:00
|
|
|
|
2017-11-19 15:16:39 +00:00
|
|
|
// FileByID returns full file object including File.FilePath, allowing you to
|
|
|
|
// download the file from the server.
|
2016-09-27 12:04:13 +00:00
|
|
|
//
|
2017-11-19 15:16:39 +00:00
|
|
|
// Usually, Telegram-provided File objects miss FilePath so you might need to
|
|
|
|
// perform an additional request to fetch them.
|
|
|
|
func (b *Bot) FileByID(fileID string) (File, error) {
|
2016-09-27 12:04:13 +00:00
|
|
|
params := map[string]string{
|
2016-10-09 19:57:08 +00:00
|
|
|
"file_id": fileID,
|
2016-09-27 12:04:13 +00:00
|
|
|
}
|
2017-11-18 10:19:58 +00:00
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("getFile", params)
|
2016-09-27 12:04:13 +00:00
|
|
|
if err != nil {
|
2017-11-19 15:16:39 +00:00
|
|
|
return File{}, err
|
2016-09-27 12:04:13 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 10:19:58 +00:00
|
|
|
var resp struct {
|
2016-09-27 12:04:13 +00:00
|
|
|
Ok bool
|
|
|
|
Description string
|
2017-11-19 15:16:39 +00:00
|
|
|
Result File
|
2016-09-27 12:04:13 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
err = json.Unmarshal(data, &resp)
|
2016-09-27 12:04:13 +00:00
|
|
|
if err != nil {
|
2017-11-19 15:16:39 +00:00
|
|
|
return File{}, errors.Wrap(err, "bad response json")
|
2016-09-27 12:04:13 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 10:19:58 +00:00
|
|
|
if !resp.Ok {
|
2017-11-19 15:16:39 +00:00
|
|
|
return File{}, errors.Errorf("api error: %s", resp.Description)
|
2017-08-15 13:44:01 +00:00
|
|
|
|
2016-09-27 12:04:13 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 10:19:58 +00:00
|
|
|
return resp.Result, nil
|
2016-09-27 12:04:13 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 15:58:40 +00:00
|
|
|
// Download saves the file from Telegram servers locally.
|
|
|
|
//
|
|
|
|
// Maximum file size to download is 20 MB.
|
2019-03-03 05:14:14 +00:00
|
|
|
func (b *Bot) Download(file *File, localFilename string) error {
|
|
|
|
reader, err := b.GetFile(file)
|
2017-11-24 15:58:40 +00:00
|
|
|
if err != nil {
|
2020-04-05 17:23:51 +00:00
|
|
|
return wrapError(err)
|
2017-11-24 15:58:40 +00:00
|
|
|
}
|
2019-03-03 05:14:14 +00:00
|
|
|
defer reader.Close()
|
2017-11-24 15:58:40 +00:00
|
|
|
|
|
|
|
out, err := os.Create(localFilename)
|
|
|
|
if err != nil {
|
2020-04-05 17:23:51 +00:00
|
|
|
return wrapError(err)
|
2017-11-24 15:58:40 +00:00
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
2019-03-03 05:14:14 +00:00
|
|
|
_, err = io.Copy(out, reader)
|
2017-11-24 15:58:40 +00:00
|
|
|
if err != nil {
|
2020-04-05 17:23:51 +00:00
|
|
|
return wrapError(err)
|
2017-11-24 15:58:40 +00:00
|
|
|
}
|
2019-03-03 05:14:14 +00:00
|
|
|
file.FileLocal = localFilename
|
2017-11-24 15:58:40 +00:00
|
|
|
|
2019-03-03 05:14:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFile from Telegram servers
|
|
|
|
func (b *Bot) GetFile(file *File) (io.ReadCloser, error) {
|
|
|
|
f, err := b.FileByID(file.FileID)
|
2017-11-24 15:58:40 +00:00
|
|
|
if err != nil {
|
2019-03-03 05:14:14 +00:00
|
|
|
return nil, err
|
2017-11-24 15:58:40 +00:00
|
|
|
}
|
2020-03-29 12:59:27 +00:00
|
|
|
// save FilePath
|
|
|
|
file.FilePath = f.FilePath
|
2017-11-24 15:58:40 +00:00
|
|
|
|
2020-03-29 12:59:27 +00:00
|
|
|
req, err := http.NewRequest("GET", b.URL+"/file/bot"+b.Token+"/"+f.FilePath, nil)
|
|
|
|
if err != nil {
|
2020-04-05 17:23:51 +00:00
|
|
|
return nil, wrapError(err)
|
2020-03-29 12:59:27 +00:00
|
|
|
}
|
2017-11-24 15:58:40 +00:00
|
|
|
|
2020-03-29 12:59:27 +00:00
|
|
|
resp, err := b.client.Do(req)
|
2019-03-03 05:14:14 +00:00
|
|
|
if err != nil {
|
2020-03-29 12:59:27 +00:00
|
|
|
return nil, errors.Wrap(err, "file http.GET failed")
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil, errors.Errorf("api error: expected 200 OK but got %s", resp.Status)
|
2019-03-03 05:14:14 +00:00
|
|
|
}
|
2017-11-24 15:58:40 +00:00
|
|
|
|
2019-03-03 05:14:14 +00:00
|
|
|
return resp.Body, nil
|
|
|
|
}
|
2019-07-22 18:10:18 +00:00
|
|
|
|
2017-11-26 09:15:11 +00:00
|
|
|
// StopLiveLocation should be called to stop broadcasting live message location
|
|
|
|
// before Location.LivePeriod expires.
|
|
|
|
//
|
|
|
|
// It supports telebot.ReplyMarkup.
|
|
|
|
func (b *Bot) StopLiveLocation(message Editable, options ...interface{}) (*Message, error) {
|
|
|
|
messageID, chatID := message.MessageSig()
|
|
|
|
|
|
|
|
params := map[string]string{
|
2020-03-29 12:59:27 +00:00
|
|
|
"chat_id": strconv.FormatInt(chatID, 10),
|
2018-06-30 20:34:04 +00:00
|
|
|
"message_id": messageID,
|
2017-11-26 09:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sendOpts := extractOptions(options)
|
|
|
|
embedSendOptions(params, sendOpts)
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("stopMessageLiveLocation", params)
|
2017-11-26 09:15:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractMessage(data)
|
2017-11-26 09:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetInviteLink should be used to export chat's invite link.
|
|
|
|
func (b *Bot) GetInviteLink(chat *Chat) (string, error) {
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": chat.Recipient(),
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("exportChatInviteLink", params)
|
2017-11-26 09:15:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp struct {
|
|
|
|
Ok bool
|
|
|
|
Description string
|
|
|
|
Result string
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
err = json.Unmarshal(data, &resp)
|
2017-11-26 09:15:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "bad response json")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !resp.Ok {
|
|
|
|
return "", errors.Errorf("api error: %s", resp.Description)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Result, nil
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:59:27 +00:00
|
|
|
// SetGroupTitle should be used to update group title.
|
2017-11-26 09:15:11 +00:00
|
|
|
func (b *Bot) SetGroupTitle(chat *Chat, newTitle string) error {
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": chat.Recipient(),
|
|
|
|
"title": newTitle,
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("setChatTitle", params)
|
2017-11-26 09:15:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractOk(data)
|
2017-11-26 09:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetGroupDescription should be used to update group title.
|
|
|
|
func (b *Bot) SetGroupDescription(chat *Chat, description string) error {
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": chat.Recipient(),
|
|
|
|
"description": description,
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("setChatDescription", params)
|
2017-11-26 09:15:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractOk(data)
|
2017-11-26 09:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetGroupPhoto should be used to update group photo.
|
|
|
|
func (b *Bot) SetGroupPhoto(chat *Chat, p *Photo) error {
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": chat.Recipient(),
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.sendFiles("setChatPhoto", map[string]File{"photo": p.File}, params)
|
2017-11-26 09:15:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractOk(data)
|
2017-11-26 09:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetGroupStickerSet should be used to update group's group sticker set.
|
|
|
|
func (b *Bot) SetGroupStickerSet(chat *Chat, setName string) error {
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": chat.Recipient(),
|
|
|
|
"sticker_set_name": setName,
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("setChatStickerSet", params)
|
2017-11-26 09:15:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractOk(data)
|
2017-11-26 09:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteGroupPhoto should be used to just remove group photo.
|
|
|
|
func (b *Bot) DeleteGroupPhoto(chat *Chat) error {
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": chat.Recipient(),
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("deleteGroupPhoto", params)
|
2017-11-26 09:15:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractOk(data)
|
2017-11-26 09:15:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteGroupStickerSet should be used to just remove group sticker set.
|
|
|
|
func (b *Bot) DeleteGroupStickerSet(chat *Chat) error {
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": chat.Recipient(),
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("deleteChatStickerSet", params)
|
2017-11-26 09:15:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractOk(data)
|
2017-11-26 09:15:11 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 15:28:22 +00:00
|
|
|
// Leave makes bot leave a group, supergroup or channel.
|
|
|
|
func (b *Bot) Leave(chat *Chat) error {
|
2016-10-09 19:57:08 +00:00
|
|
|
params := map[string]string{
|
2017-11-21 15:28:22 +00:00
|
|
|
"chat_id": chat.Recipient(),
|
2016-10-09 19:57:08 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("leaveChat", params)
|
2016-10-09 19:57:08 +00:00
|
|
|
if err != nil {
|
2017-11-18 10:19:58 +00:00
|
|
|
return err
|
2016-10-09 20:00:04 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractOk(data)
|
2016-10-09 20:00:04 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 00:44:32 +00:00
|
|
|
// Use this method to pin a message in a supergroup or a channel.
|
2016-11-09 23:28:00 +00:00
|
|
|
//
|
2017-11-26 00:44:32 +00:00
|
|
|
// It supports telebot.Silent option.
|
|
|
|
func (b *Bot) Pin(message Editable, options ...interface{}) error {
|
|
|
|
messageID, chatID := message.MessageSig()
|
2017-11-18 10:19:58 +00:00
|
|
|
|
2017-11-26 00:44:32 +00:00
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": strconv.FormatInt(chatID, 10),
|
2017-12-26 22:39:15 +00:00
|
|
|
"message_id": messageID,
|
2016-10-09 20:00:04 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 00:44:32 +00:00
|
|
|
sendOpts := extractOptions(options)
|
2017-11-26 07:57:58 +00:00
|
|
|
embedSendOptions(params, sendOpts)
|
2016-10-09 20:00:04 +00:00
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("pinChatMessage", params)
|
2016-10-09 20:00:04 +00:00
|
|
|
if err != nil {
|
2017-11-26 00:44:32 +00:00
|
|
|
return err
|
2016-10-09 19:57:08 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractOk(data)
|
2016-10-09 19:57:08 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 00:44:32 +00:00
|
|
|
// Use this method to unpin a message in a supergroup or a channel.
|
2016-10-09 20:09:07 +00:00
|
|
|
//
|
2017-11-26 00:44:32 +00:00
|
|
|
// It supports telebot.Silent option.
|
|
|
|
func (b *Bot) Unpin(chat *Chat) error {
|
2016-10-09 20:09:07 +00:00
|
|
|
params := map[string]string{
|
2017-11-19 15:16:39 +00:00
|
|
|
"chat_id": chat.Recipient(),
|
2016-10-09 20:09:07 +00:00
|
|
|
}
|
2017-11-18 10:19:58 +00:00
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("unpinChatMessage", params)
|
2016-10-09 20:09:07 +00:00
|
|
|
if err != nil {
|
2017-11-26 00:44:32 +00:00
|
|
|
return err
|
2016-10-09 20:09:07 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractOk(data)
|
2016-10-09 20:09:07 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 00:44:32 +00:00
|
|
|
// ChatByID fetches chat info of its ID.
|
|
|
|
//
|
|
|
|
// Including current name of the user for one-on-one conversations,
|
|
|
|
// current username of a user, group or channel, etc.
|
|
|
|
//
|
|
|
|
// Returns a Chat object on success.
|
|
|
|
func (b *Bot) ChatByID(id string) (*Chat, error) {
|
2016-10-09 20:11:23 +00:00
|
|
|
params := map[string]string{
|
2017-11-26 00:44:32 +00:00
|
|
|
"chat_id": id,
|
2016-10-09 20:11:23 +00:00
|
|
|
}
|
2017-11-18 10:19:58 +00:00
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("getChat", params)
|
2016-10-09 20:11:23 +00:00
|
|
|
if err != nil {
|
2017-11-26 00:44:32 +00:00
|
|
|
return nil, err
|
2016-10-09 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 10:19:58 +00:00
|
|
|
var resp struct {
|
2016-10-09 20:11:23 +00:00
|
|
|
Ok bool
|
2017-11-26 00:44:32 +00:00
|
|
|
Description string
|
|
|
|
Result *Chat
|
2016-10-09 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
err = json.Unmarshal(data, &resp)
|
2016-10-09 20:11:23 +00:00
|
|
|
if err != nil {
|
2017-11-26 00:44:32 +00:00
|
|
|
return nil, errors.Wrap(err, "bad response json")
|
2016-10-09 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 10:19:58 +00:00
|
|
|
if !resp.Ok {
|
2017-11-26 00:44:32 +00:00
|
|
|
return nil, errors.Errorf("api error: %s", resp.Description)
|
2016-10-09 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
2018-05-19 16:22:14 +00:00
|
|
|
if resp.Result.Type == ChatChannel && resp.Result.Username == "" {
|
2020-04-05 17:23:51 +00:00
|
|
|
// Channel is Private
|
2017-12-30 12:08:44 +00:00
|
|
|
resp.Result.Type = ChatChannelPrivate
|
|
|
|
}
|
|
|
|
|
2017-11-18 10:19:58 +00:00
|
|
|
return resp.Result, nil
|
2016-10-09 20:11:23 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 15:16:39 +00:00
|
|
|
// ProfilePhotosOf return list of profile pictures for a user.
|
|
|
|
func (b *Bot) ProfilePhotosOf(user *User) ([]Photo, error) {
|
2016-10-09 20:29:03 +00:00
|
|
|
params := map[string]string{
|
2017-11-19 15:16:39 +00:00
|
|
|
"user_id": user.Recipient(),
|
2016-10-09 20:29:03 +00:00
|
|
|
}
|
2017-11-18 10:19:58 +00:00
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("getUserProfilePhotos", params)
|
2016-10-09 20:29:03 +00:00
|
|
|
if err != nil {
|
2017-11-18 10:19:58 +00:00
|
|
|
return nil, err
|
2016-10-09 20:29:03 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 10:19:58 +00:00
|
|
|
var resp struct {
|
|
|
|
Ok bool
|
|
|
|
Result struct {
|
2017-11-19 15:16:39 +00:00
|
|
|
Count int `json:"total_count"`
|
|
|
|
Photos []Photo `json:"photos"`
|
2017-11-18 10:19:58 +00:00
|
|
|
}
|
|
|
|
|
2016-11-10 19:34:02 +00:00
|
|
|
Description string `json:"description"`
|
2016-10-09 20:29:03 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
err = json.Unmarshal(data, &resp)
|
2016-10-09 20:29:03 +00:00
|
|
|
if err != nil {
|
2017-11-18 10:19:58 +00:00
|
|
|
return nil, errors.Wrap(err, "bad response json")
|
2016-10-09 20:29:03 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 10:19:58 +00:00
|
|
|
if !resp.Ok {
|
|
|
|
return nil, errors.Errorf("api error: %s", resp.Description)
|
2016-10-09 20:29:03 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 10:19:58 +00:00
|
|
|
return resp.Result.Photos, nil
|
2016-10-09 20:29:03 +00:00
|
|
|
}
|
|
|
|
|
2017-11-20 23:41:39 +00:00
|
|
|
// ChatMemberOf return information about a member of a chat.
|
2016-10-09 20:16:33 +00:00
|
|
|
//
|
|
|
|
// Returns a ChatMember object on success.
|
2017-11-21 15:28:22 +00:00
|
|
|
func (b *Bot) ChatMemberOf(chat *Chat, user *User) (*ChatMember, error) {
|
2016-10-09 20:16:33 +00:00
|
|
|
params := map[string]string{
|
2017-11-19 15:16:39 +00:00
|
|
|
"chat_id": chat.Recipient(),
|
2017-11-18 14:41:23 +00:00
|
|
|
"user_id": user.Recipient(),
|
2016-10-09 20:16:33 +00:00
|
|
|
}
|
2017-11-18 10:19:58 +00:00
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("getChatMember", params)
|
2016-10-09 20:16:33 +00:00
|
|
|
if err != nil {
|
2017-11-21 15:28:22 +00:00
|
|
|
return nil, err
|
2016-10-09 20:16:33 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 10:19:58 +00:00
|
|
|
var resp struct {
|
2016-10-09 20:16:33 +00:00
|
|
|
Ok bool
|
2017-11-21 15:28:22 +00:00
|
|
|
Result *ChatMember
|
2016-11-10 19:34:02 +00:00
|
|
|
Description string `json:"description"`
|
2016-10-09 20:16:33 +00:00
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
err = json.Unmarshal(data, &resp)
|
2016-10-09 20:16:33 +00:00
|
|
|
if err != nil {
|
2017-11-21 15:28:22 +00:00
|
|
|
return nil, errors.Wrap(err, "bad response json")
|
2016-10-09 20:16:33 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 10:19:58 +00:00
|
|
|
if !resp.Ok {
|
2017-11-21 15:28:22 +00:00
|
|
|
return nil, errors.Errorf("api error: %s", resp.Description)
|
2016-10-09 20:16:33 +00:00
|
|
|
}
|
|
|
|
|
2017-11-18 10:19:58 +00:00
|
|
|
return resp.Result, nil
|
2016-10-09 20:16:33 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 03:08:06 +00:00
|
|
|
// FileURLByID returns direct url for files using FileId which you can get from File object
|
2017-11-20 23:41:39 +00:00
|
|
|
func (b *Bot) FileURLByID(fileID string) (string, error) {
|
2017-11-19 15:16:39 +00:00
|
|
|
f, err := b.FileByID(fileID)
|
2016-09-27 12:04:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2020-03-29 12:59:27 +00:00
|
|
|
return b.URL + "/file/bot" + b.Token + "/" + f.FilePath, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UploadStickerFile returns uploaded File on success.
|
|
|
|
func (b *Bot) UploadStickerFile(userID int, pngSticker *File) (*File, error) {
|
|
|
|
files := map[string]File{
|
|
|
|
"png_sticker": *pngSticker,
|
|
|
|
}
|
|
|
|
params := map[string]string{
|
|
|
|
"user_id": strconv.Itoa(userID),
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.sendFiles("uploadStickerFile", files, params)
|
2020-03-29 12:59:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp struct {
|
|
|
|
Ok bool
|
|
|
|
Result File
|
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
err = json.Unmarshal(data, &resp)
|
2020-03-29 12:59:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !resp.Ok {
|
|
|
|
return nil, errors.Errorf("api error: %s", resp.Description)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &resp.Result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetStickerSet returns StickerSet on success.
|
|
|
|
func (b *Bot) GetStickerSet(name string) (*StickerSet, error) {
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("getStickerSet", map[string]string{"name": name})
|
2020-03-29 12:59:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp struct {
|
|
|
|
Ok bool
|
|
|
|
Description string
|
|
|
|
Result *StickerSet
|
|
|
|
}
|
2020-04-06 13:04:25 +00:00
|
|
|
err = json.Unmarshal(data, &resp)
|
2020-03-29 12:59:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !resp.Ok {
|
|
|
|
return nil, errors.Errorf("api error: %s", resp.Description)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateNewStickerSet creates new sticker set.
|
|
|
|
func (b *Bot) CreateNewStickerSet(sp StickerSetParams, containsMasks bool, maskPosition MaskPosition) error {
|
|
|
|
files := map[string]File{
|
|
|
|
"png_sticker": *sp.PngSticker,
|
|
|
|
}
|
|
|
|
params := map[string]string{
|
|
|
|
"user_id": strconv.Itoa(sp.UserID),
|
|
|
|
"name": sp.Name,
|
|
|
|
"title": sp.Title,
|
|
|
|
"emojis": sp.Emojis,
|
|
|
|
}
|
|
|
|
|
|
|
|
if containsMasks {
|
|
|
|
mp, err := json.Marshal(&maskPosition)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
params["mask_position"] = string(mp)
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.sendFiles("createNewStickerSet", files, params)
|
2020-03-29 12:59:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractOk(data)
|
2020-03-29 12:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AddStickerToSet adds new sticker to existing sticker set.
|
|
|
|
func (b *Bot) AddStickerToSet(sp StickerSetParams, maskPosition MaskPosition) error {
|
|
|
|
files := map[string]File{
|
|
|
|
"png_sticker": *sp.PngSticker,
|
|
|
|
}
|
|
|
|
params := map[string]string{
|
|
|
|
"user_id": strconv.Itoa(sp.UserID),
|
|
|
|
"name": sp.Name,
|
|
|
|
"title": sp.Title,
|
|
|
|
"emojis": sp.Emojis,
|
|
|
|
}
|
|
|
|
|
|
|
|
if maskPosition != (MaskPosition{}) {
|
|
|
|
mp, err := json.Marshal(&maskPosition)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
params["mask_position"] = string(mp)
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.sendFiles("addStickerToSet", files, params)
|
2020-03-29 12:59:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractOk(data)
|
2020-03-29 12:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetStickerPositionInSet moves a sticker in set to a specific position.
|
|
|
|
func (b *Bot) SetStickerPositionInSet(sticker string, position int) error {
|
|
|
|
params := map[string]string{
|
|
|
|
"sticker": sticker,
|
|
|
|
"position": strconv.Itoa(position),
|
|
|
|
}
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("setStickerPositionInSet", params)
|
2020-03-29 12:59:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractOk(data)
|
2020-03-29 12:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteStickerFromSet deletes sticker from set created by the bot.
|
|
|
|
func (b *Bot) DeleteStickerFromSet(sticker string) error {
|
2020-04-06 13:04:25 +00:00
|
|
|
data, err := b.Raw("deleteStickerFromSet", map[string]string{"sticker": sticker})
|
2020-03-29 12:59:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-04-06 13:04:25 +00:00
|
|
|
return extractOk(data)
|
2019-07-22 18:10:18 +00:00
|
|
|
}
|