telebot/bot.go

1145 lines
26 KiB
Go
Raw Normal View History

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"
"github.com/pkg/errors"
)
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.
func NewBot(pref Settings) (*Bot, error) {
if pref.Updates == 0 {
pref.Updates = 100
2017-08-15 15:00:22 +00:00
}
client := pref.Client
if client == nil {
client = http.DefaultClient
}
bot := &Bot{
2018-06-30 20:34:04 +00:00
Token: pref.Token,
Updates: make(chan Update, pref.Updates),
Poller: pref.Poller,
handlers: make(map[string]interface{}),
2017-11-27 12:52:16 +00:00
stop: make(chan struct{}),
reporter: pref.Reporter,
2018-04-18 18:17:03 +00:00
client: client,
2015-07-03 18:54:40 +00:00
}
user, err := bot.getMe()
if err != nil {
return nil, err
}
bot.Me = user
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
Updates chan Update
Poller Poller
handlers map[string]interface{}
reporter func(error)
stop chan struct{}
client *http.Client
2017-11-21 02:22:45 +00:00
}
// Settings represents a utility struct for passing certain
// properties of a bot around and is required to make bots.
type Settings struct {
// Telegram token
Token string
2017-11-21 02:22:45 +00:00
// Updates channel capacity
Updates int // Default: 100
// Poller is the provider of Updates.
Poller Poller
// Reporter is a callback function that will get called
// on any panics recovered from endpoint handlers.
Reporter func(error)
// HTTP Client used to make requests to telegram api
Client *http.Client
}
// Update object represents an incoming update.
type Update struct {
ID int `json:"update_id"`
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 01:07:36 +00:00
2017-12-26 22:46:50 +00:00
ChosenInlineResult *ChosenInlineResult `json:"chosen_inline_result,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 {
ResultID string `json:"result_id"`
Query string `json:"query"`
// Inline messages only!
MessageID string `json:"inline_message_id"`
From User `json:"from"`
Location *Location `json:"location,omitempty"`
}
2017-11-21 02:22:45 +00:00
// Handle lets you set the handler for some command name or
// one of the supported endpoints.
//
// Example:
//
// b.handle("/help", func (m *tb.Message) {})
// b.handle(tb.OnEdited, func (m *tb.Message) {})
// b.handle(tb.OnQuery, func (q *tb.Query) {})
//
// // make a hook for one of your preserved (by-pointer)
// // inline buttons.
// b.handle(&inlineButton, func (c *tb.Callback) {})
//
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
default:
panic("telebot: unsupported endpoint")
}
2017-11-21 02:22:45 +00:00
}
var (
2017-11-27 14:56:22 +00:00
cmdRx = regexp.MustCompile(`^(\/\w+)(@(\w+))?(\s|$)(.+)?`)
2017-12-10 22:32:08 +00:00
cbackRx = regexp.MustCompile(`^\f(\w+)(\|(.+))?$`)
)
2017-11-21 02:22:45 +00:00
func (b *Bot) handleCommand(m *Message, cmdName, cmdBot string) bool {
return false
}
// Start brings bot into motion by consuming incoming
// updates (see Bot.Updates channel).
func (b *Bot) Start() {
if b.Poller == nil {
panic("telebot: can't start without a poller")
}
2017-11-27 12:52:16 +00:00
stopPoller := make(chan struct{})
2017-11-27 12:52:16 +00:00
go b.Poller.Poll(b, b.Updates, stopPoller)
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-27 12:52:16 +00:00
// call to stop polling
case <-b.stop:
stopPoller <- struct{}{}
2017-11-27 12:52:16 +00:00
// polling has stopped
case <-stopPoller:
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 12:52:16 +00:00
// Command found - handle and return
2017-11-27 14:56:22 +00:00
if match != nil {
// Syntax: "</command>@<bot> <payload>"
2017-11-27 14:56:22 +00:00
command, botName := match[0][1], match[0][3]
m.Payload = match[0][5]
if botName != "" && !strings.EqualFold(b.Me.Username, botName) {
return
}
if b.handle(command, m) {
return
2017-11-27 14:56:22 +00:00
}
}
// 1:1 satisfaction
if b.handle(m.Text, m) {
return
}
2017-11-27 12:52:16 +00:00
// OnText
b.handle(OnText, m)
return
}
2017-11-27 12:52:16 +00:00
// on media
if b.handleMedia(m) {
return
}
// OnAddedToGroup
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-27 12:52:16 +00:00
if m.UserJoined != nil {
b.handle(OnUserJoined, m)
return
}
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-27 12:52:16 +00:00
return
}
2017-11-27 12:52:16 +00:00
if m.UserLeft != nil {
b.handle(OnUserLeft, m)
return
}
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-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-27 12:52:16 +00:00
if m.GroupPhotoDeleted {
b.handle(OnGroupPhotoDeleted, m)
return
}
2017-11-27 12:52:16 +00:00
if m.MigrateTo != 0 {
if handler, ok := b.handlers[OnMigration]; ok {
if handler, ok := handler.(func(int64, int64)); ok {
// i'm not 100% sure that any of the values
// won't be cached, so I pass them all in:
go func(b *Bot, handler func(int64, int64), from, to int64) {
defer b.deferDebug()
handler(from, to)
}(b, handler, m.MigrateFrom, m.MigrateTo)
} else {
panic("telebot: migration handler is bad")
2017-11-27 12:52:16 +00:00
}
}
return
}
2017-11-27 12:52:16 +00:00
return
}
2017-11-27 12:52:16 +00:00
if upd.EditedMessage != nil {
b.handle(OnEdited, upd.EditedMessage)
return
}
2017-11-27 12:52:16 +00:00
if upd.ChannelPost != nil {
b.handle(OnChannelPost, upd.ChannelPost)
return
}
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 != "" {
data := upd.Callback.Data
if data[0] == '\f' {
match := cbackRx.FindAllStringSubmatch(data, -1)
2017-11-27 12:52:16 +00:00
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 {
if handler, ok := handler.(func(*Callback)); ok {
upd.Callback.Data = payload
// i'm not 100% sure that any of the values
// won't be cached, so I pass them all in:
go func(b *Bot, handler func(*Callback), c *Callback) {
defer b.deferDebug()
handler(c)
}(b, handler, upd.Callback)
2017-11-27 12:52:16 +00:00
return
}
}
2017-11-27 12:52:16 +00:00
}
}
2017-11-27 12:52:16 +00:00
}
2017-11-27 12:52:16 +00:00
if handler, ok := b.handlers[OnCallback]; ok {
if handler, ok := handler.(func(*Callback)); ok {
// i'm not 100% sure that any of the values
// won't be cached, so I pass them all in:
go func(b *Bot, handler func(*Callback), c *Callback) {
defer b.deferDebug()
handler(c)
}(b, handler, upd.Callback)
} else {
panic("telebot: callback handler is bad")
2017-11-21 02:22:45 +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 {
if handler, ok := handler.(func(*Query)); ok {
// i'm not 100% sure that any of the values
// won't be cached, so I pass them all in:
go func(b *Bot, handler func(*Query), q *Query) {
defer b.deferDebug()
handler(q)
}(b, handler, upd.Query)
} else {
panic("telebot: query handler is bad")
2017-11-21 02:22:45 +00:00
}
}
2017-11-27 12:52:16 +00:00
return
}
2017-12-26 01:07:36 +00:00
if upd.ChosenInlineResult != nil {
if handler, ok := b.handlers[OnChosenInlineResult]; ok {
if handler, ok := handler.(func(*ChosenInlineResult)); ok {
// i'm not 100% sure that any of the values
// won't be cached, so I pass them all in:
go func(b *Bot, handler func(*ChosenInlineResult),
r *ChosenInlineResult) {
defer b.deferDebug()
handler(r)
}(b, handler, upd.ChosenInlineResult)
} else {
panic("telebot: chosen inline result handler is bad")
}
}
return
}
}
func (b *Bot) handle(end string, m *Message) bool {
handler, ok := b.handlers[end]
if !ok {
return false
}
if handler, ok := handler.(func(*Message)); ok {
// i'm not 100% sure that any of the values
// won't be cached, so I pass them all in:
go func(b *Bot, handler func(*Message), m *Message) {
defer b.deferDebug()
handler(m)
}(b, handler, m)
return true
}
return false
}
func (b *Bot) handleMedia(m *Message) bool {
if m.Photo != nil {
b.handle(OnPhoto, m)
return true
}
2018-04-24 15:46:07 +00:00
if m.Voice != nil {
b.handle(OnVoice, m)
return true
}
if m.Audio != nil {
b.handle(OnAudio, m)
return true
}
if m.Document != nil {
b.handle(OnDocument, m)
return true
}
if m.Sticker != nil {
b.handle(OnSticker, m)
return true
}
if m.Video != nil {
b.handle(OnVideo, m)
return true
}
if m.VideoNote != nil {
b.handle(OnVideoNote, m)
return true
}
if m.Contact != nil {
b.handle(OnContact, m)
return true
}
if m.Location != nil {
b.handle(OnLocation, m)
return true
}
if m.Venue != nil {
b.handle(OnVenue, m)
return true
}
return false
}
// Stop gracefully shuts the poller down.
func (b *Bot) Stop() {
2017-11-27 12:52:16 +00:00
b.stop <- struct{}{}
}
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.
//
// Note: since most arguments are of type interface{}, make sure to pass
// them by-pointer, NOT by-value, which will result in a panic.
//
// What is a send option exactly? It can be one of the following types:
//
// - *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!
func (b *Bot) Send(to Recipient, what interface{}, options ...interface{}) (*Message, error) {
sendOpts := extractOptions(options)
switch object := what.(type) {
case string:
return b.sendText(to, object, sendOpts)
case Sendable:
return object.Send(b, to, sendOpts)
default:
panic("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
// SendAlbum is used when sending multiple instances of media as a single
// message (so-called album).
2017-11-17 14:29:44 +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))
files := make(map[string]string)
for i, x := range a {
var (
f *File
caption string
mediaRepr, mediaType string
)
switch y := x.(type) {
case *Photo:
f = &y.File
mediaType = "photo"
caption = y.Caption
case *Video:
f = &y.File
mediaType = "video"
caption = y.Caption
default:
return nil, errors.Errorf("telebot: album entry #%d is not valid", i)
}
if f.InCloud() {
mediaRepr = f.FileID
} else if f.FileURL != "" {
mediaRepr = f.FileURL
} else if f.OnDisk() {
mediaRepr = fmt.Sprintf("attach://%d", i)
files[strconv.Itoa(i)] = f.FileLocal
} else {
return nil, errors.Errorf(
"telebot: album entry #%d doesn't exist anywhere", i)
}
jsonRepr, _ := json.Marshal(map[string]string{
"type": mediaType,
"media": mediaRepr,
"caption": caption,
})
media[i] = string(jsonRepr)
}
params := map[string]string{
"chat_id": to.Recipient(),
"media": "[" + strings.Join(media, ",") + "]",
}
sendOpts := extractOptions(options)
embedSendOptions(params, sendOpts)
respJSON, err := b.sendFiles("sendMediaGroup", files, params)
if err != nil {
return nil, err
}
var resp struct {
Ok bool
Result []Message
Description string
}
err = json.Unmarshal(respJSON, &resp)
if err != nil {
return nil, errors.Wrap(err, "bad response json")
}
if !resp.Ok {
return nil, errors.Errorf("api error: %s", resp.Description)
}
for attachName, _ := range files {
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.
func (b *Bot) Reply(to *Message, what interface{}, options ...interface{}) (*Message, error) {
// This function will panic upon unsupported payloads and options!
sendOpts := extractOptions(options)
if sendOpts == nil {
sendOpts = &SendOptions{}
2015-07-03 18:54:40 +00:00
}
sendOpts.ReplyTo = to
2015-07-03 18:54:40 +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!
func (b *Bot) Forward(to Recipient, what *Message, options ...interface{}) (*Message, error) {
params := map[string]string{
2017-11-18 14:41:23 +00:00
"chat_id": to.Recipient(),
"from_chat_id": what.Chat.Recipient(),
"message_id": strconv.Itoa(what.ID),
}
sendOpts := extractOptions(options)
embedSendOptions(params, sendOpts)
2015-07-03 18:54:40 +00:00
respJSON, err := b.Raw("forwardMessage", params)
2015-07-03 18:54:40 +00:00
if err != nil {
return nil, err
2015-07-03 18:54:40 +00:00
}
return extractMsgResponse(respJSON)
}
2016-07-04 10:56:53 +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()
// TODO: add support for inline messages (chatID = 0)
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")
}
// if inline message
if chatID == 0 {
params["inline_message_id"] = messageID
} else {
params["chat_id"] = strconv.FormatInt(chatID, 10)
params["message_id"] = messageID
}
sendOpts := extractOptions(options)
embedSendOptions(params, sendOpts)
respJSON, err := b.Raw("editMessageText", params)
if err != nil {
return nil, err
}
return extractMsgResponse(respJSON)
}
// EditCaption used to edit already sent photo caption with known recepient and message id.
//
// On success, returns edited message object
func (b *Bot) EditCaption(originalMsg Editable, caption string) (*Message, error) {
messageID, chatID := originalMsg.MessageSig()
params := map[string]string{"caption": caption}
// if inline message
if chatID == 0 {
params["inline_message_id"] = messageID
} else {
params["chat_id"] = strconv.FormatInt(chatID, 10)
params["message_id"] = messageID
}
respJSON, err := b.Raw("editMessageCaption", params)
if err != nil {
return nil, err
}
return extractMsgResponse(respJSON)
}
2017-11-17 14:29:44 +00:00
// Delete removes the message, including service messages,
// with the following limitations:
//
// * 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.
//
func (b *Bot) Delete(message Editable) error {
messageID, chatID := message.MessageSig()
2017-11-17 07:22:16 +00:00
params := map[string]string{
"chat_id": strconv.FormatInt(chatID, 10),
"message_id": messageID,
2017-11-17 07:22:16 +00:00
}
respJSON, err := b.Raw("deleteMessage", params)
2017-11-17 07:22:16 +00:00
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
// 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
// and die just once the client recieves a message from the bot.
//
// Currently, Telegram supports only a narrow range of possible
// actions, these are aligned as constants of this package.
func (b *Bot) Notify(recipient Recipient, action ChatAction) error {
params := map[string]string{
2017-11-18 14:41:23 +00:00
"chat_id": recipient.Recipient(),
"action": string(action),
}
2015-07-06 16:13:08 +00:00
respJSON, err := b.Raw("sendChatAction", params)
if err != nil {
return err
}
2017-11-18 10:19:58 +00:00
return extractOkResponse(respJSON)
2015-07-06 16:13:08 +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
// will result in an error.
func (b *Bot) Answer(query *Query, response *QueryResponse) error {
2017-11-29 20:14:25 +00:00
response.QueryID = query.ID
for _, result := range response.Results {
result.Process()
}
respJSON, err := b.Raw("answerInlineQuery", response)
if err != nil {
return err
}
2017-11-18 10:19:58 +00:00
return extractOkResponse(respJSON)
}
2016-07-25 22:09:18 +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
respJSON, err := b.Raw("answerCallbackQuery", response)
2016-07-25 22:09:18 +00:00
if err != nil {
return err
}
2017-11-18 10:19:58 +00:00
return extractOkResponse(respJSON)
2016-07-25 22:09:18 +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.
//
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) {
params := map[string]string{
2016-10-09 19:57:08 +00:00
"file_id": fileID,
}
2017-11-18 10:19:58 +00:00
respJSON, err := b.Raw("getFile", params)
if err != nil {
2017-11-19 15:16:39 +00:00
return File{}, err
}
2017-11-18 10:19:58 +00:00
var resp struct {
Ok bool
Description string
2017-11-19 15:16:39 +00:00
Result File
}
2017-11-18 10:19:58 +00:00
err = json.Unmarshal(respJSON, &resp)
if err != nil {
2017-11-19 15:16:39 +00:00
return File{}, errors.Wrap(err, "bad response json")
}
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-11-18 10:19:58 +00:00
return resp.Result, nil
}
2017-11-24 15:58:40 +00:00
// Download saves the file from Telegram servers locally.
//
// Maximum file size to download is 20 MB.
func (b *Bot) Download(f *File, localFilename string) error {
g, err := b.FileByID(f.FileID)
if err != nil {
return err
}
url := fmt.Sprintf("https://api.telegram.org/file/bot%s/%s",
b.Token, g.FilePath)
out, err := os.Create(localFilename)
if err != nil {
return wrapSystem(err)
}
defer out.Close()
resp, err := http.Get(url)
if err != nil {
return wrapSystem(err)
}
defer resp.Body.Close()
_, err = io.Copy(out, resp.Body)
if err != nil {
return wrapSystem(err)
}
g.FileLocal = localFilename
*f = g
return nil
}
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{
"chat_id": fmt.Sprintf("%d", chatID),
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)
respJSON, err := b.Raw("stopMessageLiveLocation", params)
2017-11-26 09:15:11 +00:00
if err != nil {
return nil, err
}
return extractMsgResponse(respJSON)
}
// 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(),
}
respJSON, 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
}
err = json.Unmarshal(respJSON, &resp)
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
}
// SetChatTitle should be used to update group title.
func (b *Bot) SetGroupTitle(chat *Chat, newTitle string) error {
params := map[string]string{
"chat_id": chat.Recipient(),
"title": newTitle,
}
respJSON, err := b.Raw("setChatTitle", params)
2017-11-26 09:15:11 +00:00
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
// 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,
}
respJSON, err := b.Raw("setChatDescription", params)
2017-11-26 09:15:11 +00:00
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
// 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(),
}
respJSON, err := b.sendFiles("setChatPhoto",
map[string]string{"photo": p.FileLocal}, params)
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
// 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,
}
respJSON, err := b.Raw("setChatStickerSet", params)
2017-11-26 09:15:11 +00:00
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
// DeleteGroupPhoto should be used to just remove group photo.
func (b *Bot) DeleteGroupPhoto(chat *Chat) error {
params := map[string]string{
"chat_id": chat.Recipient(),
}
respJSON, err := b.Raw("deleteGroupPhoto", params)
2017-11-26 09:15:11 +00:00
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
// 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(),
}
respJSON, err := b.Raw("deleteChatStickerSet", params)
2017-11-26 09:15:11 +00:00
if err != nil {
return err
}
return extractOkResponse(respJSON)
}
// 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{
"chat_id": chat.Recipient(),
2016-10-09 19:57:08 +00:00
}
respJSON, 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
}
2017-11-18 10:19:58 +00:00
return extractOkResponse(respJSON)
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.
//
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),
"message_id": messageID,
2016-10-09 20:00:04 +00:00
}
2017-11-26 00:44:32 +00:00
sendOpts := extractOptions(options)
embedSendOptions(params, sendOpts)
2016-10-09 20:00:04 +00:00
respJSON, 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
}
2017-11-26 00:44:32 +00:00
return extractOkResponse(respJSON)
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
2018-06-29 17:29:20 +00:00
respJSON, 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
}
2017-11-26 00:44:32 +00:00
return extractOkResponse(respJSON)
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
respJSON, 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
}
2017-11-18 10:19:58 +00:00
err = json.Unmarshal(respJSON, &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
}
if resp.Result.Type == ChatChannel && resp.Result.Username == "" {
//Channel is Private
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
respJSON, 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
}
Description string `json:"description"`
2016-10-09 20:29:03 +00:00
}
2017-11-18 10:19:58 +00:00
err = json.Unmarshal(respJSON, &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
}
// ChatMemberOf return information about a member of a chat.
2016-10-09 20:16:33 +00:00
//
// Returns a ChatMember object on success.
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
respJSON, err := b.Raw("getChatMember", params)
2016-10-09 20:16:33 +00:00
if err != nil {
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
Result *ChatMember
Description string `json:"description"`
2016-10-09 20:16:33 +00:00
}
2017-11-18 10:19:58 +00:00
err = json.Unmarshal(respJSON, &resp)
2016-10-09 20:16:33 +00:00
if err != nil {
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 {
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
}
// FileURLByID returns direct url for files using FileId which you can get from File object
func (b *Bot) FileURLByID(fileID string) (string, error) {
2017-11-19 15:16:39 +00:00
f, err := b.FileByID(fileID)
if err != nil {
return "", err
}
2016-10-09 19:57:08 +00:00
return "https://api.telegram.org/file/bot" + b.Token + "/" + f.FilePath, nil
}