2016-01-03 23:54:24 +01:00
|
|
|
// Package tgbotapi has functions and types used for interacting with
|
|
|
|
// the Telegram Bot API.
|
2015-06-26 06:26:24 +02:00
|
|
|
package tgbotapi
|
2015-06-25 07:34:05 +02:00
|
|
|
|
2015-09-07 18:20:43 +02:00
|
|
|
import (
|
2015-11-20 11:42:26 +01:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-10-29 22:03:48 +01:00
|
|
|
"io"
|
2015-11-20 11:42:26 +01:00
|
|
|
"io/ioutil"
|
2015-09-07 18:20:43 +02:00
|
|
|
"net/http"
|
2015-11-20 11:42:26 +01:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
2015-11-21 14:33:58 +01:00
|
|
|
"strings"
|
2015-11-21 15:39:19 +01:00
|
|
|
"time"
|
2016-12-15 22:51:58 +01:00
|
|
|
|
|
|
|
"github.com/technoweenie/multipartstreamer"
|
2015-09-07 18:20:43 +02:00
|
|
|
)
|
2015-07-27 00:16:45 +02:00
|
|
|
|
2016-01-03 23:54:24 +01:00
|
|
|
// BotAPI allows you to interact with the Telegram Bot API.
|
2015-06-26 08:53:20 +02:00
|
|
|
type BotAPI struct {
|
2017-01-27 18:40:09 +01:00
|
|
|
Token string `json:"token"`
|
|
|
|
Debug bool `json:"debug"`
|
|
|
|
Buffer int `json:"buffer"`
|
|
|
|
|
2019-05-24 12:14:48 +02:00
|
|
|
Self User `json:"-"`
|
|
|
|
Client *http.Client `json:"-"`
|
2018-03-09 23:22:21 +01:00
|
|
|
shutdownChannel chan interface{}
|
2019-05-24 12:14:48 +02:00
|
|
|
|
|
|
|
apiEndpoint string
|
2015-06-25 07:34:05 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// NewBotAPI creates a new BotAPI instance.
|
2016-01-03 23:54:24 +01:00
|
|
|
//
|
|
|
|
// It requires a token, provided by @BotFather on Telegram.
|
2015-07-28 13:00:31 +02:00
|
|
|
func NewBotAPI(token string) (*BotAPI, error) {
|
2015-07-28 18:18:50 +02:00
|
|
|
return NewBotAPIWithClient(token, &http.Client{})
|
2015-07-28 13:00:31 +02:00
|
|
|
}
|
|
|
|
|
2016-01-03 23:54:24 +01:00
|
|
|
// NewBotAPIWithClient creates a new BotAPI instance
|
|
|
|
// and allows you to pass a http.Client.
|
|
|
|
//
|
|
|
|
// It requires a token, provided by @BotFather on Telegram.
|
2015-07-28 18:14:13 +02:00
|
|
|
func NewBotAPIWithClient(token string, client *http.Client) (*BotAPI, error) {
|
2015-06-26 08:53:20 +02:00
|
|
|
bot := &BotAPI{
|
2019-05-24 12:14:48 +02:00
|
|
|
Token: token,
|
|
|
|
Client: client,
|
|
|
|
Buffer: 100,
|
2018-03-09 23:22:21 +01:00
|
|
|
shutdownChannel: make(chan interface{}),
|
2019-05-24 12:14:48 +02:00
|
|
|
|
|
|
|
apiEndpoint: APIEndpoint,
|
2015-06-25 23:15:28 +02:00
|
|
|
}
|
2015-06-26 06:44:14 +02:00
|
|
|
|
|
|
|
self, err := bot.GetMe()
|
|
|
|
if err != nil {
|
2017-01-27 18:40:09 +01:00
|
|
|
return nil, err
|
2015-06-26 06:44:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bot.Self = self
|
|
|
|
|
2015-06-26 06:45:56 +02:00
|
|
|
return bot, nil
|
2015-06-25 23:15:28 +02:00
|
|
|
}
|
2015-11-20 11:42:26 +01:00
|
|
|
|
2019-05-24 12:14:48 +02:00
|
|
|
func (b *BotAPI) SetAPIEndpoint(apiEndpoint string) {
|
|
|
|
b.apiEndpoint = apiEndpoint
|
|
|
|
}
|
|
|
|
|
2015-11-20 11:42:26 +01:00
|
|
|
// MakeRequest makes a request to a specific endpoint with our token.
|
|
|
|
func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, error) {
|
2019-05-24 12:14:48 +02:00
|
|
|
method := fmt.Sprintf(bot.apiEndpoint, bot.Token, endpoint)
|
2016-01-03 23:54:24 +01:00
|
|
|
|
|
|
|
resp, err := bot.Client.PostForm(method, params)
|
2015-11-20 11:42:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return APIResponse{}, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2017-10-29 22:03:48 +01:00
|
|
|
var apiResp APIResponse
|
|
|
|
bytes, err := bot.decodeAPIResponse(resp.Body, &apiResp)
|
|
|
|
if err != nil {
|
|
|
|
return apiResp, err
|
|
|
|
}
|
2017-10-31 00:34:34 +01:00
|
|
|
|
2017-10-29 22:03:48 +01:00
|
|
|
if bot.Debug {
|
2017-10-31 00:34:34 +01:00
|
|
|
log.Printf("%s resp: %s", endpoint, bytes)
|
2017-10-29 22:03:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if !apiResp.Ok {
|
2018-03-04 12:10:17 +01:00
|
|
|
parameters := ResponseParameters{}
|
|
|
|
if apiResp.Parameters != nil {
|
|
|
|
parameters = *apiResp.Parameters
|
|
|
|
}
|
2019-03-01 17:17:35 +01:00
|
|
|
return apiResp, Error{Code: apiResp.ErrorCode, Message: apiResp.Description, ResponseParameters: parameters}
|
2015-11-20 11:42:26 +01:00
|
|
|
}
|
|
|
|
|
2017-10-29 22:03:48 +01:00
|
|
|
return apiResp, nil
|
|
|
|
}
|
|
|
|
|
2017-11-07 03:17:06 +01:00
|
|
|
// decodeAPIResponse decode response and return slice of bytes if debug enabled.
|
|
|
|
// If debug disabled, just decode http.Response.Body stream to APIResponse struct
|
|
|
|
// for efficient memory usage
|
2017-10-29 22:03:48 +01:00
|
|
|
func (bot *BotAPI) decodeAPIResponse(responseBody io.Reader, resp *APIResponse) (_ []byte, err error) {
|
|
|
|
if !bot.Debug {
|
|
|
|
dec := json.NewDecoder(responseBody)
|
|
|
|
err = dec.Decode(resp)
|
|
|
|
return
|
2015-11-20 11:42:26 +01:00
|
|
|
}
|
|
|
|
|
2017-10-29 22:03:48 +01:00
|
|
|
// if debug, read reponse body
|
|
|
|
data, err := ioutil.ReadAll(responseBody)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2015-11-20 11:42:26 +01:00
|
|
|
|
2017-10-29 22:03:48 +01:00
|
|
|
err = json.Unmarshal(data, resp)
|
|
|
|
if err != nil {
|
|
|
|
return
|
2015-11-20 11:42:26 +01:00
|
|
|
}
|
|
|
|
|
2017-10-31 00:34:34 +01:00
|
|
|
return data, nil
|
2015-11-20 11:42:26 +01:00
|
|
|
}
|
|
|
|
|
2016-01-03 23:54:24 +01:00
|
|
|
// makeMessageRequest makes a request to a method that returns a Message.
|
2015-11-21 17:43:24 +01:00
|
|
|
func (bot *BotAPI) makeMessageRequest(endpoint string, params url.Values) (Message, error) {
|
2015-11-20 12:19:37 +01:00
|
|
|
resp, err := bot.MakeRequest(endpoint, params)
|
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var message Message
|
|
|
|
json.Unmarshal(resp.Result, &message)
|
2015-11-20 13:01:05 +01:00
|
|
|
|
2015-11-20 15:55:32 +01:00
|
|
|
bot.debugLog(endpoint, params, message)
|
2015-11-20 13:01:05 +01:00
|
|
|
|
2015-11-20 12:19:37 +01:00
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
2015-11-20 11:42:26 +01:00
|
|
|
// UploadFile makes a request to the API with a file.
|
|
|
|
//
|
|
|
|
// Requires the parameter to hold the file not be in the params.
|
2016-01-03 23:54:24 +01:00
|
|
|
// File should be a string to a file path, a FileBytes struct,
|
2016-10-03 17:47:19 +02:00
|
|
|
// a FileReader struct, or a url.URL.
|
2016-01-03 23:54:24 +01:00
|
|
|
//
|
|
|
|
// Note that if your FileReader has a size set to -1, it will read
|
|
|
|
// the file into memory to calculate a size.
|
2015-11-20 11:42:26 +01:00
|
|
|
func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldname string, file interface{}) (APIResponse, error) {
|
|
|
|
ms := multipartstreamer.New()
|
|
|
|
|
|
|
|
switch f := file.(type) {
|
|
|
|
case string:
|
2016-10-03 17:47:19 +02:00
|
|
|
ms.WriteFields(params)
|
|
|
|
|
2015-11-20 11:42:26 +01:00
|
|
|
fileHandle, err := os.Open(f)
|
|
|
|
if err != nil {
|
|
|
|
return APIResponse{}, err
|
|
|
|
}
|
|
|
|
defer fileHandle.Close()
|
|
|
|
|
|
|
|
fi, err := os.Stat(f)
|
|
|
|
if err != nil {
|
|
|
|
return APIResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ms.WriteReader(fieldname, fileHandle.Name(), fi.Size(), fileHandle)
|
|
|
|
case FileBytes:
|
2016-10-03 17:47:19 +02:00
|
|
|
ms.WriteFields(params)
|
|
|
|
|
2015-11-20 11:42:26 +01:00
|
|
|
buf := bytes.NewBuffer(f.Bytes)
|
|
|
|
ms.WriteReader(fieldname, f.Name, int64(len(f.Bytes)), buf)
|
|
|
|
case FileReader:
|
2016-10-03 17:47:19 +02:00
|
|
|
ms.WriteFields(params)
|
|
|
|
|
2016-01-01 07:10:19 +01:00
|
|
|
if f.Size != -1 {
|
|
|
|
ms.WriteReader(fieldname, f.Name, f.Size, f.Reader)
|
2015-11-20 11:42:26 +01:00
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2016-01-01 07:10:19 +01:00
|
|
|
data, err := ioutil.ReadAll(f.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return APIResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(data)
|
|
|
|
|
|
|
|
ms.WriteReader(fieldname, f.Name, int64(len(data)), buf)
|
2016-10-03 17:47:19 +02:00
|
|
|
case url.URL:
|
|
|
|
params[fieldname] = f.String()
|
|
|
|
|
|
|
|
ms.WriteFields(params)
|
2015-11-20 11:42:26 +01:00
|
|
|
default:
|
2016-01-04 05:49:58 +01:00
|
|
|
return APIResponse{}, errors.New(ErrBadFileType)
|
2015-11-20 11:42:26 +01:00
|
|
|
}
|
|
|
|
|
2019-05-24 12:14:48 +02:00
|
|
|
method := fmt.Sprintf(bot.apiEndpoint, bot.Token, endpoint)
|
2016-01-03 23:54:24 +01:00
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", method, nil)
|
2015-11-20 11:42:26 +01:00
|
|
|
if err != nil {
|
|
|
|
return APIResponse{}, err
|
|
|
|
}
|
|
|
|
|
2016-01-01 07:10:19 +01:00
|
|
|
ms.SetupRequest(req)
|
|
|
|
|
2015-11-20 11:42:26 +01:00
|
|
|
res, err := bot.Client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return APIResponse{}, err
|
|
|
|
}
|
|
|
|
defer res.Body.Close()
|
|
|
|
|
|
|
|
bytes, err := ioutil.ReadAll(res.Body)
|
|
|
|
if err != nil {
|
|
|
|
return APIResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if bot.Debug {
|
2016-01-01 07:10:19 +01:00
|
|
|
log.Println(string(bytes))
|
2015-11-20 11:42:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var apiResp APIResponse
|
2017-01-18 10:05:01 +01:00
|
|
|
|
|
|
|
err = json.Unmarshal(bytes, &apiResp)
|
|
|
|
if err != nil {
|
|
|
|
return APIResponse{}, err
|
|
|
|
}
|
2015-11-20 11:42:26 +01:00
|
|
|
|
|
|
|
if !apiResp.Ok {
|
|
|
|
return APIResponse{}, errors.New(apiResp.Description)
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiResp, nil
|
|
|
|
}
|
|
|
|
|
2015-11-21 17:43:24 +01:00
|
|
|
// GetFileDirectURL returns direct URL to file
|
|
|
|
//
|
2016-01-03 23:54:24 +01:00
|
|
|
// It requires the FileID.
|
2015-11-21 17:43:24 +01:00
|
|
|
func (bot *BotAPI) GetFileDirectURL(fileID string) (string, error) {
|
|
|
|
file, err := bot.GetFile(FileConfig{fileID})
|
2015-11-21 14:25:59 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2015-11-21 17:43:24 +01:00
|
|
|
return file.Link(bot.Token), nil
|
2015-11-21 14:25:59 +01:00
|
|
|
}
|
|
|
|
|
2015-11-20 11:42:26 +01:00
|
|
|
// GetMe fetches the currently authenticated bot.
|
|
|
|
//
|
2016-01-03 23:54:24 +01:00
|
|
|
// This method is called upon creation to validate the token,
|
|
|
|
// and so you may get this data from BotAPI.Self without the need for
|
|
|
|
// another request.
|
2015-11-20 11:42:26 +01:00
|
|
|
func (bot *BotAPI) GetMe() (User, error) {
|
|
|
|
resp, err := bot.MakeRequest("getMe", nil)
|
|
|
|
if err != nil {
|
|
|
|
return User{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var user User
|
|
|
|
json.Unmarshal(resp.Result, &user)
|
|
|
|
|
2016-01-01 07:10:19 +01:00
|
|
|
bot.debugLog("getMe", nil, user)
|
2015-11-20 11:42:26 +01:00
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
2016-01-03 23:54:24 +01:00
|
|
|
// IsMessageToMe returns true if message directed to this bot.
|
2015-11-21 17:43:24 +01:00
|
|
|
//
|
2016-01-03 23:54:24 +01:00
|
|
|
// It requires the Message.
|
2015-11-21 15:39:19 +01:00
|
|
|
func (bot *BotAPI) IsMessageToMe(message Message) bool {
|
2015-11-21 14:33:58 +01:00
|
|
|
return strings.Contains(message.Text, "@"+bot.Self.UserName)
|
|
|
|
}
|
|
|
|
|
2016-01-03 23:54:24 +01:00
|
|
|
// Send will send a Chattable item to Telegram.
|
2015-11-21 17:43:24 +01:00
|
|
|
//
|
2016-01-03 23:54:24 +01:00
|
|
|
// It requires the Chattable to send.
|
2015-11-20 15:55:32 +01:00
|
|
|
func (bot *BotAPI) Send(c Chattable) (Message, error) {
|
|
|
|
switch c.(type) {
|
|
|
|
case Fileable:
|
|
|
|
return bot.sendFile(c.(Fileable))
|
|
|
|
default:
|
|
|
|
return bot.sendChattable(c)
|
|
|
|
}
|
2015-11-20 11:42:26 +01:00
|
|
|
}
|
|
|
|
|
2016-01-03 23:54:24 +01:00
|
|
|
// debugLog checks if the bot is currently running in debug mode, and if
|
|
|
|
// so will display information about the request and response in the
|
|
|
|
// debug log.
|
2015-11-20 15:55:32 +01:00
|
|
|
func (bot *BotAPI) debugLog(context string, v url.Values, message interface{}) {
|
2015-11-20 12:50:23 +01:00
|
|
|
if bot.Debug {
|
|
|
|
log.Printf("%s req : %+v\n", context, v)
|
|
|
|
log.Printf("%s resp: %+v\n", context, message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-03 23:54:24 +01:00
|
|
|
// sendExisting will send a Message with an existing file to Telegram.
|
2015-11-20 15:08:53 +01:00
|
|
|
func (bot *BotAPI) sendExisting(method string, config Fileable) (Message, error) {
|
2016-01-03 23:54:24 +01:00
|
|
|
v, err := config.values()
|
2015-11-20 15:08:53 +01:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
2015-11-21 17:43:24 +01:00
|
|
|
message, err := bot.makeMessageRequest(method, v)
|
2015-11-20 15:08:53 +01:00
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
2016-01-03 23:54:24 +01:00
|
|
|
// uploadAndSend will send a Message with a new file to Telegram.
|
2015-11-20 15:31:01 +01:00
|
|
|
func (bot *BotAPI) uploadAndSend(method string, config Fileable) (Message, error) {
|
2016-01-03 23:54:24 +01:00
|
|
|
params, err := config.params()
|
2015-11-20 15:31:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
2016-01-03 23:54:24 +01:00
|
|
|
file := config.getFile()
|
2015-11-20 15:31:01 +01:00
|
|
|
|
2016-01-03 23:54:24 +01:00
|
|
|
resp, err := bot.UploadFile(method, params, config.name(), file)
|
2015-11-20 15:31:01 +01:00
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var message Message
|
|
|
|
json.Unmarshal(resp.Result, &message)
|
|
|
|
|
2016-01-03 23:54:24 +01:00
|
|
|
bot.debugLog(method, nil, message)
|
2015-11-20 15:31:01 +01:00
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
2016-01-03 23:54:24 +01:00
|
|
|
// sendFile determines if the file is using an existing file or uploading
|
|
|
|
// a new file, then sends it as needed.
|
2015-11-20 15:55:32 +01:00
|
|
|
func (bot *BotAPI) sendFile(config Fileable) (Message, error) {
|
2016-01-03 23:54:24 +01:00
|
|
|
if config.useExistingFile() {
|
|
|
|
return bot.sendExisting(config.method(), config)
|
2015-11-20 15:31:01 +01:00
|
|
|
}
|
|
|
|
|
2016-01-03 23:54:24 +01:00
|
|
|
return bot.uploadAndSend(config.method(), config)
|
2015-11-20 15:31:01 +01:00
|
|
|
}
|
|
|
|
|
2016-01-03 23:54:24 +01:00
|
|
|
// sendChattable sends a Chattable.
|
2015-11-20 15:55:32 +01:00
|
|
|
func (bot *BotAPI) sendChattable(config Chattable) (Message, error) {
|
2016-01-03 23:54:24 +01:00
|
|
|
v, err := config.values()
|
2015-11-20 12:06:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
2015-11-20 11:42:26 +01:00
|
|
|
}
|
|
|
|
|
2016-01-03 23:54:24 +01:00
|
|
|
message, err := bot.makeMessageRequest(config.method(), v)
|
2015-11-20 11:42:26 +01:00
|
|
|
|
2015-11-20 15:08:53 +01:00
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
2015-11-20 11:42:26 +01:00
|
|
|
// GetUserProfilePhotos gets a user's profile photos.
|
|
|
|
//
|
2016-01-03 23:54:24 +01:00
|
|
|
// It requires UserID.
|
2015-11-20 11:42:26 +01:00
|
|
|
// Offset and Limit are optional.
|
|
|
|
func (bot *BotAPI) GetUserProfilePhotos(config UserProfilePhotosConfig) (UserProfilePhotos, error) {
|
|
|
|
v := url.Values{}
|
|
|
|
v.Add("user_id", strconv.Itoa(config.UserID))
|
|
|
|
if config.Offset != 0 {
|
|
|
|
v.Add("offset", strconv.Itoa(config.Offset))
|
|
|
|
}
|
|
|
|
if config.Limit != 0 {
|
|
|
|
v.Add("limit", strconv.Itoa(config.Limit))
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := bot.MakeRequest("getUserProfilePhotos", v)
|
|
|
|
if err != nil {
|
|
|
|
return UserProfilePhotos{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var profilePhotos UserProfilePhotos
|
|
|
|
json.Unmarshal(resp.Result, &profilePhotos)
|
|
|
|
|
2015-11-20 15:55:32 +01:00
|
|
|
bot.debugLog("GetUserProfilePhoto", v, profilePhotos)
|
2015-11-20 11:42:26 +01:00
|
|
|
|
|
|
|
return profilePhotos, nil
|
|
|
|
}
|
|
|
|
|
2016-01-03 23:54:24 +01:00
|
|
|
// GetFile returns a File which can download a file from Telegram.
|
2015-11-20 11:42:26 +01:00
|
|
|
//
|
|
|
|
// Requires FileID.
|
|
|
|
func (bot *BotAPI) GetFile(config FileConfig) (File, error) {
|
|
|
|
v := url.Values{}
|
|
|
|
v.Add("file_id", config.FileID)
|
|
|
|
|
|
|
|
resp, err := bot.MakeRequest("getFile", v)
|
|
|
|
if err != nil {
|
|
|
|
return File{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var file File
|
|
|
|
json.Unmarshal(resp.Result, &file)
|
|
|
|
|
2015-11-20 15:55:32 +01:00
|
|
|
bot.debugLog("GetFile", v, file)
|
2015-11-20 11:42:26 +01:00
|
|
|
|
|
|
|
return file, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUpdates fetches updates.
|
|
|
|
// If a WebHook is set, this will not return any data!
|
|
|
|
//
|
|
|
|
// Offset, Limit, and Timeout are optional.
|
2016-01-03 23:54:24 +01:00
|
|
|
// To avoid stale items, set Offset to one higher than the previous item.
|
|
|
|
// Set Timeout to a large number to reduce requests so you can get updates
|
|
|
|
// instantly instead of having to wait between requests.
|
2015-11-20 11:42:26 +01:00
|
|
|
func (bot *BotAPI) GetUpdates(config UpdateConfig) ([]Update, error) {
|
|
|
|
v := url.Values{}
|
2016-06-29 19:37:30 +02:00
|
|
|
if config.Offset != 0 {
|
2015-11-20 11:42:26 +01:00
|
|
|
v.Add("offset", strconv.Itoa(config.Offset))
|
|
|
|
}
|
|
|
|
if config.Limit > 0 {
|
|
|
|
v.Add("limit", strconv.Itoa(config.Limit))
|
|
|
|
}
|
|
|
|
if config.Timeout > 0 {
|
|
|
|
v.Add("timeout", strconv.Itoa(config.Timeout))
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := bot.MakeRequest("getUpdates", v)
|
|
|
|
if err != nil {
|
|
|
|
return []Update{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var updates []Update
|
|
|
|
json.Unmarshal(resp.Result, &updates)
|
|
|
|
|
2016-01-03 23:54:24 +01:00
|
|
|
bot.debugLog("getUpdates", v, updates)
|
2015-11-20 11:42:26 +01:00
|
|
|
|
|
|
|
return updates, nil
|
|
|
|
}
|
|
|
|
|
2016-01-03 23:54:24 +01:00
|
|
|
// RemoveWebhook unsets the webhook.
|
2015-11-21 12:26:39 +01:00
|
|
|
func (bot *BotAPI) RemoveWebhook() (APIResponse, error) {
|
|
|
|
return bot.MakeRequest("setWebhook", url.Values{})
|
|
|
|
}
|
|
|
|
|
2015-11-20 11:42:26 +01:00
|
|
|
// SetWebhook sets a webhook.
|
2016-01-03 23:54:24 +01:00
|
|
|
//
|
2015-11-20 11:42:26 +01:00
|
|
|
// If this is set, GetUpdates will not get any data!
|
|
|
|
//
|
2016-07-26 20:44:48 +02:00
|
|
|
// If you do not have a legitimate TLS certificate, you need to include
|
2016-01-03 23:54:24 +01:00
|
|
|
// your self signed certificate with the config.
|
2015-11-20 11:42:26 +01:00
|
|
|
func (bot *BotAPI) SetWebhook(config WebhookConfig) (APIResponse, error) {
|
2017-01-17 15:18:26 +01:00
|
|
|
|
2015-11-20 11:42:26 +01:00
|
|
|
if config.Certificate == nil {
|
|
|
|
v := url.Values{}
|
2015-11-21 12:26:39 +01:00
|
|
|
v.Add("url", config.URL.String())
|
2017-01-17 15:18:26 +01:00
|
|
|
if config.MaxConnections != 0 {
|
|
|
|
v.Add("max_connections", strconv.Itoa(config.MaxConnections))
|
|
|
|
}
|
2015-11-20 11:42:26 +01:00
|
|
|
|
|
|
|
return bot.MakeRequest("setWebhook", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
params := make(map[string]string)
|
|
|
|
params["url"] = config.URL.String()
|
2017-01-17 15:18:26 +01:00
|
|
|
if config.MaxConnections != 0 {
|
|
|
|
params["max_connections"] = strconv.Itoa(config.MaxConnections)
|
|
|
|
}
|
2015-11-20 11:42:26 +01:00
|
|
|
|
|
|
|
resp, err := bot.UploadFile("setWebhook", params, "certificate", config.Certificate)
|
|
|
|
if err != nil {
|
|
|
|
return APIResponse{}, err
|
|
|
|
}
|
|
|
|
|
2017-01-18 10:05:01 +01:00
|
|
|
return resp, nil
|
2015-11-20 11:42:26 +01:00
|
|
|
}
|
|
|
|
|
2016-10-03 17:47:19 +02:00
|
|
|
// GetWebhookInfo allows you to fetch information about a webhook and if
|
|
|
|
// one currently is set, along with pending update count and error messages.
|
|
|
|
func (bot *BotAPI) GetWebhookInfo() (WebhookInfo, error) {
|
|
|
|
resp, err := bot.MakeRequest("getWebhookInfo", url.Values{})
|
|
|
|
if err != nil {
|
|
|
|
return WebhookInfo{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var info WebhookInfo
|
|
|
|
err = json.Unmarshal(resp.Result, &info)
|
|
|
|
|
|
|
|
return info, err
|
|
|
|
}
|
|
|
|
|
2015-11-21 17:43:24 +01:00
|
|
|
// GetUpdatesChan starts and returns a channel for getting updates.
|
2017-01-26 18:43:28 +01:00
|
|
|
func (bot *BotAPI) GetUpdatesChan(config UpdateConfig) (UpdatesChannel, error) {
|
2017-01-27 18:40:09 +01:00
|
|
|
ch := make(chan Update, bot.Buffer)
|
2015-11-20 11:42:26 +01:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
2018-03-09 23:22:21 +01:00
|
|
|
select {
|
|
|
|
case <-bot.shutdownChannel:
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
2019-05-24 12:14:48 +02:00
|
|
|
|
2015-11-20 11:42:26 +01:00
|
|
|
updates, err := bot.GetUpdates(config)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
log.Println("Failed to get updates, retrying in 3 seconds...")
|
|
|
|
time.Sleep(time.Second * 3)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, update := range updates {
|
2018-10-08 20:58:50 +02:00
|
|
|
if update.UpdateID >= config.Offset {
|
|
|
|
config.Offset = update.UpdateID + 1
|
|
|
|
ch <- update
|
|
|
|
}
|
2015-11-20 11:42:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-12-16 02:07:54 +01:00
|
|
|
return ch, nil
|
2015-11-20 11:42:26 +01:00
|
|
|
}
|
|
|
|
|
2018-03-09 23:22:21 +01:00
|
|
|
// StopReceivingUpdates stops the go routine which receives updates
|
|
|
|
func (bot *BotAPI) StopReceivingUpdates() {
|
|
|
|
if bot.Debug {
|
|
|
|
log.Println("Stopping the update receiver routine...")
|
|
|
|
}
|
|
|
|
close(bot.shutdownChannel)
|
|
|
|
}
|
|
|
|
|
2015-11-20 11:42:26 +01:00
|
|
|
// ListenForWebhook registers a http handler for a webhook.
|
2017-01-26 18:43:28 +01:00
|
|
|
func (bot *BotAPI) ListenForWebhook(pattern string) UpdatesChannel {
|
2017-01-27 18:40:09 +01:00
|
|
|
ch := make(chan Update, bot.Buffer)
|
2015-11-20 11:42:26 +01:00
|
|
|
|
2016-01-04 06:05:36 +01:00
|
|
|
http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
|
2019-09-18 02:34:37 +02:00
|
|
|
ch <- bot.HandleUpdate(w, r)
|
2015-11-20 11:42:26 +01:00
|
|
|
})
|
2015-11-21 12:22:08 +01:00
|
|
|
|
2016-12-16 02:07:54 +01:00
|
|
|
return ch
|
2015-11-20 11:42:26 +01:00
|
|
|
}
|
2016-01-01 07:10:19 +01:00
|
|
|
|
2019-09-18 02:34:37 +02:00
|
|
|
// HandleUpdate parses and returns update received via webhook
|
|
|
|
func (bot *BotAPI) HandleUpdate(res http.ResponseWriter, req *http.Request) Update {
|
|
|
|
bytes, _ := ioutil.ReadAll(req.Body)
|
|
|
|
req.Body.Close()
|
|
|
|
|
|
|
|
var update Update
|
|
|
|
json.Unmarshal(bytes, &update)
|
|
|
|
|
|
|
|
return update
|
|
|
|
}
|
|
|
|
|
2016-01-01 07:10:19 +01:00
|
|
|
// AnswerInlineQuery sends a response to an inline query.
|
2016-01-03 23:54:24 +01:00
|
|
|
//
|
|
|
|
// Note that you must respond to an inline query within 30 seconds.
|
2016-01-01 07:10:19 +01:00
|
|
|
func (bot *BotAPI) AnswerInlineQuery(config InlineConfig) (APIResponse, error) {
|
|
|
|
v := url.Values{}
|
|
|
|
|
|
|
|
v.Add("inline_query_id", config.InlineQueryID)
|
|
|
|
v.Add("cache_time", strconv.Itoa(config.CacheTime))
|
|
|
|
v.Add("is_personal", strconv.FormatBool(config.IsPersonal))
|
|
|
|
v.Add("next_offset", config.NextOffset)
|
|
|
|
data, err := json.Marshal(config.Results)
|
|
|
|
if err != nil {
|
|
|
|
return APIResponse{}, err
|
|
|
|
}
|
|
|
|
v.Add("results", string(data))
|
2016-04-12 15:28:46 +02:00
|
|
|
v.Add("switch_pm_text", config.SwitchPMText)
|
|
|
|
v.Add("switch_pm_parameter", config.SwitchPMParameter)
|
2016-01-01 07:10:19 +01:00
|
|
|
|
2016-01-18 06:34:12 +01:00
|
|
|
bot.debugLog("answerInlineQuery", v, nil)
|
|
|
|
|
2016-01-01 07:10:19 +01:00
|
|
|
return bot.MakeRequest("answerInlineQuery", v)
|
|
|
|
}
|
2016-04-12 15:28:46 +02:00
|
|
|
|
|
|
|
// AnswerCallbackQuery sends a response to an inline query callback.
|
|
|
|
func (bot *BotAPI) AnswerCallbackQuery(config CallbackConfig) (APIResponse, error) {
|
|
|
|
v := url.Values{}
|
|
|
|
|
|
|
|
v.Add("callback_query_id", config.CallbackQueryID)
|
2016-10-03 17:47:19 +02:00
|
|
|
if config.Text != "" {
|
|
|
|
v.Add("text", config.Text)
|
|
|
|
}
|
2016-04-12 15:28:46 +02:00
|
|
|
v.Add("show_alert", strconv.FormatBool(config.ShowAlert))
|
2016-10-03 17:47:19 +02:00
|
|
|
if config.URL != "" {
|
|
|
|
v.Add("url", config.URL)
|
|
|
|
}
|
2016-11-25 06:50:35 +01:00
|
|
|
v.Add("cache_time", strconv.Itoa(config.CacheTime))
|
2016-04-12 15:28:46 +02:00
|
|
|
|
|
|
|
bot.debugLog("answerCallbackQuery", v, nil)
|
|
|
|
|
|
|
|
return bot.MakeRequest("answerCallbackQuery", v)
|
|
|
|
}
|
2016-04-12 15:56:05 +02:00
|
|
|
|
|
|
|
// KickChatMember kicks a user from a chat. Note that this only will work
|
|
|
|
// in supergroups, and requires the bot to be an admin. Also note they
|
|
|
|
// will be unable to rejoin until they are unbanned.
|
2017-07-01 06:34:09 +02:00
|
|
|
func (bot *BotAPI) KickChatMember(config KickChatMemberConfig) (APIResponse, error) {
|
2016-04-12 15:56:05 +02:00
|
|
|
v := url.Values{}
|
|
|
|
|
|
|
|
if config.SuperGroupUsername == "" {
|
|
|
|
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10))
|
|
|
|
} else {
|
|
|
|
v.Add("chat_id", config.SuperGroupUsername)
|
|
|
|
}
|
|
|
|
v.Add("user_id", strconv.Itoa(config.UserID))
|
|
|
|
|
2017-07-01 06:34:09 +02:00
|
|
|
if config.UntilDate != 0 {
|
|
|
|
v.Add("until_date", strconv.FormatInt(config.UntilDate, 10))
|
|
|
|
}
|
|
|
|
|
2016-04-12 15:56:05 +02:00
|
|
|
bot.debugLog("kickChatMember", v, nil)
|
|
|
|
|
|
|
|
return bot.MakeRequest("kickChatMember", v)
|
|
|
|
}
|
|
|
|
|
2016-05-22 17:16:28 +02:00
|
|
|
// LeaveChat makes the bot leave the chat.
|
|
|
|
func (bot *BotAPI) LeaveChat(config ChatConfig) (APIResponse, error) {
|
|
|
|
v := url.Values{}
|
|
|
|
|
|
|
|
if config.SuperGroupUsername == "" {
|
|
|
|
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10))
|
|
|
|
} else {
|
|
|
|
v.Add("chat_id", config.SuperGroupUsername)
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.debugLog("leaveChat", v, nil)
|
|
|
|
|
|
|
|
return bot.MakeRequest("leaveChat", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetChat gets information about a chat.
|
|
|
|
func (bot *BotAPI) GetChat(config ChatConfig) (Chat, error) {
|
|
|
|
v := url.Values{}
|
|
|
|
|
|
|
|
if config.SuperGroupUsername == "" {
|
|
|
|
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10))
|
|
|
|
} else {
|
|
|
|
v.Add("chat_id", config.SuperGroupUsername)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := bot.MakeRequest("getChat", v)
|
|
|
|
if err != nil {
|
|
|
|
return Chat{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var chat Chat
|
|
|
|
err = json.Unmarshal(resp.Result, &chat)
|
|
|
|
|
|
|
|
bot.debugLog("getChat", v, chat)
|
|
|
|
|
|
|
|
return chat, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetChatAdministrators gets a list of administrators in the chat.
|
|
|
|
//
|
|
|
|
// If none have been appointed, only the creator will be returned.
|
|
|
|
// Bots are not shown, even if they are an administrator.
|
|
|
|
func (bot *BotAPI) GetChatAdministrators(config ChatConfig) ([]ChatMember, error) {
|
|
|
|
v := url.Values{}
|
|
|
|
|
|
|
|
if config.SuperGroupUsername == "" {
|
|
|
|
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10))
|
|
|
|
} else {
|
|
|
|
v.Add("chat_id", config.SuperGroupUsername)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := bot.MakeRequest("getChatAdministrators", v)
|
|
|
|
if err != nil {
|
|
|
|
return []ChatMember{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var members []ChatMember
|
|
|
|
err = json.Unmarshal(resp.Result, &members)
|
|
|
|
|
|
|
|
bot.debugLog("getChatAdministrators", v, members)
|
|
|
|
|
|
|
|
return members, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetChatMembersCount gets the number of users in a chat.
|
|
|
|
func (bot *BotAPI) GetChatMembersCount(config ChatConfig) (int, error) {
|
|
|
|
v := url.Values{}
|
|
|
|
|
|
|
|
if config.SuperGroupUsername == "" {
|
|
|
|
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10))
|
|
|
|
} else {
|
|
|
|
v.Add("chat_id", config.SuperGroupUsername)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := bot.MakeRequest("getChatMembersCount", v)
|
|
|
|
if err != nil {
|
|
|
|
return -1, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var count int
|
|
|
|
err = json.Unmarshal(resp.Result, &count)
|
|
|
|
|
|
|
|
bot.debugLog("getChatMembersCount", v, count)
|
|
|
|
|
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetChatMember gets a specific chat member.
|
|
|
|
func (bot *BotAPI) GetChatMember(config ChatConfigWithUser) (ChatMember, error) {
|
|
|
|
v := url.Values{}
|
|
|
|
|
|
|
|
if config.SuperGroupUsername == "" {
|
|
|
|
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10))
|
|
|
|
} else {
|
|
|
|
v.Add("chat_id", config.SuperGroupUsername)
|
|
|
|
}
|
|
|
|
v.Add("user_id", strconv.Itoa(config.UserID))
|
|
|
|
|
|
|
|
resp, err := bot.MakeRequest("getChatMember", v)
|
|
|
|
if err != nil {
|
|
|
|
return ChatMember{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var member ChatMember
|
|
|
|
err = json.Unmarshal(resp.Result, &member)
|
|
|
|
|
|
|
|
bot.debugLog("getChatMember", v, member)
|
|
|
|
|
|
|
|
return member, err
|
|
|
|
}
|
|
|
|
|
2016-04-12 15:56:05 +02:00
|
|
|
// UnbanChatMember unbans a user from a chat. Note that this only will work
|
2017-06-18 18:59:25 +02:00
|
|
|
// in supergroups and channels, and requires the bot to be an admin.
|
2016-04-12 15:56:05 +02:00
|
|
|
func (bot *BotAPI) UnbanChatMember(config ChatMemberConfig) (APIResponse, error) {
|
|
|
|
v := url.Values{}
|
|
|
|
|
2017-06-18 18:59:25 +02:00
|
|
|
if config.SuperGroupUsername != "" {
|
2016-04-12 15:56:05 +02:00
|
|
|
v.Add("chat_id", config.SuperGroupUsername)
|
2017-06-18 18:59:25 +02:00
|
|
|
} else if config.ChannelUsername != "" {
|
|
|
|
v.Add("chat_id", config.ChannelUsername)
|
|
|
|
} else {
|
|
|
|
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10))
|
2016-04-12 15:56:05 +02:00
|
|
|
}
|
|
|
|
v.Add("user_id", strconv.Itoa(config.UserID))
|
|
|
|
|
|
|
|
bot.debugLog("unbanChatMember", v, nil)
|
|
|
|
|
|
|
|
return bot.MakeRequest("unbanChatMember", v)
|
|
|
|
}
|
2016-10-03 17:47:19 +02:00
|
|
|
|
2017-07-01 06:34:09 +02:00
|
|
|
// RestrictChatMember to restrict a user in a supergroup. The bot must be an
|
|
|
|
//administrator in the supergroup for this to work and must have the
|
|
|
|
//appropriate admin rights. Pass True for all boolean parameters to lift
|
|
|
|
//restrictions from a user. Returns True on success.
|
|
|
|
func (bot *BotAPI) RestrictChatMember(config RestrictChatMemberConfig) (APIResponse, error) {
|
|
|
|
v := url.Values{}
|
|
|
|
|
|
|
|
if config.SuperGroupUsername != "" {
|
|
|
|
v.Add("chat_id", config.SuperGroupUsername)
|
|
|
|
} else if config.ChannelUsername != "" {
|
|
|
|
v.Add("chat_id", config.ChannelUsername)
|
|
|
|
} else {
|
|
|
|
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10))
|
|
|
|
}
|
|
|
|
v.Add("user_id", strconv.Itoa(config.UserID))
|
|
|
|
|
2018-03-28 15:10:29 +02:00
|
|
|
if config.CanSendMessages != nil {
|
2017-07-01 06:34:09 +02:00
|
|
|
v.Add("can_send_messages", strconv.FormatBool(*config.CanSendMessages))
|
|
|
|
}
|
2018-03-28 15:10:29 +02:00
|
|
|
if config.CanSendMediaMessages != nil {
|
2017-07-01 06:34:09 +02:00
|
|
|
v.Add("can_send_media_messages", strconv.FormatBool(*config.CanSendMediaMessages))
|
|
|
|
}
|
2018-03-28 15:10:29 +02:00
|
|
|
if config.CanSendOtherMessages != nil {
|
2017-07-01 06:34:09 +02:00
|
|
|
v.Add("can_send_other_messages", strconv.FormatBool(*config.CanSendOtherMessages))
|
|
|
|
}
|
2018-03-28 15:10:29 +02:00
|
|
|
if config.CanAddWebPagePreviews != nil {
|
2017-07-01 06:34:09 +02:00
|
|
|
v.Add("can_add_web_page_previews", strconv.FormatBool(*config.CanAddWebPagePreviews))
|
|
|
|
}
|
2017-11-09 16:41:22 +01:00
|
|
|
if config.UntilDate != 0 {
|
|
|
|
v.Add("until_date", strconv.FormatInt(config.UntilDate, 10))
|
|
|
|
}
|
2017-07-01 06:34:09 +02:00
|
|
|
|
|
|
|
bot.debugLog("restrictChatMember", v, nil)
|
|
|
|
|
|
|
|
return bot.MakeRequest("restrictChatMember", v)
|
|
|
|
}
|
|
|
|
|
2017-11-09 19:51:50 +01:00
|
|
|
// PromoteChatMember add admin rights to user
|
2017-07-01 06:34:09 +02:00
|
|
|
func (bot *BotAPI) PromoteChatMember(config PromoteChatMemberConfig) (APIResponse, error) {
|
|
|
|
v := url.Values{}
|
|
|
|
|
|
|
|
if config.SuperGroupUsername != "" {
|
|
|
|
v.Add("chat_id", config.SuperGroupUsername)
|
|
|
|
} else if config.ChannelUsername != "" {
|
|
|
|
v.Add("chat_id", config.ChannelUsername)
|
|
|
|
} else {
|
|
|
|
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10))
|
|
|
|
}
|
|
|
|
v.Add("user_id", strconv.Itoa(config.UserID))
|
|
|
|
|
2018-03-28 15:10:29 +02:00
|
|
|
if config.CanChangeInfo != nil {
|
2017-07-01 06:34:09 +02:00
|
|
|
v.Add("can_change_info", strconv.FormatBool(*config.CanChangeInfo))
|
|
|
|
}
|
2018-03-28 15:10:29 +02:00
|
|
|
if config.CanPostMessages != nil {
|
2017-07-01 06:34:09 +02:00
|
|
|
v.Add("can_post_messages", strconv.FormatBool(*config.CanPostMessages))
|
|
|
|
}
|
2018-03-28 15:10:29 +02:00
|
|
|
if config.CanEditMessages != nil {
|
2017-07-01 06:34:09 +02:00
|
|
|
v.Add("can_edit_messages", strconv.FormatBool(*config.CanEditMessages))
|
|
|
|
}
|
2018-03-28 15:10:29 +02:00
|
|
|
if config.CanDeleteMessages != nil {
|
2017-07-01 06:34:09 +02:00
|
|
|
v.Add("can_delete_messages", strconv.FormatBool(*config.CanDeleteMessages))
|
|
|
|
}
|
2018-03-28 15:10:29 +02:00
|
|
|
if config.CanInviteUsers != nil {
|
2017-07-01 06:34:09 +02:00
|
|
|
v.Add("can_invite_users", strconv.FormatBool(*config.CanInviteUsers))
|
|
|
|
}
|
2018-03-28 15:10:29 +02:00
|
|
|
if config.CanRestrictMembers != nil {
|
2017-07-01 06:34:09 +02:00
|
|
|
v.Add("can_restrict_members", strconv.FormatBool(*config.CanRestrictMembers))
|
|
|
|
}
|
2018-03-28 15:10:29 +02:00
|
|
|
if config.CanPinMessages != nil {
|
2017-07-01 06:34:09 +02:00
|
|
|
v.Add("can_pin_messages", strconv.FormatBool(*config.CanPinMessages))
|
|
|
|
}
|
2018-03-28 15:10:29 +02:00
|
|
|
if config.CanPromoteMembers != nil {
|
2017-07-01 06:34:09 +02:00
|
|
|
v.Add("can_promote_members", strconv.FormatBool(*config.CanPromoteMembers))
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.debugLog("promoteChatMember", v, nil)
|
|
|
|
|
|
|
|
return bot.MakeRequest("promoteChatMember", v)
|
|
|
|
}
|
|
|
|
|
2016-10-03 17:47:19 +02:00
|
|
|
// GetGameHighScores allows you to get the high scores for a game.
|
|
|
|
func (bot *BotAPI) GetGameHighScores(config GetGameHighScoresConfig) ([]GameHighScore, error) {
|
|
|
|
v, _ := config.values()
|
|
|
|
|
|
|
|
resp, err := bot.MakeRequest(config.method(), v)
|
|
|
|
if err != nil {
|
|
|
|
return []GameHighScore{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var highScores []GameHighScore
|
|
|
|
err = json.Unmarshal(resp.Result, &highScores)
|
|
|
|
|
|
|
|
return highScores, err
|
|
|
|
}
|
2017-06-02 22:18:42 +02:00
|
|
|
|
2017-06-03 08:59:40 +02:00
|
|
|
// AnswerShippingQuery allows you to reply to Update with shipping_query parameter.
|
2017-06-02 22:18:42 +02:00
|
|
|
func (bot *BotAPI) AnswerShippingQuery(config ShippingConfig) (APIResponse, error) {
|
|
|
|
v := url.Values{}
|
|
|
|
|
|
|
|
v.Add("shipping_query_id", config.ShippingQueryID)
|
2017-06-03 08:59:40 +02:00
|
|
|
v.Add("ok", strconv.FormatBool(config.OK))
|
|
|
|
if config.OK == true {
|
2017-06-02 22:18:42 +02:00
|
|
|
data, err := json.Marshal(config.ShippingOptions)
|
|
|
|
if err != nil {
|
|
|
|
return APIResponse{}, err
|
|
|
|
}
|
|
|
|
v.Add("shipping_options", string(data))
|
|
|
|
} else {
|
|
|
|
v.Add("error_message", config.ErrorMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.debugLog("answerShippingQuery", v, nil)
|
|
|
|
|
|
|
|
return bot.MakeRequest("answerShippingQuery", v)
|
|
|
|
}
|
|
|
|
|
2017-06-03 08:59:40 +02:00
|
|
|
// AnswerPreCheckoutQuery allows you to reply to Update with pre_checkout_query.
|
2017-06-02 22:18:42 +02:00
|
|
|
func (bot *BotAPI) AnswerPreCheckoutQuery(config PreCheckoutConfig) (APIResponse, error) {
|
|
|
|
v := url.Values{}
|
|
|
|
|
|
|
|
v.Add("pre_checkout_query_id", config.PreCheckoutQueryID)
|
2017-06-03 08:59:40 +02:00
|
|
|
v.Add("ok", strconv.FormatBool(config.OK))
|
|
|
|
if config.OK != true {
|
2017-06-02 22:18:42 +02:00
|
|
|
v.Add("error", config.ErrorMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.debugLog("answerPreCheckoutQuery", v, nil)
|
|
|
|
|
|
|
|
return bot.MakeRequest("answerPreCheckoutQuery", v)
|
|
|
|
}
|
2017-06-08 11:32:05 +02:00
|
|
|
|
|
|
|
// DeleteMessage deletes a message in a chat
|
|
|
|
func (bot *BotAPI) DeleteMessage(config DeleteMessageConfig) (APIResponse, error) {
|
|
|
|
v, err := config.values()
|
|
|
|
if err != nil {
|
|
|
|
return APIResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.debugLog(config.method(), v, nil)
|
|
|
|
|
|
|
|
return bot.MakeRequest(config.method(), v)
|
|
|
|
}
|
2017-07-17 19:48:51 +02:00
|
|
|
|
|
|
|
// GetInviteLink get InviteLink for a chat
|
|
|
|
func (bot *BotAPI) GetInviteLink(config ChatConfig) (string, error) {
|
2017-08-05 11:29:06 +02:00
|
|
|
v := url.Values{}
|
2017-07-17 19:48:51 +02:00
|
|
|
|
2017-08-05 11:29:06 +02:00
|
|
|
if config.SuperGroupUsername == "" {
|
|
|
|
v.Add("chat_id", strconv.FormatInt(config.ChatID, 10))
|
|
|
|
} else {
|
|
|
|
v.Add("chat_id", config.SuperGroupUsername)
|
|
|
|
}
|
2017-07-17 19:48:51 +02:00
|
|
|
|
2017-08-05 11:29:06 +02:00
|
|
|
resp, err := bot.MakeRequest("exportChatInviteLink", v)
|
2017-11-09 19:51:50 +01:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2017-07-17 19:48:51 +02:00
|
|
|
|
2017-08-05 11:29:06 +02:00
|
|
|
var inviteLink string
|
|
|
|
err = json.Unmarshal(resp.Result, &inviteLink)
|
2017-07-17 19:48:51 +02:00
|
|
|
|
2017-08-05 11:29:06 +02:00
|
|
|
return inviteLink, err
|
2017-07-17 19:48:51 +02:00
|
|
|
}
|
2017-08-05 11:29:06 +02:00
|
|
|
|
2017-11-09 19:51:50 +01:00
|
|
|
// PinChatMessage pin message in supergroup
|
2017-08-05 11:29:06 +02:00
|
|
|
func (bot *BotAPI) PinChatMessage(config PinChatMessageConfig) (APIResponse, error) {
|
|
|
|
v, err := config.values()
|
|
|
|
if err != nil {
|
|
|
|
return APIResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.debugLog(config.method(), v, nil)
|
|
|
|
|
|
|
|
return bot.MakeRequest(config.method(), v)
|
|
|
|
}
|
|
|
|
|
2017-11-09 19:51:50 +01:00
|
|
|
// UnpinChatMessage unpin message in supergroup
|
2017-08-05 11:29:06 +02:00
|
|
|
func (bot *BotAPI) UnpinChatMessage(config UnpinChatMessageConfig) (APIResponse, error) {
|
|
|
|
v, err := config.values()
|
|
|
|
if err != nil {
|
|
|
|
return APIResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.debugLog(config.method(), v, nil)
|
|
|
|
|
|
|
|
return bot.MakeRequest(config.method(), v)
|
2017-10-29 22:03:48 +01:00
|
|
|
}
|
2018-03-03 19:20:03 +01:00
|
|
|
|
|
|
|
// SetChatTitle change title of chat.
|
|
|
|
func (bot *BotAPI) SetChatTitle(config SetChatTitleConfig) (APIResponse, error) {
|
|
|
|
v, err := config.values()
|
|
|
|
if err != nil {
|
|
|
|
return APIResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.debugLog(config.method(), v, nil)
|
|
|
|
|
|
|
|
return bot.MakeRequest(config.method(), v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetChatDescription change description of chat.
|
|
|
|
func (bot *BotAPI) SetChatDescription(config SetChatDescriptionConfig) (APIResponse, error) {
|
|
|
|
v, err := config.values()
|
|
|
|
if err != nil {
|
|
|
|
return APIResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.debugLog(config.method(), v, nil)
|
|
|
|
|
|
|
|
return bot.MakeRequest(config.method(), v)
|
|
|
|
}
|
2018-03-05 16:04:37 +01:00
|
|
|
|
|
|
|
// SetChatPhoto change photo of chat.
|
|
|
|
func (bot *BotAPI) SetChatPhoto(config SetChatPhotoConfig) (APIResponse, error) {
|
|
|
|
params, err := config.params()
|
|
|
|
if err != nil {
|
|
|
|
return APIResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
file := config.getFile()
|
|
|
|
|
|
|
|
return bot.UploadFile(config.method(), params, config.name(), file)
|
|
|
|
}
|
2018-03-05 16:12:56 +01:00
|
|
|
|
|
|
|
// DeleteChatPhoto delete photo of chat.
|
|
|
|
func (bot *BotAPI) DeleteChatPhoto(config DeleteChatPhotoConfig) (APIResponse, error) {
|
|
|
|
v, err := config.values()
|
|
|
|
if err != nil {
|
|
|
|
return APIResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
bot.debugLog(config.method(), v, nil)
|
|
|
|
|
|
|
|
return bot.MakeRequest(config.method(), v)
|
|
|
|
}
|