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
|
|
|
|
}
|
2020-08-06 19:14:16 +00:00
|
|
|
if pref.Poller == nil {
|
|
|
|
pref.Poller = &LongPoller{}
|
|
|
|
}
|
2018-12-12 22:45:03 +00:00
|
|
|
|
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
|
|
|
Poller: pref.Poller,
|
2020-07-25 19:44:50 +00:00
|
|
|
OnError: pref.OnError,
|
|
|
|
|
2020-10-01 14:34:29 +00:00
|
|
|
Updates: make(chan Update, pref.Updates),
|
|
|
|
handlers: make(map[string]HandlerFunc),
|
|
|
|
stop: make(chan struct{}),
|
2017-11-21 01:50:44 +00:00
|
|
|
|
2020-04-28 11:53:44 +00:00
|
|
|
synchronous: pref.Synchronous,
|
2020-05-31 12:00:30 +00:00
|
|
|
verbose: pref.Verbose,
|
2020-06-07 20:12:49 +00:00
|
|
|
parseMode: pref.ParseMode,
|
2020-04-28 11:53:44 +00:00
|
|
|
client: client,
|
2015-07-03 18:54:40 +00:00
|
|
|
}
|
|
|
|
|
2021-02-22 17:49:23 +00:00
|
|
|
if pref.Offline {
|
2020-04-28 12:29:23 +00:00
|
|
|
bot.Me = &User{}
|
|
|
|
} else {
|
|
|
|
user, err := bot.getMe()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
bot.Me = user
|
2017-11-18 13:06:20 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 19:13:11 +00:00
|
|
|
bot.group = bot.Group()
|
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
|
2020-07-25 21:00:06 +00:00
|
|
|
OnError func(error, Context)
|
2020-07-25 20:55:01 +00:00
|
|
|
|
2020-08-06 19:13:11 +00:00
|
|
|
group *Group
|
2020-10-01 14:34:29 +00:00
|
|
|
handlers map[string]HandlerFunc
|
2020-04-28 11:53:44 +00:00
|
|
|
synchronous bool
|
2020-05-31 12:00:30 +00:00
|
|
|
verbose bool
|
2020-06-07 20:12:49 +00:00
|
|
|
parseMode ParseMode
|
2020-04-28 11:53:44 +00:00
|
|
|
stop chan struct{}
|
|
|
|
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 {
|
2020-07-25 19:44:50 +00:00
|
|
|
URL string
|
2017-11-20 23:41:39 +00:00
|
|
|
Token string
|
|
|
|
|
2020-07-25 19:44:50 +00:00
|
|
|
// Updates channel capacity, defaulted to 100.
|
|
|
|
Updates int
|
2017-11-20 23:41:39 +00:00
|
|
|
|
|
|
|
// Poller is the provider of Updates.
|
|
|
|
Poller Poller
|
2017-11-27 15:58:41 +00:00
|
|
|
|
2020-04-28 11:53:44 +00:00
|
|
|
// Synchronous prevents handlers from running in parallel.
|
|
|
|
// It makes ProcessUpdate return after the handler is finished.
|
|
|
|
Synchronous bool
|
|
|
|
|
2020-06-09 19:26:28 +00:00
|
|
|
// Verbose forces bot to log all upcoming requests.
|
|
|
|
// Use for debugging purposes only.
|
2020-05-31 12:00:30 +00:00
|
|
|
Verbose bool
|
|
|
|
|
2020-06-07 20:12:49 +00:00
|
|
|
// ParseMode used to set default parse mode of all sent messages.
|
|
|
|
// It attaches to every send, edit or whatever method. You also
|
|
|
|
// will be able to override the default mode by passing a new one.
|
|
|
|
ParseMode ParseMode
|
|
|
|
|
2020-07-25 21:00:06 +00:00
|
|
|
// OnError is a callback function that will get called on errors
|
|
|
|
// resulted from the handler. It is used as post-middleware function.
|
|
|
|
// Notice that context can be nil.
|
|
|
|
OnError func(error, Context)
|
2018-04-18 13:42:18 +00:00
|
|
|
|
|
|
|
// HTTP Client used to make requests to telegram api
|
|
|
|
Client *http.Client
|
2020-04-28 12:29:23 +00:00
|
|
|
|
2021-02-22 17:49:23 +00:00
|
|
|
// Offline allows to create a bot without network for testing purposes.
|
|
|
|
Offline bool
|
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"`
|
|
|
|
|
2021-08-19 14:40:36 +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"`
|
|
|
|
InlineResult *InlineResult `json:"chosen_inline_result,omitempty"`
|
|
|
|
ShippingQuery *ShippingQuery `json:"shipping_query,omitempty"`
|
|
|
|
PreCheckoutQuery *PreCheckoutQuery `json:"pre_checkout_query,omitempty"`
|
|
|
|
Poll *Poll `json:"poll,omitempty"`
|
|
|
|
PollAnswer *PollAnswer `json:"poll_answer,omitempty"`
|
|
|
|
MyChatMember *ChatMemberUpdate `json:"my_chat_member,omitempty"`
|
|
|
|
ChatMember *ChatMemberUpdate `json:"chat_member,omitempty"`
|
2021-11-05 20:31:32 +00:00
|
|
|
ChatJoinRequest *ChatJoinRequest `json:"chat_join_request,omitempty"`
|
2017-12-26 01:07:36 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 12:55:04 +00:00
|
|
|
// Command represents a bot command.
|
|
|
|
type Command struct {
|
2020-05-21 09:02:35 +00:00
|
|
|
// Text is a text of the command, 1-32 characters.
|
2020-04-26 12:55:04 +00:00
|
|
|
// Can contain only lowercase English letters, digits and underscores.
|
|
|
|
Text string `json:"command"`
|
|
|
|
|
|
|
|
// Description of the command, 3-256 characters.
|
|
|
|
Description string `json:"description"`
|
|
|
|
}
|
|
|
|
|
2020-09-15 15:12:43 +00:00
|
|
|
// Group returns a new group.
|
|
|
|
func (b *Bot) Group() *Group {
|
2020-10-01 14:34:29 +00:00
|
|
|
return &Group{b: b}
|
2020-09-15 15:12:43 +00:00
|
|
|
}
|
|
|
|
|
2020-08-06 19:13:11 +00:00
|
|
|
// Use adds middleware to the global bot chain.
|
|
|
|
func (b *Bot) Use(middleware ...MiddlewareFunc) {
|
|
|
|
b.group.Use(middleware...)
|
|
|
|
}
|
|
|
|
|
2017-11-21 02:22:45 +00:00
|
|
|
// Handle lets you set the handler for some command name or
|
2020-07-25 20:55:01 +00:00
|
|
|
// one of the supported endpoints. It also applies middleware
|
|
|
|
// if such passed to the function.
|
2017-11-21 02:22:45 +00:00
|
|
|
//
|
2017-11-21 03:08:06 +00:00
|
|
|
// Example:
|
|
|
|
//
|
2020-07-25 20:55:01 +00:00
|
|
|
// b.Handle("/start", func (c tele.Context) error {
|
|
|
|
// return c.Reply("Hello!")
|
|
|
|
// })
|
|
|
|
//
|
|
|
|
// b.Handle(&inlineButton, func (c tele.Context) error {
|
2020-07-25 21:01:39 +00:00
|
|
|
// return c.Respond(&tele.CallbackResponse{Text: "Hello!"})
|
2020-07-25 20:55:01 +00:00
|
|
|
// })
|
2017-11-21 03:08:06 +00:00
|
|
|
//
|
2020-07-25 20:55:01 +00:00
|
|
|
// Middleware usage:
|
2017-11-26 02:33:28 +00:00
|
|
|
//
|
2020-07-25 20:55:01 +00:00
|
|
|
// protected := telemw.Whitelist(ids...)
|
|
|
|
// b.Handle("/ban", onBan, protected)
|
|
|
|
//
|
|
|
|
func (b *Bot) Handle(endpoint interface{}, h HandlerFunc, m ...MiddlewareFunc) {
|
2020-10-01 14:34:29 +00:00
|
|
|
if len(b.group.middleware) > 0 {
|
|
|
|
m = append(b.group.middleware, m...)
|
|
|
|
}
|
|
|
|
|
|
|
|
handler := func(c Context) error {
|
|
|
|
return applyMiddleware(h, m...)(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch end := endpoint.(type) {
|
|
|
|
case string:
|
|
|
|
b.handlers[end] = handler
|
|
|
|
case CallbackEndpoint:
|
|
|
|
b.handlers[end.CallbackUnique()] = handler
|
|
|
|
default:
|
|
|
|
panic("telebot: unsupported endpoint")
|
|
|
|
}
|
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|$)(.+)?`)
|
2021-02-15 16:16:26 +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:
|
2020-04-28 15:51:38 +00:00
|
|
|
b.ProcessUpdate(upd)
|
2017-11-27 12:52:16 +00:00
|
|
|
// call to stop polling
|
|
|
|
case <-b.stop:
|
2020-05-01 19:14:50 +00:00
|
|
|
close(stop)
|
2017-11-27 12:52:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-21 02:22:45 +00:00
|
|
|
|
2020-04-26 18:29:25 +00:00
|
|
|
// Stop gracefully shuts the poller down.
|
|
|
|
func (b *Bot) Stop() {
|
|
|
|
b.stop <- struct{}{}
|
|
|
|
}
|
|
|
|
|
2020-10-22 13:22:41 +00:00
|
|
|
// NewMarkup simply returns newly created markup instance.
|
|
|
|
func (b *Bot) NewMarkup() *ReplyMarkup {
|
|
|
|
return &ReplyMarkup{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewContext returns a new native context object,
|
|
|
|
// field by the passed update.
|
|
|
|
func (b *Bot) NewContext(upd Update) Context {
|
|
|
|
return &nativeContext{
|
2021-07-25 12:46:42 +00:00
|
|
|
b: b,
|
2021-08-19 14:26:05 +00:00
|
|
|
update: upd,
|
2021-07-25 12:46:42 +00:00
|
|
|
message: upd.Message,
|
|
|
|
callback: upd.Callback,
|
|
|
|
query: upd.Query,
|
|
|
|
inlineResult: upd.InlineResult,
|
|
|
|
shippingQuery: upd.ShippingQuery,
|
|
|
|
preCheckoutQuery: upd.PreCheckoutQuery,
|
|
|
|
poll: upd.Poll,
|
|
|
|
pollAnswer: upd.PollAnswer,
|
2021-07-26 14:37:48 +00:00
|
|
|
myChatMember: upd.MyChatMember,
|
|
|
|
chatMember: upd.ChatMember,
|
2021-11-05 20:31:32 +00:00
|
|
|
chatJoinRequest: upd.ChatJoinRequest,
|
2020-07-25 19:44:50 +00:00
|
|
|
}
|
2020-10-22 13:22:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ProcessUpdate processes a single incoming update.
|
|
|
|
// A started bot calls this function automatically.
|
|
|
|
func (b *Bot) ProcessUpdate(upd Update) {
|
|
|
|
c := b.NewContext(upd).(*nativeContext)
|
2020-07-25 19:44:50 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
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 {
|
2020-07-25 19:44:50 +00:00
|
|
|
b.handle(OnPinned, c)
|
2017-11-27 12:52:16 +00:00
|
|
|
return
|
|
|
|
}
|
2017-11-23 02:13:15 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
// Commands
|
|
|
|
if m.Text != "" {
|
2020-05-21 09:02:35 +00:00
|
|
|
// Filtering malicious messages
|
2017-11-27 12:52:16 +00:00
|
|
|
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>"
|
2020-04-06 19:27:58 +00:00
|
|
|
command, botName := match[0][1], match[0][3]
|
2021-11-07 16:28:21 +00:00
|
|
|
|
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]
|
2020-07-25 19:44:50 +00:00
|
|
|
if b.handle(command, c) {
|
2017-11-27 15:58:41 +00:00
|
|
|
return
|
2017-11-27 14:56:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 15:58:41 +00:00
|
|
|
// 1:1 satisfaction
|
2020-07-25 19:44:50 +00:00
|
|
|
if b.handle(m.Text, c) {
|
2017-11-27 15:58:41 +00:00
|
|
|
return
|
2017-11-26 01:26:07 +00:00
|
|
|
}
|
|
|
|
|
2020-07-25 19:44:50 +00:00
|
|
|
b.handle(OnText, c)
|
2017-11-27 12:52:16 +00:00
|
|
|
return
|
|
|
|
}
|
2017-11-26 01:26:07 +00:00
|
|
|
|
2020-07-25 19:44:50 +00:00
|
|
|
if b.handleMedia(c) {
|
2017-11-27 12:52:16 +00:00
|
|
|
return
|
|
|
|
}
|
2017-11-26 01:26:07 +00:00
|
|
|
|
2021-11-07 16:28:21 +00:00
|
|
|
if m.Contact != nil {
|
|
|
|
b.handle(OnContact, c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if m.Location != nil {
|
|
|
|
b.handle(OnLocation, c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if m.Venue != nil {
|
|
|
|
b.handle(OnVenue, c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if m.Game != nil {
|
|
|
|
b.handle(OnGame, c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if m.Dice != nil {
|
|
|
|
b.handle(OnDice, c)
|
|
|
|
return
|
|
|
|
}
|
2020-05-21 08:48:20 +00:00
|
|
|
if m.Invoice != nil {
|
2020-07-25 19:44:50 +00:00
|
|
|
b.handle(OnInvoice, c)
|
2020-05-21 08:48:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if m.Payment != nil {
|
2020-07-25 19:44:50 +00:00
|
|
|
b.handle(OnPayment, c)
|
2020-05-21 08:48:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-07-25 19:44:50 +00:00
|
|
|
b.handle(OnAddedToGroup, c)
|
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.UserJoined != nil {
|
2020-07-25 19:44:50 +00:00
|
|
|
b.handle(OnUserJoined, c)
|
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.UsersJoined != nil {
|
|
|
|
for _, user := range m.UsersJoined {
|
|
|
|
m.UserJoined = &user
|
2020-07-25 19:44:50 +00:00
|
|
|
b.handle(OnUserJoined, c)
|
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 {
|
2020-07-25 19:44:50 +00:00
|
|
|
b.handle(OnUserLeft, c)
|
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.NewGroupTitle != "" {
|
2020-07-25 19:44:50 +00:00
|
|
|
b.handle(OnNewGroupTitle, c)
|
2017-11-27 12:52:16 +00:00
|
|
|
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 {
|
2020-07-25 19:44:50 +00:00
|
|
|
b.handle(OnNewGroupPhoto, c)
|
2017-11-27 12:52:16 +00:00
|
|
|
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 {
|
2020-07-25 19:44:50 +00:00
|
|
|
b.handle(OnGroupPhotoDeleted, c)
|
2017-11-27 12:52:16 +00:00
|
|
|
return
|
2017-11-21 00:00:58 +00:00
|
|
|
}
|
|
|
|
|
2021-08-19 13:40:07 +00:00
|
|
|
if m.GroupCreated {
|
|
|
|
b.handle(OnGroupCreated, c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.SuperGroupCreated {
|
|
|
|
b.handle(OnSuperGroupCreated, c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.ChannelCreated {
|
|
|
|
b.handle(OnChannelCreated, c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
if m.MigrateTo != 0 {
|
2020-07-25 19:44:50 +00:00
|
|
|
m.MigrateFrom = m.Chat.ID
|
|
|
|
b.handle(OnMigration, c)
|
2017-11-27 12:52:16 +00:00
|
|
|
return
|
2017-11-21 00:00:58 +00:00
|
|
|
}
|
2021-03-27 09:03:04 +00:00
|
|
|
|
|
|
|
if m.VoiceChatStarted != nil {
|
|
|
|
b.handle(OnVoiceChatStarted, c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.VoiceChatEnded != nil {
|
|
|
|
b.handle(OnVoiceChatEnded, c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.VoiceChatParticipants != nil {
|
|
|
|
b.handle(OnVoiceChatParticipants, c)
|
|
|
|
return
|
|
|
|
}
|
2021-05-23 16:55:49 +00:00
|
|
|
|
2021-07-25 12:46:42 +00:00
|
|
|
if m.VoiceChatScheduled != nil {
|
|
|
|
b.handle(OnVoiceChatScheduled, c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-05-23 16:55:49 +00:00
|
|
|
if m.ProximityAlert != nil {
|
|
|
|
b.handle(OnProximityAlert, c)
|
|
|
|
return
|
|
|
|
}
|
2021-05-23 19:13:39 +00:00
|
|
|
|
|
|
|
if m.AutoDeleteTimer != nil {
|
|
|
|
b.handle(OnAutoDeleteTimer, c)
|
|
|
|
return
|
|
|
|
}
|
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 {
|
2020-07-25 19:44:50 +00:00
|
|
|
c.message = upd.EditedMessage
|
|
|
|
b.handle(OnEdited, c)
|
2017-11-27 12:52:16 +00:00
|
|
|
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 {
|
2020-07-25 19:44:50 +00:00
|
|
|
c.message = m.PinnedMessage
|
|
|
|
b.handle(OnPinned, c)
|
2020-04-06 11:22:01 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-25 19:44:50 +00:00
|
|
|
c.message = upd.ChannelPost
|
|
|
|
b.handle(OnChannelPost, c)
|
2017-11-27 12:52:16 +00:00
|
|
|
return
|
|
|
|
}
|
2017-11-26 02:33:28 +00:00
|
|
|
|
2017-11-27 12:52:16 +00:00
|
|
|
if upd.EditedChannelPost != nil {
|
2020-07-25 19:44:50 +00:00
|
|
|
c.message = upd.EditedChannelPost
|
|
|
|
b.handle(OnEditedChannelPost, c)
|
2017-11-27 12:52:16 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if upd.Callback != nil {
|
|
|
|
if upd.Callback.Data != "" {
|
2021-07-25 13:07:48 +00:00
|
|
|
if data := upd.Callback.Data; data[0] == '\f' {
|
2017-11-27 12:52:16 +00:00
|
|
|
match := cbackRx.FindAllStringSubmatch(data, -1)
|
|
|
|
if match != nil {
|
2017-12-10 22:32:08 +00:00
|
|
|
unique, payload := match[0][1], match[0][3]
|
2020-10-01 14:34:29 +00:00
|
|
|
if handler, ok := b.handlers["\f"+unique]; ok {
|
2020-10-12 18:01:34 +00:00
|
|
|
c.callback.Unique = unique
|
2020-10-09 21:31:19 +00:00
|
|
|
c.callback.Data = payload
|
2020-07-25 19:44:50 +00:00
|
|
|
b.runHandler(handler, c)
|
2020-04-06 19:27:58 +00:00
|
|
|
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
|
|
|
|
2020-07-25 19:44:50 +00:00
|
|
|
b.handle(OnCallback, c)
|
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 {
|
2020-07-25 19:44:50 +00:00
|
|
|
b.handle(OnQuery, c)
|
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
|
|
|
|
2021-07-25 12:46:42 +00:00
|
|
|
if upd.InlineResult != nil {
|
|
|
|
b.handle(OnInlineResult, c)
|
2017-12-26 01:07:36 +00:00
|
|
|
return
|
|
|
|
}
|
2019-01-28 00:11:10 +00:00
|
|
|
|
2020-05-20 20:40:42 +00:00
|
|
|
if upd.ShippingQuery != nil {
|
2020-07-25 19:44:50 +00:00
|
|
|
b.handle(OnShipping, c)
|
2020-05-20 20:40:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-28 00:11:10 +00:00
|
|
|
if upd.PreCheckoutQuery != nil {
|
2020-07-25 19:44:50 +00:00
|
|
|
b.handle(OnCheckout, c)
|
2019-01-28 00:11:10 +00:00
|
|
|
return
|
|
|
|
}
|
2020-03-30 16:21:06 +00:00
|
|
|
|
|
|
|
if upd.Poll != nil {
|
2020-07-25 19:44:50 +00:00
|
|
|
b.handle(OnPoll, c)
|
2020-03-30 16:21:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if upd.PollAnswer != nil {
|
2020-07-25 19:44:50 +00:00
|
|
|
b.handle(OnPollAnswer, c)
|
2020-03-30 16:21:06 +00:00
|
|
|
return
|
|
|
|
}
|
2021-03-30 11:16:11 +00:00
|
|
|
|
|
|
|
if upd.MyChatMember != nil {
|
|
|
|
b.handle(OnMyChatMember, c)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if upd.ChatMember != nil {
|
|
|
|
b.handle(OnChatMember, c)
|
|
|
|
return
|
|
|
|
}
|
2021-11-05 20:31:32 +00:00
|
|
|
|
|
|
|
if upd.ChatJoinRequest != nil {
|
|
|
|
b.handle(OnChatJoinRequest, c)
|
|
|
|
return
|
|
|
|
}
|
2017-11-21 00:00:58 +00:00
|
|
|
}
|
|
|
|
|
2020-07-25 19:44:50 +00:00
|
|
|
func (b *Bot) handle(end string, c Context) bool {
|
2020-10-01 14:34:29 +00:00
|
|
|
if handler, ok := b.handlers[end]; ok {
|
2020-07-25 19:44:50 +00:00
|
|
|
b.runHandler(handler, c)
|
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
|
|
|
}
|
|
|
|
|
2020-07-25 19:44:50 +00:00
|
|
|
func (b *Bot) handleMedia(c Context) bool {
|
2021-11-07 14:25:21 +00:00
|
|
|
var (
|
|
|
|
m = c.Message()
|
|
|
|
fired = true
|
|
|
|
)
|
2020-07-25 19:44:50 +00:00
|
|
|
|
2020-04-06 19:27:58 +00:00
|
|
|
switch {
|
|
|
|
case m.Photo != nil:
|
2021-11-07 14:25:21 +00:00
|
|
|
fired = b.handle(OnPhoto, c)
|
2020-04-06 19:27:58 +00:00
|
|
|
case m.Voice != nil:
|
2021-11-07 14:25:21 +00:00
|
|
|
fired = b.handle(OnVoice, c)
|
2020-04-06 19:27:58 +00:00
|
|
|
case m.Audio != nil:
|
2021-11-07 14:25:21 +00:00
|
|
|
fired = b.handle(OnAudio, c)
|
2020-04-29 13:02:52 +00:00
|
|
|
case m.Animation != nil:
|
2021-11-07 14:25:21 +00:00
|
|
|
fired = b.handle(OnAnimation, c)
|
2020-04-06 19:27:58 +00:00
|
|
|
case m.Document != nil:
|
2021-11-07 14:25:21 +00:00
|
|
|
fired = b.handle(OnDocument, c)
|
2020-04-06 19:27:58 +00:00
|
|
|
case m.Sticker != nil:
|
2021-11-07 14:25:21 +00:00
|
|
|
fired = b.handle(OnSticker, c)
|
2020-04-06 19:27:58 +00:00
|
|
|
case m.Video != nil:
|
2021-11-07 14:25:21 +00:00
|
|
|
fired = b.handle(OnVideo, c)
|
2020-04-06 19:27:58 +00:00
|
|
|
case m.VideoNote != nil:
|
2021-11-07 14:25:21 +00:00
|
|
|
fired = b.handle(OnVideoNote, c)
|
2020-04-06 19:27:58 +00:00
|
|
|
default:
|
|
|
|
return false
|
2017-11-24 15:24:07 +00:00
|
|
|
}
|
2021-11-07 14:25:21 +00:00
|
|
|
|
|
|
|
if !fired {
|
|
|
|
return b.handle(OnMedia, c)
|
|
|
|
}
|
|
|
|
|
2020-04-06 19:27:58 +00:00
|
|
|
return true
|
2017-11-24 15:24:07 +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.
|
|
|
|
//
|
2020-06-19 20:34:37 +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)
|
2020-05-21 09:02:35 +00:00
|
|
|
// - Option (a shortcut flag for popular options)
|
2017-11-21 03:08:06 +00:00
|
|
|
// - ParseMode (HTML, Markdown, etc)
|
2017-11-17 14:29:44 +00:00
|
|
|
//
|
2020-06-19 20:34:37 +00:00
|
|
|
func (b *Bot) Send(to Recipient, what interface{}, opts ...interface{}) (*Message, error) {
|
2020-04-16 14:57:16 +00:00
|
|
|
if to == nil {
|
|
|
|
return nil, ErrBadRecipient
|
|
|
|
}
|
|
|
|
|
2020-06-19 20:34:37 +00:00
|
|
|
sendOpts := extractOptions(opts)
|
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:
|
2020-04-16 15:56:47 +00:00
|
|
|
return nil, ErrUnsupportedWhat
|
2015-07-03 18:54:40 +00:00
|
|
|
}
|
2015-06-26 16:12:54 +00:00
|
|
|
}
|
2015-07-02 09:05:50 +00:00
|
|
|
|
2020-04-26 15:19:49 +00:00
|
|
|
// SendAlbum sends multiple instances of media as a single message.
|
2020-07-25 21:01:39 +00:00
|
|
|
// From all existing options, it only supports tele.Silent.
|
2020-06-19 20:34:37 +00:00
|
|
|
func (b *Bot) SendAlbum(to Recipient, a Album, opts ...interface{}) ([]Message, error) {
|
2020-04-16 14:57:16 +00:00
|
|
|
if to == nil {
|
|
|
|
return nil, ErrBadRecipient
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:28:21 +00:00
|
|
|
sendOpts := extractOptions(opts)
|
2017-11-25 14:22:13 +00:00
|
|
|
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 {
|
2020-04-26 15:19:49 +00:00
|
|
|
var (
|
|
|
|
repr string
|
|
|
|
data []byte
|
2020-06-09 20:28:28 +00:00
|
|
|
file = x.MediaFile()
|
2020-04-26 15:19:49 +00:00
|
|
|
)
|
2017-11-25 14:22:13 +00:00
|
|
|
|
2020-03-29 12:59:27 +00:00
|
|
|
switch {
|
2020-06-09 20:28:28 +00:00
|
|
|
case file.InCloud():
|
|
|
|
repr = file.FileID
|
|
|
|
case file.FileURL != "":
|
|
|
|
repr = file.FileURL
|
|
|
|
case file.OnDisk() || file.FileReader != nil:
|
2020-04-26 15:19:49 +00:00
|
|
|
repr = "attach://" + strconv.Itoa(i)
|
2020-06-09 20:28:28 +00:00
|
|
|
files[strconv.Itoa(i)] = *file
|
2020-03-29 12:59:27 +00:00
|
|
|
default:
|
2020-04-26 15:19:49 +00:00
|
|
|
return nil, errors.Errorf("telebot: album entry #%d does not exist", i)
|
2017-11-25 14:22:13 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:28:21 +00:00
|
|
|
im := x.InputMedia()
|
|
|
|
im.Media = repr
|
2021-11-07 18:32:22 +00:00
|
|
|
|
|
|
|
if len(sendOpts.Entities) > 0 {
|
|
|
|
im.Entities = sendOpts.Entities
|
|
|
|
} else {
|
|
|
|
im.ParseMode = sendOpts.ParseMode
|
|
|
|
}
|
2017-11-25 14:22:13 +00:00
|
|
|
|
2021-11-07 16:28:21 +00:00
|
|
|
data, _ = json.Marshal(im)
|
2020-04-26 15:19:49 +00:00
|
|
|
media[i] = string(data)
|
2017-11-25 14:22:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": to.Recipient(),
|
|
|
|
"media": "[" + strings.Join(media, ",") + "]",
|
|
|
|
}
|
2020-06-07 20:12:49 +00:00
|
|
|
b.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 {
|
2020-04-26 15:19:49 +00:00
|
|
|
Result []Message
|
2017-11-25 14:22:13 +00:00
|
|
|
}
|
2020-04-26 15:19:49 +00:00
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
return nil, wrapError(err)
|
2017-11-25 14:22:13 +00:00
|
|
|
}
|
|
|
|
|
2020-03-29 12:59:27 +00:00
|
|
|
for attachName := range files {
|
2017-11-25 14:22:13 +00:00
|
|
|
i, _ := strconv.Atoi(attachName)
|
2021-11-26 23:29:36 +00:00
|
|
|
r := resp.Result[i]
|
2017-11-25 14:22:13 +00:00
|
|
|
|
|
|
|
var newID string
|
2021-11-26 23:29:36 +00:00
|
|
|
switch {
|
|
|
|
case r.Photo != nil:
|
|
|
|
newID = r.Photo.FileID
|
|
|
|
case r.Video != nil:
|
|
|
|
newID = r.Video.FileID
|
|
|
|
case r.Audio != nil:
|
|
|
|
newID = r.Audio.FileID
|
|
|
|
case r.Document != nil:
|
|
|
|
newID = r.Document.FileID
|
2017-11-25 14:22:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
a[i].MediaFile().FileID = newID
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reply behaves just like Send() with an exception of "reply-to" indicator.
|
2020-04-16 15:56:47 +00:00
|
|
|
// This function will panic upon nil Message.
|
2020-06-19 20:34:37 +00:00
|
|
|
func (b *Bot) Reply(to *Message, what interface{}, opts ...interface{}) (*Message, error) {
|
|
|
|
sendOpts := extractOptions(opts)
|
2017-11-19 01:44:31 +00:00
|
|
|
if sendOpts == nil {
|
|
|
|
sendOpts = &SendOptions{}
|
2015-07-03 18:54:40 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 01:44:31 +00:00
|
|
|
sendOpts.ReplyTo = to
|
|
|
|
return b.Send(to.Chat, what, sendOpts)
|
2015-07-02 09:05:50 +00:00
|
|
|
}
|
2015-07-02 18:39:39 +00:00
|
|
|
|
2020-04-16 15:56:47 +00:00
|
|
|
// Forward behaves just like Send() but of all options it only supports Silent (see Bots API).
|
2020-05-21 09:13:20 +00:00
|
|
|
// This function will panic upon nil Editable.
|
2020-06-19 20:34:37 +00:00
|
|
|
func (b *Bot) Forward(to Recipient, msg Editable, opts ...interface{}) (*Message, error) {
|
2020-04-16 14:57:16 +00:00
|
|
|
if to == nil {
|
|
|
|
return nil, ErrBadRecipient
|
|
|
|
}
|
2020-05-21 09:13:20 +00:00
|
|
|
msgID, chatID := msg.MessageSig()
|
2020-04-16 14:57:16 +00:00
|
|
|
|
2016-06-26 14:05:37 +00:00
|
|
|
params := map[string]string{
|
2017-11-18 14:41:23 +00:00
|
|
|
"chat_id": to.Recipient(),
|
2020-05-21 09:13:20 +00:00
|
|
|
"from_chat_id": strconv.FormatInt(chatID, 10),
|
|
|
|
"message_id": msgID,
|
2015-07-06 13:46:19 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 20:34:37 +00:00
|
|
|
sendOpts := extractOptions(opts)
|
2020-06-07 20:12:49 +00:00
|
|
|
b.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
|
|
|
|
2021-05-24 08:41:58 +00:00
|
|
|
// Copy behaves just like Forward() but the copied message doesn't have a link to the original message (see Bots API).
|
|
|
|
//
|
|
|
|
// This function will panic upon nil Editable.
|
|
|
|
func (b *Bot) Copy(to Recipient, msg Editable, options ...interface{}) (*Message, error) {
|
|
|
|
if to == nil {
|
|
|
|
return nil, ErrBadRecipient
|
|
|
|
}
|
|
|
|
msgID, chatID := msg.MessageSig()
|
|
|
|
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": to.Recipient(),
|
|
|
|
"from_chat_id": strconv.FormatInt(chatID, 10),
|
|
|
|
"message_id": msgID,
|
|
|
|
}
|
|
|
|
|
|
|
|
sendOpts := extractOptions(options)
|
|
|
|
b.embedSendOptions(params, sendOpts)
|
|
|
|
|
|
|
|
data, err := b.Raw("copyMessage", params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return extractMessage(data)
|
|
|
|
}
|
|
|
|
|
2017-11-19 01:44:31 +00:00
|
|
|
// Edit is magic, it lets you change already sent message.
|
2020-06-19 20:34:37 +00:00
|
|
|
// This function will panic upon nil Editable.
|
2017-11-19 01:44:31 +00:00
|
|
|
//
|
2020-05-31 16:11:42 +00:00
|
|
|
// If edited message is sent by the bot, returns it,
|
|
|
|
// otherwise returns nil and ErrTrueResult.
|
|
|
|
//
|
2017-11-19 01:44:31 +00:00
|
|
|
// Use cases:
|
|
|
|
//
|
2020-06-07 18:41:13 +00:00
|
|
|
// b.Edit(m, m.Text, newMarkup)
|
2020-07-25 21:01:39 +00:00
|
|
|
// b.Edit(m, "new <b>text</b>", tele.ModeHTML)
|
|
|
|
// b.Edit(m, &tele.ReplyMarkup{...})
|
|
|
|
// b.Edit(m, &tele.Photo{File: ...})
|
|
|
|
// b.Edit(m, tele.Location{42.1337, 69.4242})
|
2021-07-25 13:07:48 +00:00
|
|
|
// b.Edit(c, "edit inline message from the callback")
|
|
|
|
// b.Edit(r, "edit message from chosen inline result")
|
2017-11-19 01:44:31 +00:00
|
|
|
//
|
2020-06-19 20:34:37 +00:00
|
|
|
func (b *Bot) Edit(msg Editable, what interface{}, opts ...interface{}) (*Message, error) {
|
2020-04-16 15:56:47 +00:00
|
|
|
var (
|
|
|
|
method string
|
2020-05-05 20:40:48 +00:00
|
|
|
params = make(map[string]string)
|
2020-04-16 15:56:47 +00:00
|
|
|
)
|
2017-11-26 09:15:11 +00:00
|
|
|
|
|
|
|
switch v := what.(type) {
|
2020-06-07 18:41:13 +00:00
|
|
|
case *ReplyMarkup:
|
|
|
|
return b.EditReplyMarkup(msg, v)
|
2021-11-07 16:28:21 +00:00
|
|
|
case Inputtable:
|
2020-06-19 20:34:37 +00:00
|
|
|
return b.EditMedia(msg, v, opts...)
|
2017-11-26 09:15:11 +00:00
|
|
|
case string:
|
2020-04-16 15:56:47 +00:00
|
|
|
method = "editMessageText"
|
2017-11-26 09:15:11 +00:00
|
|
|
params["text"] = v
|
|
|
|
case Location:
|
2020-04-16 15:56:47 +00:00
|
|
|
method = "editMessageLiveLocation"
|
2017-11-26 09:15:11 +00:00
|
|
|
params["latitude"] = fmt.Sprintf("%f", v.Lat)
|
|
|
|
params["longitude"] = fmt.Sprintf("%f", v.Lng)
|
2021-05-23 16:55:49 +00:00
|
|
|
|
|
|
|
if v.HorizontalAccuracy != nil {
|
|
|
|
params["horizontal_accuracy"] = fmt.Sprintf("%f", *v.HorizontalAccuracy)
|
|
|
|
}
|
|
|
|
if v.Heading != 0 {
|
|
|
|
params["heading"] = strconv.Itoa(v.Heading)
|
|
|
|
}
|
|
|
|
if v.AlertRadius != 0 {
|
|
|
|
params["proximity_alert_radius"] = strconv.Itoa(v.AlertRadius)
|
|
|
|
}
|
2017-11-26 09:15:11 +00:00
|
|
|
default:
|
2020-04-16 15:56:47 +00:00
|
|
|
return nil, ErrUnsupportedWhat
|
2017-11-26 09:15:11 +00:00
|
|
|
}
|
2017-11-19 01:44:31 +00:00
|
|
|
|
2020-04-16 15:56:47 +00:00
|
|
|
msgID, chatID := msg.MessageSig()
|
2020-05-06 17:20:01 +00:00
|
|
|
|
2020-04-16 15:56:47 +00:00
|
|
|
if chatID == 0 { // if inline message
|
|
|
|
params["inline_message_id"] = msgID
|
2017-11-19 01:44:31 +00:00
|
|
|
} else {
|
|
|
|
params["chat_id"] = strconv.FormatInt(chatID, 10)
|
2020-04-16 15:56:47 +00:00
|
|
|
params["message_id"] = msgID
|
2017-11-19 01:44:31 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 20:34:37 +00:00
|
|
|
sendOpts := extractOptions(opts)
|
2020-06-07 20:12:49 +00:00
|
|
|
b.embedSendOptions(params, sendOpts)
|
2017-11-19 01:44:31 +00:00
|
|
|
|
2020-04-16 15:56:47 +00:00
|
|
|
data, err := b.Raw(method, 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-04-26 15:19:49 +00:00
|
|
|
// EditReplyMarkup edits reply markup of already sent message.
|
2020-06-19 20:34:37 +00:00
|
|
|
// This function will panic upon nil Editable.
|
2020-04-23 18:56:27 +00:00
|
|
|
// Pass nil or empty ReplyMarkup to delete it from the message.
|
2020-03-29 12:59:27 +00:00
|
|
|
//
|
2020-05-31 16:11:42 +00:00
|
|
|
// If edited message is sent by the bot, returns it,
|
|
|
|
// otherwise returns nil and ErrTrueResult.
|
|
|
|
//
|
2020-04-26 15:19:49 +00:00
|
|
|
func (b *Bot) EditReplyMarkup(msg Editable, markup *ReplyMarkup) (*Message, error) {
|
|
|
|
msgID, chatID := msg.MessageSig()
|
2020-05-05 20:40:48 +00:00
|
|
|
params := make(map[string]string)
|
2020-03-29 12:59:27 +00:00
|
|
|
|
2020-04-23 18:56:27 +00:00
|
|
|
if chatID == 0 { // if inline message
|
2020-04-26 15:19:49 +00:00
|
|
|
params["inline_message_id"] = msgID
|
2020-03-29 12:59:27 +00:00
|
|
|
} else {
|
|
|
|
params["chat_id"] = strconv.FormatInt(chatID, 10)
|
2020-04-26 15:19:49 +00:00
|
|
|
params["message_id"] = msgID
|
2020-03-29 12:59:27 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 18:56:27 +00:00
|
|
|
if markup == nil {
|
|
|
|
// will delete reply markup
|
|
|
|
markup = &ReplyMarkup{}
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:59:27 +00:00
|
|
|
processButtons(markup.InlineKeyboard)
|
2020-04-23 18:56:27 +00:00
|
|
|
data, _ := json.Marshal(markup)
|
|
|
|
params["reply_markup"] = string(data)
|
2020-03-29 12:59:27 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-04-26 15:19:49 +00:00
|
|
|
// EditCaption edits already sent photo caption with known recipient and message id.
|
2020-06-19 20:34:37 +00:00
|
|
|
// This function will panic upon nil Editable.
|
2017-11-19 01:44:31 +00:00
|
|
|
//
|
2020-05-31 16:11:42 +00:00
|
|
|
// If edited message is sent by the bot, returns it,
|
|
|
|
// otherwise returns nil and ErrTrueResult.
|
|
|
|
//
|
2020-06-19 20:34:37 +00:00
|
|
|
func (b *Bot) EditCaption(msg Editable, caption string, opts ...interface{}) (*Message, error) {
|
2020-04-26 15:19:49 +00:00
|
|
|
msgID, chatID := msg.MessageSig()
|
2017-11-19 01:44:31 +00:00
|
|
|
|
2020-04-26 15:19:49 +00:00
|
|
|
params := map[string]string{
|
|
|
|
"caption": caption,
|
|
|
|
}
|
2017-11-19 01:44:31 +00:00
|
|
|
|
2020-04-26 15:19:49 +00:00
|
|
|
if chatID == 0 { // if inline message
|
|
|
|
params["inline_message_id"] = msgID
|
2017-11-19 01:44:31 +00:00
|
|
|
} else {
|
|
|
|
params["chat_id"] = strconv.FormatInt(chatID, 10)
|
2020-04-26 15:19:49 +00:00
|
|
|
params["message_id"] = msgID
|
2017-11-19 01:44:31 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 20:34:37 +00:00
|
|
|
sendOpts := extractOptions(opts)
|
2020-06-07 20:12:49 +00:00
|
|
|
b.embedSendOptions(params, sendOpts)
|
2020-03-29 12:59:27 +00:00
|
|
|
|
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-04-26 15:19:49 +00:00
|
|
|
// EditMedia edits already sent media with known recipient and message id.
|
2020-06-19 20:34:37 +00:00
|
|
|
// This function will panic upon nil Editable.
|
2019-02-20 06:11:21 +00:00
|
|
|
//
|
2020-05-31 16:11:42 +00:00
|
|
|
// If edited message is sent by the bot, returns it,
|
|
|
|
// otherwise returns nil and ErrTrueResult.
|
|
|
|
//
|
2019-02-20 06:11:21 +00:00
|
|
|
// Use cases:
|
|
|
|
//
|
2020-07-25 21:01:39 +00:00
|
|
|
// b.EditMedia(m, &tele.Photo{File: tele.FromDisk("chicken.jpg")})
|
|
|
|
// b.EditMedia(m, &tele.Video{File: tele.FromURL("http://video.mp4")})
|
2019-02-20 06:11:21 +00:00
|
|
|
//
|
2021-11-07 16:28:21 +00:00
|
|
|
func (b *Bot) EditMedia(msg Editable, media Inputtable, opts ...interface{}) (*Message, error) {
|
2020-04-26 15:19:49 +00:00
|
|
|
var (
|
|
|
|
repr string
|
2021-11-07 16:28:21 +00:00
|
|
|
file = media.MediaFile()
|
|
|
|
files = make(map[string]File)
|
2019-02-20 06:11:21 +00:00
|
|
|
|
2021-11-07 16:28:21 +00:00
|
|
|
thumb *Photo
|
2020-04-26 15:19:49 +00:00
|
|
|
thumbName = "thumb"
|
|
|
|
)
|
2019-02-20 06:11:21 +00:00
|
|
|
|
2020-03-29 12:59:27 +00:00
|
|
|
switch {
|
2020-04-26 15:19:49 +00:00
|
|
|
case file.InCloud():
|
|
|
|
repr = file.FileID
|
|
|
|
case file.FileURL != "":
|
|
|
|
repr = file.FileURL
|
|
|
|
case file.OnDisk() || file.FileReader != nil:
|
|
|
|
s := file.FileLocal
|
|
|
|
if file.FileReader != nil {
|
2019-07-22 18:10:18 +00:00
|
|
|
s = "0"
|
2020-04-26 15:19:49 +00:00
|
|
|
} else if s == thumbName {
|
|
|
|
thumbName = "thumb2"
|
2019-02-20 06:11:21 +00:00
|
|
|
}
|
2020-04-26 15:19:49 +00:00
|
|
|
|
|
|
|
repr = "attach://" + s
|
|
|
|
files[s] = *file
|
2020-03-29 12:59:27 +00:00
|
|
|
default:
|
2021-11-07 16:28:21 +00:00
|
|
|
return nil, errors.Errorf("telebot: cannot edit media, it does not exist")
|
2019-02-20 06:11:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 15:19:49 +00:00
|
|
|
switch m := media.(type) {
|
2019-07-22 18:10:18 +00:00
|
|
|
case *Video:
|
2020-04-26 15:19:49 +00:00
|
|
|
thumb = m.Thumbnail
|
2019-07-22 18:10:18 +00:00
|
|
|
case *Audio:
|
2021-11-07 16:28:21 +00:00
|
|
|
thumb = m.Thumbnail
|
|
|
|
case *Document:
|
2020-04-26 15:19:49 +00:00
|
|
|
thumb = m.Thumbnail
|
2021-09-30 16:41:17 +00:00
|
|
|
case *Animation:
|
|
|
|
thumb = m.Thumbnail
|
2019-02-20 06:11:21 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 15:19:49 +00:00
|
|
|
msgID, chatID := msg.MessageSig()
|
2020-05-06 17:20:01 +00:00
|
|
|
params := make(map[string]string)
|
2019-02-20 06:11:21 +00:00
|
|
|
|
2020-06-19 20:34:37 +00:00
|
|
|
sendOpts := extractOptions(opts)
|
2020-06-07 20:12:49 +00:00
|
|
|
b.embedSendOptions(params, sendOpts)
|
2019-02-20 06:11:21 +00:00
|
|
|
|
2021-11-07 16:28:21 +00:00
|
|
|
im := media.InputMedia()
|
|
|
|
im.Media = repr
|
2021-11-07 18:32:22 +00:00
|
|
|
|
|
|
|
if len(sendOpts.Entities) > 0 {
|
|
|
|
im.Entities = sendOpts.Entities
|
|
|
|
} else {
|
|
|
|
im.ParseMode = sendOpts.ParseMode
|
|
|
|
}
|
2021-11-07 16:28:21 +00:00
|
|
|
|
2019-02-20 06:11:21 +00:00
|
|
|
if thumb != nil {
|
2021-11-07 16:28:21 +00:00
|
|
|
im.Thumbnail = "attach://" + thumbName
|
2020-04-26 15:19:49 +00:00
|
|
|
files[thumbName] = *thumb.MediaFile()
|
2019-02-20 06:11:21 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:28:21 +00:00
|
|
|
data, _ := json.Marshal(im)
|
2020-04-26 15:19:49 +00:00
|
|
|
params["media"] = string(data)
|
2019-02-20 06:11:21 +00:00
|
|
|
|
2020-05-31 16:11:42 +00:00
|
|
|
if chatID == 0 { // if inline message
|
2020-04-26 15:19:49 +00:00
|
|
|
params["inline_message_id"] = msgID
|
|
|
|
} else {
|
|
|
|
params["chat_id"] = strconv.FormatInt(chatID, 10)
|
|
|
|
params["message_id"] = msgID
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := b.sendFiles("editMessageMedia", files, 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
|
|
|
}
|
|
|
|
|
2020-06-19 20:34:37 +00:00
|
|
|
// Delete removes the message, including service messages.
|
|
|
|
// This function will panic upon nil Editable.
|
2017-11-17 14:29:44 +00:00
|
|
|
//
|
2017-11-21 03:08:06 +00:00
|
|
|
// * A message can only be deleted if it was sent less than 48 hours ago.
|
2020-06-19 20:34:37 +00:00
|
|
|
// * A dice message in a private chat can only be deleted if it was sent more than 24 hours ago.
|
|
|
|
// * Bots can delete outgoing messages in private chats, groups, and supergroups.
|
|
|
|
// * Bots can delete incoming messages in private chats.
|
|
|
|
// * Bots granted can_post_messages permissions can delete outgoing messages in channels.
|
2017-11-21 03:08:06 +00:00
|
|
|
// * 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.
|
|
|
|
//
|
2020-04-26 15:19:49 +00:00
|
|
|
func (b *Bot) Delete(msg Editable) error {
|
|
|
|
msgID, chatID := msg.MessageSig()
|
2017-11-19 01:44:31 +00:00
|
|
|
|
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),
|
2020-04-26 15:19:49 +00:00
|
|
|
"message_id": msgID,
|
2017-11-17 07:22:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 17:20:01 +00:00
|
|
|
_, err := b.Raw("deleteMessage", params)
|
|
|
|
return err
|
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.
|
2020-06-19 20:34:37 +00:00
|
|
|
//
|
2020-04-16 14:57:16 +00:00
|
|
|
func (b *Bot) Notify(to Recipient, action ChatAction) error {
|
|
|
|
if to == nil {
|
|
|
|
return ErrBadRecipient
|
|
|
|
}
|
|
|
|
|
2016-06-26 14:05:37 +00:00
|
|
|
params := map[string]string{
|
2020-04-16 14:57:16 +00:00
|
|
|
"chat_id": to.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-05-06 17:20:01 +00:00
|
|
|
_, err := b.Raw("sendChatAction", params)
|
|
|
|
return err
|
2015-07-06 16:13:08 +00:00
|
|
|
}
|
2016-06-26 14:05:37 +00:00
|
|
|
|
2020-05-20 20:40:42 +00:00
|
|
|
// Ship replies to the shipping query, if you sent an invoice
|
|
|
|
// requesting an address and the parameter is_flexible was specified.
|
|
|
|
//
|
2020-06-19 19:51:01 +00:00
|
|
|
// Example:
|
2020-05-21 10:38:06 +00:00
|
|
|
//
|
2020-05-21 09:02:35 +00:00
|
|
|
// b.Ship(query) // OK
|
|
|
|
// b.Ship(query, opts...) // OK with options
|
|
|
|
// b.Ship(query, "Oops!") // Error message
|
2020-05-20 20:40:42 +00:00
|
|
|
//
|
|
|
|
func (b *Bot) Ship(query *ShippingQuery, what ...interface{}) error {
|
|
|
|
params := map[string]string{
|
|
|
|
"shipping_query_id": query.ID,
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(what) == 0 {
|
|
|
|
params["ok"] = "True"
|
|
|
|
} else if s, ok := what[0].(string); ok {
|
|
|
|
params["ok"] = "False"
|
|
|
|
params["error_message"] = s
|
|
|
|
} else {
|
|
|
|
var opts []ShippingOption
|
|
|
|
for _, v := range what {
|
|
|
|
opt, ok := v.(ShippingOption)
|
|
|
|
if !ok {
|
|
|
|
return ErrUnsupportedWhat
|
|
|
|
}
|
|
|
|
opts = append(opts, opt)
|
|
|
|
}
|
|
|
|
|
|
|
|
params["ok"] = "True"
|
|
|
|
data, _ := json.Marshal(opts)
|
|
|
|
params["shipping_options"] = string(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := b.Raw("answerShippingQuery", params)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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-05-06 17:20:01 +00:00
|
|
|
_, err := b.Raw("answerPreCheckoutQuery", params)
|
|
|
|
return err
|
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.
|
2020-04-26 15:19:49 +00:00
|
|
|
func (b *Bot) Answer(query *Query, resp *QueryResponse) error {
|
|
|
|
resp.QueryID = query.ID
|
2017-11-29 20:14:25 +00:00
|
|
|
|
2020-04-26 15:19:49 +00:00
|
|
|
for _, result := range resp.Results {
|
2021-07-07 13:00:51 +00:00
|
|
|
result.Process(b)
|
2017-12-26 22:39:15 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 17:20:01 +00:00
|
|
|
_, err := b.Raw("answerInlineQuery", resp)
|
|
|
|
return err
|
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:
|
|
|
|
//
|
2020-06-19 19:51:01 +00:00
|
|
|
// b.Respond(c)
|
|
|
|
// b.Respond(c, response)
|
2017-12-10 22:32:08 +00:00
|
|
|
//
|
2020-06-09 20:28:28 +00:00
|
|
|
func (b *Bot) Respond(c *Callback, resp ...*CallbackResponse) error {
|
|
|
|
var r *CallbackResponse
|
|
|
|
if resp == nil {
|
|
|
|
r = &CallbackResponse{}
|
2017-12-10 22:32:08 +00:00
|
|
|
} else {
|
2020-06-09 20:28:28 +00:00
|
|
|
r = resp[0]
|
2017-12-10 22:32:08 +00:00
|
|
|
}
|
2016-07-25 22:09:18 +00:00
|
|
|
|
2020-06-09 20:28:28 +00:00
|
|
|
r.CallbackID = c.ID
|
|
|
|
_, err := b.Raw("answerCallbackQuery", r)
|
2020-05-06 17:20:01 +00:00
|
|
|
return err
|
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.
|
2020-06-19 20:34:37 +00:00
|
|
|
//
|
2017-11-19 15:16:39 +00:00
|
|
|
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 {
|
2020-04-26 15:19:49 +00:00
|
|
|
Result File
|
2016-09-27 12:04:13 +00:00
|
|
|
}
|
2020-04-26 15:19:49 +00:00
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
return File{}, wrapError(err)
|
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 {
|
2020-06-19 20:34:37 +00:00
|
|
|
reader, err := b.File(file)
|
2017-11-24 15:58:40 +00:00
|
|
|
if err != nil {
|
2020-06-19 20:34:37 +00:00
|
|
|
return 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
|
|
|
}
|
|
|
|
|
2020-04-26 15:19:49 +00:00
|
|
|
file.FileLocal = localFilename
|
2019-03-03 05:14:14 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-19 20:34:37 +00:00
|
|
|
// File gets a file from Telegram servers.
|
|
|
|
func (b *Bot) File(file *File) (io.ReadCloser, error) {
|
2019-03-03 05:14:14 +00:00
|
|
|
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-04-26 15:19:49 +00:00
|
|
|
url := b.URL + "/file/bot" + b.Token + "/" + f.FilePath
|
|
|
|
file.FilePath = f.FilePath // saving file path
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodGet, url, nil)
|
2020-03-29 12:59:27 +00:00
|
|
|
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-04-26 15:19:49 +00:00
|
|
|
return nil, wrapError(err)
|
2020-03-29 12:59:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
resp.Body.Close()
|
2020-04-26 15:19:49 +00:00
|
|
|
return nil, errors.Errorf("telebot: expected status 200 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
|
|
|
|
2020-04-26 15:19:49 +00:00
|
|
|
// StopLiveLocation stops broadcasting live message location
|
2017-11-26 09:15:11 +00:00
|
|
|
// before Location.LivePeriod expires.
|
|
|
|
//
|
2020-06-19 20:34:37 +00:00
|
|
|
// It supports ReplyMarkup.
|
|
|
|
// This function will panic upon nil Editable.
|
|
|
|
//
|
2020-05-31 16:11:42 +00:00
|
|
|
// If the message is sent by the bot, returns it,
|
|
|
|
// otherwise returns nil and ErrTrueResult.
|
|
|
|
//
|
2020-06-19 20:34:37 +00:00
|
|
|
func (b *Bot) StopLiveLocation(msg Editable, opts ...interface{}) (*Message, error) {
|
2020-04-26 15:19:49 +00:00
|
|
|
msgID, chatID := msg.MessageSig()
|
2017-11-26 09:15:11 +00:00
|
|
|
|
|
|
|
params := map[string]string{
|
2020-03-29 12:59:27 +00:00
|
|
|
"chat_id": strconv.FormatInt(chatID, 10),
|
2020-04-26 15:19:49 +00:00
|
|
|
"message_id": msgID,
|
2017-11-26 09:15:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 20:34:37 +00:00
|
|
|
sendOpts := extractOptions(opts)
|
2020-06-07 20:12:49 +00:00
|
|
|
b.embedSendOptions(params, sendOpts)
|
2017-11-26 09:15:11 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-04-26 14:05:21 +00:00
|
|
|
// StopPoll stops a poll which was sent by the bot and returns
|
|
|
|
// the stopped Poll object with the final results.
|
|
|
|
//
|
|
|
|
// It supports ReplyMarkup.
|
|
|
|
// This function will panic upon nil Editable.
|
2020-06-19 20:34:37 +00:00
|
|
|
//
|
|
|
|
func (b *Bot) StopPoll(msg Editable, opts ...interface{}) (*Poll, error) {
|
2020-04-26 14:05:21 +00:00
|
|
|
msgID, chatID := msg.MessageSig()
|
|
|
|
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": strconv.FormatInt(chatID, 10),
|
|
|
|
"message_id": msgID,
|
|
|
|
}
|
|
|
|
|
2020-06-19 20:34:37 +00:00
|
|
|
sendOpts := extractOptions(opts)
|
2020-06-07 20:12:49 +00:00
|
|
|
b.embedSendOptions(params, sendOpts)
|
2020-04-26 14:05:21 +00:00
|
|
|
|
|
|
|
data, err := b.Raw("stopPoll", params)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp struct {
|
|
|
|
Result *Poll
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
return nil, wrapError(err)
|
|
|
|
}
|
|
|
|
return resp.Result, nil
|
|
|
|
}
|
|
|
|
|
2020-06-19 20:34:37 +00:00
|
|
|
// InviteLink should be used to export chat's invite link.
|
|
|
|
func (b *Bot) InviteLink(chat *Chat) (string, error) {
|
2017-11-26 09:15:11 +00:00
|
|
|
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 {
|
2020-04-26 15:19:49 +00:00
|
|
|
Result string
|
2017-11-26 09:15:11 +00:00
|
|
|
}
|
2020-04-26 15:19:49 +00:00
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
return "", wrapError(err)
|
2017-11-26 09:15:11 +00:00
|
|
|
}
|
|
|
|
return resp.Result, nil
|
|
|
|
}
|
|
|
|
|
2020-03-29 12:59:27 +00:00
|
|
|
// SetGroupTitle should be used to update group title.
|
2020-04-26 15:19:49 +00:00
|
|
|
func (b *Bot) SetGroupTitle(chat *Chat, title string) error {
|
2017-11-26 09:15:11 +00:00
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": chat.Recipient(),
|
2020-04-26 15:19:49 +00:00
|
|
|
"title": title,
|
2017-11-26 09:15:11 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 17:20:01 +00:00
|
|
|
_, err := b.Raw("setChatTitle", params)
|
|
|
|
return err
|
2017-11-26 09:15:11 +00:00
|
|
|
}
|
|
|
|
|
2020-11-04 05:10:29 +00:00
|
|
|
// SetGroupDescription should be used to update group description.
|
2017-11-26 09:15:11 +00:00
|
|
|
func (b *Bot) SetGroupDescription(chat *Chat, description string) error {
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": chat.Recipient(),
|
|
|
|
"description": description,
|
|
|
|
}
|
|
|
|
|
2020-05-06 17:20:01 +00:00
|
|
|
_, err := b.Raw("setChatDescription", params)
|
|
|
|
return err
|
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-05-06 17:20:01 +00:00
|
|
|
_, err := b.sendFiles("setChatPhoto", map[string]File{"photo": p.File}, params)
|
|
|
|
return err
|
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-05-06 17:20:01 +00:00
|
|
|
_, err := b.Raw("setChatStickerSet", params)
|
|
|
|
return err
|
2017-11-26 09:15:11 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 11:47:08 +00:00
|
|
|
// SetGroupPermissions sets default chat permissions for all members.
|
|
|
|
func (b *Bot) SetGroupPermissions(chat *Chat, perms Rights) error {
|
2020-06-24 01:07:53 +00:00
|
|
|
params := map[string]interface{}{
|
2021-09-25 21:28:18 +00:00
|
|
|
"chat_id": chat.Recipient(),
|
|
|
|
"permissions": perms,
|
2020-04-26 11:47:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-06 17:20:01 +00:00
|
|
|
_, err := b.Raw("setChatPermissions", params)
|
|
|
|
return err
|
2020-04-26 11:47:08 +00:00
|
|
|
}
|
|
|
|
|
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-05-06 17:20:01 +00:00
|
|
|
_, err := b.Raw("deleteChatPhoto", params)
|
|
|
|
return err
|
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-05-06 17:20:01 +00:00
|
|
|
_, err := b.Raw("deleteChatStickerSet", params)
|
|
|
|
return err
|
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-05-06 17:20:01 +00:00
|
|
|
_, err := b.Raw("leaveChat", params)
|
|
|
|
return err
|
2016-10-09 20:00:04 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 15:19:49 +00:00
|
|
|
// Pin pins a message in a supergroup or a channel.
|
2016-11-09 23:28:00 +00:00
|
|
|
//
|
2020-06-19 20:34:37 +00:00
|
|
|
// It supports Silent option.
|
2020-04-26 15:19:49 +00:00
|
|
|
// This function will panic upon nil Editable.
|
2020-06-19 20:34:37 +00:00
|
|
|
//
|
|
|
|
func (b *Bot) Pin(msg Editable, opts ...interface{}) error {
|
2020-04-26 15:19:49 +00:00
|
|
|
msgID, chatID := msg.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),
|
2020-04-26 15:19:49 +00:00
|
|
|
"message_id": msgID,
|
2016-10-09 20:00:04 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 20:34:37 +00:00
|
|
|
sendOpts := extractOptions(opts)
|
2020-06-07 20:12:49 +00:00
|
|
|
b.embedSendOptions(params, sendOpts)
|
2016-10-09 20:00:04 +00:00
|
|
|
|
2020-05-06 17:20:01 +00:00
|
|
|
_, err := b.Raw("pinChatMessage", params)
|
|
|
|
return err
|
2016-10-09 19:57:08 +00:00
|
|
|
}
|
|
|
|
|
2020-04-26 15:19:49 +00:00
|
|
|
// Unpin unpins a message in a supergroup or a channel.
|
2021-05-23 16:55:49 +00:00
|
|
|
// It supports tb.Silent option.
|
|
|
|
func (b *Bot) Unpin(chat *Chat, messageID ...int) 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
|
|
|
}
|
2021-05-23 16:55:49 +00:00
|
|
|
if len(messageID) > 0 {
|
|
|
|
params["message_id"] = strconv.Itoa(messageID[0])
|
|
|
|
}
|
2017-11-18 10:19:58 +00:00
|
|
|
|
2020-05-06 17:20:01 +00:00
|
|
|
_, err := b.Raw("unpinChatMessage", params)
|
|
|
|
return err
|
2016-10-09 20:09:07 +00:00
|
|
|
}
|
|
|
|
|
2021-05-23 16:55:49 +00:00
|
|
|
// UnpinAll unpins all messages in a supergroup or a channel.
|
|
|
|
//
|
|
|
|
// It supports tb.Silent option.
|
|
|
|
func (b *Bot) UnpinAll(chat *Chat) error {
|
|
|
|
params := map[string]string{
|
|
|
|
"chat_id": chat.Recipient(),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := b.Raw("unpinAllChatMessages", params)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
//
|
2021-09-29 18:39:44 +00:00
|
|
|
func (b *Bot) ChatByID(id int64) (*Chat, error) {
|
2021-10-13 17:52:30 +00:00
|
|
|
return b.ChatByUsername(strconv.FormatInt(id, 10))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bot) ChatByUsername(name string) (*Chat, error) {
|
2016-10-09 20:11:23 +00:00
|
|
|
params := map[string]string{
|
2021-10-13 17:52:30 +00:00
|
|
|
"chat_id": name,
|
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 {
|
2020-04-26 15:19:49 +00:00
|
|
|
Result *Chat
|
2016-10-09 20:11:23 +00:00
|
|
|
}
|
2020-04-26 15:19:49 +00:00
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
return nil, wrapError(err)
|
2016-10-09 20:11:23 +00:00
|
|
|
}
|
2018-05-19 16:22:14 +00:00
|
|
|
if resp.Result.Type == ChatChannel && resp.Result.Username == "" {
|
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
|
|
|
}
|
|
|
|
|
2020-04-26 15:19:49 +00:00
|
|
|
// ProfilePhotosOf returns list of profile pictures for a user.
|
2017-11-19 15:16:39 +00:00
|
|
|
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 {
|
|
|
|
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-10-09 20:29:03 +00:00
|
|
|
}
|
2020-04-26 15:19:49 +00:00
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
return nil, wrapError(err)
|
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
|
|
|
}
|
|
|
|
|
2020-04-26 15:19:49 +00:00
|
|
|
// ChatMemberOf returns information about a member of a chat.
|
2021-06-20 16:00:31 +00:00
|
|
|
func (b *Bot) ChatMemberOf(chat, user Recipient) (*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 {
|
2020-04-26 15:19:49 +00:00
|
|
|
Result *ChatMember
|
2016-10-09 20:16:33 +00:00
|
|
|
}
|
2020-04-26 15:19:49 +00:00
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
return nil, wrapError(err)
|
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
|
|
|
}
|
|
|
|
|
2020-06-19 20:34:37 +00:00
|
|
|
// Commands returns the current list of the bot's commands.
|
|
|
|
func (b *Bot) Commands() ([]Command, error) {
|
2020-04-26 12:55:04 +00:00
|
|
|
data, err := b.Raw("getMyCommands", nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp struct {
|
|
|
|
Result []Command
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
return nil, wrapError(err)
|
|
|
|
}
|
|
|
|
return resp.Result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetCommands changes the list of the bot's commands.
|
|
|
|
func (b *Bot) SetCommands(cmds []Command) error {
|
|
|
|
data, _ := json.Marshal(cmds)
|
|
|
|
|
|
|
|
params := map[string]string{
|
|
|
|
"commands": string(data),
|
|
|
|
}
|
|
|
|
|
2020-05-06 17:20:01 +00:00
|
|
|
_, err := b.Raw("setMyCommands", params)
|
|
|
|
return err
|
2020-04-26 12:55:04 +00:00
|
|
|
}
|
2021-05-23 16:55:49 +00:00
|
|
|
|
|
|
|
// Logout logs out from the cloud Bot API server before launching the bot locally.
|
|
|
|
func (b *Bot) Logout() (bool, error) {
|
|
|
|
data, err := b.Raw("logOut", nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp struct {
|
|
|
|
Result bool `json:"result"`
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
return false, wrapError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the bot instance before moving it from one local server to another.
|
|
|
|
func (b *Bot) Close() (bool, error) {
|
|
|
|
data, err := b.Raw("close", nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp struct {
|
|
|
|
Result bool `json:"result"`
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(data, &resp); err != nil {
|
|
|
|
return false, wrapError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp.Result, nil
|
|
|
|
}
|