2015-06-26 08:53:20 +02:00
|
|
|
// Package tgbotapi has bindings 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"
|
|
|
|
"github.com/technoweenie/multipartstreamer"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2015-09-07 18:20:43 +02:00
|
|
|
"net/http"
|
2015-11-20 11:42:26 +01:00
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
2015-09-07 18:20:43 +02:00
|
|
|
)
|
2015-07-27 00:16:45 +02:00
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// BotAPI has methods for interacting with all of Telegram's Bot API endpoints.
|
|
|
|
type BotAPI struct {
|
2015-07-28 11:18:49 +02:00
|
|
|
Token string `json:"token"`
|
|
|
|
Debug bool `json:"debug"`
|
|
|
|
Self User `json:"-"`
|
|
|
|
Updates chan Update `json:"-"`
|
|
|
|
Client *http.Client `json:"-"`
|
2015-06-25 07:34:05 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// NewBotAPI creates a new BotAPI instance.
|
2015-06-26 08:19:29 +02:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2015-07-28 18:14:13 +02:00
|
|
|
// NewBotAPIWithClient creates a new BotAPI instance passing an http.Client.
|
2015-07-28 13:00:31 +02:00
|
|
|
// 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{
|
2015-07-27 00:16:45 +02:00
|
|
|
Token: token,
|
2015-07-28 11:18:49 +02:00
|
|
|
Client: client,
|
2015-06-25 23:15:28 +02:00
|
|
|
}
|
2015-06-26 06:44:14 +02:00
|
|
|
|
|
|
|
self, err := bot.GetMe()
|
|
|
|
if err != nil {
|
2015-06-26 08:53:20 +02:00
|
|
|
return &BotAPI{}, 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
|
|
|
|
|
|
|
// MakeRequest makes a request to a specific endpoint with our token.
|
|
|
|
// All requests are POSTs because Telegram doesn't care, and it's easier.
|
|
|
|
func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, error) {
|
|
|
|
resp, err := bot.Client.PostForm(fmt.Sprintf(APIEndpoint, bot.Token, endpoint), params)
|
|
|
|
if err != nil {
|
|
|
|
return APIResponse{}, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode == http.StatusForbidden {
|
|
|
|
return APIResponse{}, errors.New(APIForbidden)
|
|
|
|
}
|
|
|
|
|
|
|
|
bytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return APIResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if bot.Debug {
|
|
|
|
log.Println(endpoint, string(bytes))
|
|
|
|
}
|
|
|
|
|
|
|
|
var apiResp APIResponse
|
|
|
|
json.Unmarshal(bytes, &apiResp)
|
|
|
|
|
|
|
|
if !apiResp.Ok {
|
|
|
|
return APIResponse{}, errors.New(apiResp.Description)
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiResp, nil
|
|
|
|
}
|
|
|
|
|
2015-11-20 12:19:37 +01:00
|
|
|
func (bot *BotAPI) MakeMessageRequest(endpoint string, params url.Values) (Message, error) {
|
|
|
|
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.
|
|
|
|
// File should be a string to a file path, a FileBytes struct, or a FileReader struct.
|
|
|
|
func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldname string, file interface{}) (APIResponse, error) {
|
|
|
|
ms := multipartstreamer.New()
|
|
|
|
ms.WriteFields(params)
|
|
|
|
|
|
|
|
switch f := file.(type) {
|
|
|
|
case string:
|
|
|
|
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:
|
|
|
|
buf := bytes.NewBuffer(f.Bytes)
|
|
|
|
ms.WriteReader(fieldname, f.Name, int64(len(f.Bytes)), buf)
|
|
|
|
case FileReader:
|
|
|
|
if f.Size == -1 {
|
|
|
|
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)
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
ms.WriteReader(fieldname, f.Name, f.Size, f.Reader)
|
|
|
|
default:
|
|
|
|
return APIResponse{}, errors.New("bad file type")
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", fmt.Sprintf(APIEndpoint, bot.Token, endpoint), nil)
|
|
|
|
ms.SetupRequest(req)
|
|
|
|
if err != nil {
|
|
|
|
return APIResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
log.Println(string(bytes[:]))
|
|
|
|
}
|
|
|
|
|
|
|
|
var apiResp APIResponse
|
|
|
|
json.Unmarshal(bytes, &apiResp)
|
|
|
|
|
|
|
|
if !apiResp.Ok {
|
|
|
|
return APIResponse{}, errors.New(apiResp.Description)
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiResp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMe fetches the currently authenticated bot.
|
|
|
|
//
|
|
|
|
// There are no parameters for this method.
|
|
|
|
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)
|
|
|
|
|
|
|
|
if bot.Debug {
|
|
|
|
log.Printf("getMe: %+v\n", user)
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-20 15:08:53 +01:00
|
|
|
func (bot *BotAPI) sendExisting(method string, config Fileable) (Message, error) {
|
|
|
|
v, err := config.Values()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
message, err := bot.MakeMessageRequest(method, v)
|
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
2015-11-20 15:31:01 +01:00
|
|
|
func (bot *BotAPI) uploadAndSend(method string, config Fileable) (Message, error) {
|
|
|
|
params, err := config.Params()
|
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
file := config.GetFile()
|
|
|
|
|
|
|
|
resp, err := bot.UploadFile(method, params, config.Name(), file)
|
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var message Message
|
|
|
|
json.Unmarshal(resp.Result, &message)
|
|
|
|
|
|
|
|
if bot.Debug {
|
|
|
|
log.Printf("%s resp: %+v\n", method, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
2015-11-20 15:55:32 +01:00
|
|
|
func (bot *BotAPI) sendFile(config Fileable) (Message, error) {
|
2015-11-20 15:31:01 +01:00
|
|
|
if config.UseExistingFile() {
|
2015-11-20 15:55:32 +01:00
|
|
|
return bot.sendExisting(config.Method(), config)
|
2015-11-20 15:31:01 +01:00
|
|
|
}
|
|
|
|
|
2015-11-20 15:55:32 +01:00
|
|
|
return bot.uploadAndSend(config.Method(), config)
|
2015-11-20 15:31:01 +01:00
|
|
|
}
|
|
|
|
|
2015-11-20 15:55:32 +01:00
|
|
|
func (bot *BotAPI) sendChattable(config Chattable) (Message, error) {
|
2015-11-20 12:06:51 +01:00
|
|
|
v, err := config.Values()
|
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
2015-11-20 11:42:26 +01:00
|
|
|
}
|
|
|
|
|
2015-11-20 15:55:32 +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.
|
|
|
|
//
|
|
|
|
// Requires UserID.
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetFile returns a file_id required to download a file.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
// To not get old items, set Offset to one higher than the previous item.
|
|
|
|
// Set Timeout to a large number to reduce requests and get responses instantly.
|
|
|
|
func (bot *BotAPI) GetUpdates(config UpdateConfig) ([]Update, error) {
|
|
|
|
v := url.Values{}
|
|
|
|
if config.Offset > 0 {
|
|
|
|
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)
|
|
|
|
|
|
|
|
if bot.Debug {
|
|
|
|
log.Printf("getUpdates: %+v\n", updates)
|
|
|
|
}
|
|
|
|
|
|
|
|
return updates, nil
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
// If this is set, GetUpdates will not get any data!
|
|
|
|
//
|
|
|
|
// Requires URL OR to set Clear to true.
|
|
|
|
func (bot *BotAPI) SetWebhook(config WebhookConfig) (APIResponse, error) {
|
|
|
|
if config.Certificate == nil {
|
|
|
|
v := url.Values{}
|
2015-11-21 12:26:39 +01:00
|
|
|
v.Add("url", config.URL.String())
|
2015-11-20 11:42:26 +01:00
|
|
|
|
|
|
|
return bot.MakeRequest("setWebhook", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
params := make(map[string]string)
|
|
|
|
params["url"] = config.URL.String()
|
|
|
|
|
|
|
|
resp, err := bot.UploadFile("setWebhook", params, "certificate", config.Certificate)
|
|
|
|
if err != nil {
|
|
|
|
return APIResponse{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var apiResp APIResponse
|
|
|
|
json.Unmarshal(resp.Result, &apiResp)
|
|
|
|
|
|
|
|
if bot.Debug {
|
|
|
|
log.Printf("setWebhook resp: %+v\n", apiResp)
|
|
|
|
}
|
|
|
|
|
|
|
|
return apiResp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdatesChan starts a channel for getting updates.
|
|
|
|
func (bot *BotAPI) UpdatesChan(config UpdateConfig) error {
|
|
|
|
bot.Updates = make(chan Update, 100)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
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 {
|
|
|
|
if update.UpdateID >= config.Offset {
|
|
|
|
config.Offset = update.UpdateID + 1
|
|
|
|
bot.Updates <- update
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenForWebhook registers a http handler for a webhook.
|
2015-11-21 12:22:08 +01:00
|
|
|
func (bot *BotAPI) ListenForWebhook(pattern string) http.Handler {
|
2015-11-20 11:42:26 +01:00
|
|
|
bot.Updates = make(chan Update, 100)
|
|
|
|
|
2015-11-21 12:22:08 +01:00
|
|
|
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2015-11-20 11:42:26 +01:00
|
|
|
bytes, _ := ioutil.ReadAll(r.Body)
|
|
|
|
|
|
|
|
var update Update
|
|
|
|
json.Unmarshal(bytes, &update)
|
|
|
|
|
|
|
|
bot.Updates <- update
|
|
|
|
})
|
2015-11-21 12:22:08 +01:00
|
|
|
|
|
|
|
http.HandleFunc(pattern, handler)
|
|
|
|
|
|
|
|
return handler
|
2015-11-20 11:42:26 +01:00
|
|
|
}
|