2015-06-26 06:26:24 +02:00
|
|
|
package tgbotapi
|
2015-06-25 07:34:05 +02:00
|
|
|
|
|
|
|
import (
|
2015-06-25 18:25:02 +02:00
|
|
|
"bytes"
|
2015-06-25 07:34:05 +02:00
|
|
|
"encoding/json"
|
2015-06-25 21:20:02 +02:00
|
|
|
"errors"
|
2015-07-29 11:41:41 +02:00
|
|
|
"fmt"
|
2015-06-25 18:25:02 +02:00
|
|
|
"io"
|
2015-06-25 07:34:05 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2015-06-25 18:25:02 +02:00
|
|
|
"mime/multipart"
|
2015-06-25 07:34:05 +02:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2015-06-25 18:25:02 +02:00
|
|
|
"os"
|
2015-06-25 07:34:05 +02:00
|
|
|
"strconv"
|
|
|
|
)
|
|
|
|
|
2015-07-29 11:41:41 +02:00
|
|
|
// Telegram constants
|
|
|
|
const (
|
|
|
|
// APIEndpoint is the endpoint for all API methods, with formatting for Sprintf
|
|
|
|
APIEndpoint = "https://api.telegram.org/bot%s/%s"
|
|
|
|
)
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// Constant values for ChatActions
|
2015-06-25 07:34:05 +02:00
|
|
|
const (
|
2015-06-26 08:53:20 +02:00
|
|
|
ChatTyping = "typing"
|
|
|
|
ChatUploadPhoto = "upload_photo"
|
|
|
|
ChatRecordVideo = "record_video"
|
|
|
|
ChatUploadVideo = "upload_video"
|
|
|
|
ChatRecordAudio = "record_audio"
|
|
|
|
ChatUploadAudio = "upload_audio"
|
|
|
|
ChatUploadDocument = "upload_document"
|
|
|
|
ChatFindLocation = "find_location"
|
2015-06-25 07:34:05 +02:00
|
|
|
)
|
|
|
|
|
2015-07-29 11:41:41 +02:00
|
|
|
// API errors
|
|
|
|
const (
|
|
|
|
// APIForbidden happens when a token is bad
|
|
|
|
APIForbidden = "forbidden"
|
|
|
|
)
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// MessageConfig contains information about a SendMessage request.
|
2015-06-26 06:26:24 +02:00
|
|
|
type MessageConfig struct {
|
2015-06-26 08:53:20 +02:00
|
|
|
ChatID int
|
2015-06-26 06:26:24 +02:00
|
|
|
Text string
|
|
|
|
DisableWebPagePreview bool
|
2015-06-26 08:53:20 +02:00
|
|
|
ReplyToMessageID int
|
2015-06-26 06:26:24 +02:00
|
|
|
ReplyMarkup interface{}
|
2015-06-25 07:34:05 +02:00
|
|
|
}
|
|
|
|
|
2015-07-29 11:41:41 +02:00
|
|
|
// ForwardConfig contains information about a ForwardMessage request.
|
2015-06-26 06:26:24 +02:00
|
|
|
type ForwardConfig struct {
|
2015-06-26 08:53:20 +02:00
|
|
|
ChatID int
|
|
|
|
FromChatID int
|
|
|
|
MessageID int
|
2015-06-25 07:34:05 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// PhotoConfig contains information about a SendPhoto request.
|
2015-06-26 06:26:24 +02:00
|
|
|
type PhotoConfig struct {
|
2015-06-26 08:53:20 +02:00
|
|
|
ChatID int
|
2015-06-26 06:26:24 +02:00
|
|
|
Caption string
|
2015-06-26 08:53:20 +02:00
|
|
|
ReplyToMessageID int
|
2015-06-26 06:26:24 +02:00
|
|
|
ReplyMarkup interface{}
|
|
|
|
UseExistingPhoto bool
|
|
|
|
FilePath string
|
2015-06-26 08:53:20 +02:00
|
|
|
FileID string
|
2015-06-26 06:26:24 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// AudioConfig contains information about a SendAudio request.
|
2015-06-26 06:26:24 +02:00
|
|
|
type AudioConfig struct {
|
2015-06-26 08:53:20 +02:00
|
|
|
ChatID int
|
|
|
|
ReplyToMessageID int
|
2015-06-26 06:26:24 +02:00
|
|
|
ReplyMarkup interface{}
|
|
|
|
UseExistingAudio bool
|
|
|
|
FilePath string
|
2015-06-26 08:53:20 +02:00
|
|
|
FileID string
|
2015-06-26 06:26:24 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// DocumentConfig contains information about a SendDocument request.
|
2015-06-26 06:26:24 +02:00
|
|
|
type DocumentConfig struct {
|
2015-06-26 08:53:20 +02:00
|
|
|
ChatID int
|
|
|
|
ReplyToMessageID int
|
2015-06-26 06:26:24 +02:00
|
|
|
ReplyMarkup interface{}
|
|
|
|
UseExistingDocument bool
|
|
|
|
FilePath string
|
2015-06-26 08:53:20 +02:00
|
|
|
FileID string
|
2015-06-26 06:26:24 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// StickerConfig contains information about a SendSticker request.
|
2015-06-26 06:26:24 +02:00
|
|
|
type StickerConfig struct {
|
2015-06-26 08:53:20 +02:00
|
|
|
ChatID int
|
|
|
|
ReplyToMessageID int
|
2015-06-26 06:26:24 +02:00
|
|
|
ReplyMarkup interface{}
|
|
|
|
UseExistingSticker bool
|
|
|
|
FilePath string
|
2015-06-26 08:53:20 +02:00
|
|
|
FileID string
|
2015-06-26 06:26:24 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// VideoConfig contains information about a SendVideo request.
|
2015-06-26 06:26:24 +02:00
|
|
|
type VideoConfig struct {
|
2015-06-26 08:53:20 +02:00
|
|
|
ChatID int
|
|
|
|
ReplyToMessageID int
|
2015-06-26 06:26:24 +02:00
|
|
|
ReplyMarkup interface{}
|
|
|
|
UseExistingVideo bool
|
|
|
|
FilePath string
|
2015-06-26 08:53:20 +02:00
|
|
|
FileID string
|
2015-06-26 06:26:24 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// LocationConfig contains information about a SendLocation request.
|
2015-06-26 06:26:24 +02:00
|
|
|
type LocationConfig struct {
|
2015-06-26 08:53:20 +02:00
|
|
|
ChatID int
|
2015-06-26 06:26:24 +02:00
|
|
|
Latitude float64
|
|
|
|
Longitude float64
|
2015-06-26 08:53:20 +02:00
|
|
|
ReplyToMessageID int
|
2015-06-26 06:26:24 +02:00
|
|
|
ReplyMarkup interface{}
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// ChatActionConfig contains information about a SendChatAction request.
|
2015-06-26 06:26:24 +02:00
|
|
|
type ChatActionConfig struct {
|
2015-06-26 08:53:20 +02:00
|
|
|
ChatID int
|
2015-06-26 06:26:24 +02:00
|
|
|
Action string
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// UserProfilePhotosConfig contains information about a GetUserProfilePhotos request.
|
2015-06-26 06:26:24 +02:00
|
|
|
type UserProfilePhotosConfig struct {
|
2015-06-26 08:53:20 +02:00
|
|
|
UserID int
|
2015-06-26 06:26:24 +02:00
|
|
|
Offset int
|
|
|
|
Limit int
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// UpdateConfig contains information about a GetUpdates request.
|
2015-06-26 06:26:24 +02:00
|
|
|
type UpdateConfig struct {
|
|
|
|
Offset int
|
|
|
|
Limit int
|
|
|
|
Timeout int
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// WebhookConfig contains information about a SetWebhook request.
|
2015-06-26 06:26:24 +02:00
|
|
|
type WebhookConfig struct {
|
|
|
|
Clear bool
|
2015-06-26 08:53:20 +02:00
|
|
|
URL *url.URL
|
2015-06-25 07:34:05 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// MakeRequest makes a request to a specific endpoint with our token.
|
2015-06-26 07:57:39 +02:00
|
|
|
// All requests are POSTs because Telegram doesn't care, and it's easier.
|
2015-06-26 08:53:20 +02:00
|
|
|
func (bot *BotAPI) MakeRequest(endpoint string, params url.Values) (APIResponse, error) {
|
2015-07-29 11:41:41 +02:00
|
|
|
resp, err := bot.Client.PostForm(fmt.Sprintf(APIEndpoint, bot.Token, endpoint), params)
|
2015-06-25 18:25:02 +02:00
|
|
|
if err != nil {
|
2015-06-26 08:53:20 +02:00
|
|
|
return APIResponse{}, err
|
2015-06-25 18:25:02 +02:00
|
|
|
}
|
2015-07-28 18:19:53 +02:00
|
|
|
defer resp.Body.Close()
|
2015-06-25 07:34:05 +02:00
|
|
|
|
2015-07-29 11:41:41 +02:00
|
|
|
if resp.StatusCode == http.StatusForbidden {
|
|
|
|
return APIResponse{}, errors.New(APIForbidden)
|
|
|
|
}
|
|
|
|
|
2015-06-25 18:25:02 +02:00
|
|
|
bytes, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2015-06-26 08:53:20 +02:00
|
|
|
return APIResponse{}, err
|
2015-06-25 18:25:02 +02:00
|
|
|
}
|
2015-06-25 07:34:05 +02:00
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
if bot.Debug {
|
2015-06-25 21:20:02 +02:00
|
|
|
log.Println(endpoint, string(bytes))
|
2015-06-25 18:25:02 +02:00
|
|
|
}
|
2015-06-25 07:34:05 +02:00
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
var apiResp APIResponse
|
2015-06-25 18:25:02 +02:00
|
|
|
json.Unmarshal(bytes, &apiResp)
|
2015-06-25 07:34:05 +02:00
|
|
|
|
2015-06-25 21:20:02 +02:00
|
|
|
if !apiResp.Ok {
|
2015-06-26 08:53:20 +02:00
|
|
|
return APIResponse{}, errors.New(apiResp.Description)
|
2015-06-25 21:20:02 +02:00
|
|
|
}
|
|
|
|
|
2015-06-25 18:25:02 +02:00
|
|
|
return apiResp, nil
|
2015-06-25 07:34:05 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// UploadFile makes a request to the API with a file.
|
2015-06-26 07:57:39 +02:00
|
|
|
//
|
|
|
|
// Requires the parameter to hold the file not be in the params.
|
2015-06-26 08:53:20 +02:00
|
|
|
func (bot *BotAPI) UploadFile(endpoint string, params map[string]string, fieldname string, filename string) (APIResponse, error) {
|
2015-06-25 18:25:02 +02:00
|
|
|
var b bytes.Buffer
|
|
|
|
w := multipart.NewWriter(&b)
|
2015-06-25 07:34:05 +02:00
|
|
|
|
2015-06-25 18:25:02 +02:00
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
2015-06-26 08:53:20 +02:00
|
|
|
return APIResponse{}, err
|
2015-06-25 18:25:02 +02:00
|
|
|
}
|
2015-06-25 07:34:05 +02:00
|
|
|
|
2015-06-25 18:25:02 +02:00
|
|
|
fw, err := w.CreateFormFile(fieldname, filename)
|
|
|
|
if err != nil {
|
2015-06-26 08:53:20 +02:00
|
|
|
return APIResponse{}, err
|
2015-06-25 18:25:02 +02:00
|
|
|
}
|
2015-06-25 07:34:05 +02:00
|
|
|
|
2015-06-25 18:25:02 +02:00
|
|
|
if _, err = io.Copy(fw, f); err != nil {
|
2015-06-26 08:53:20 +02:00
|
|
|
return APIResponse{}, err
|
2015-06-25 18:25:02 +02:00
|
|
|
}
|
2015-06-25 07:34:05 +02:00
|
|
|
|
2015-06-25 18:25:02 +02:00
|
|
|
for key, val := range params {
|
|
|
|
if fw, err = w.CreateFormField(key); err != nil {
|
2015-06-26 08:53:20 +02:00
|
|
|
return APIResponse{}, err
|
2015-06-25 18:25:02 +02:00
|
|
|
}
|
2015-06-25 07:34:05 +02:00
|
|
|
|
2015-06-25 18:25:02 +02:00
|
|
|
if _, err = fw.Write([]byte(val)); err != nil {
|
2015-06-26 08:53:20 +02:00
|
|
|
return APIResponse{}, err
|
2015-06-25 18:25:02 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-25 07:34:05 +02:00
|
|
|
|
2015-06-25 18:25:02 +02:00
|
|
|
w.Close()
|
2015-06-25 07:34:05 +02:00
|
|
|
|
2015-07-29 11:41:41 +02:00
|
|
|
req, err := http.NewRequest("POST", fmt.Sprintf(APIEndpoint, bot.Token, endpoint), &b)
|
2015-06-25 18:25:02 +02:00
|
|
|
if err != nil {
|
2015-06-26 08:53:20 +02:00
|
|
|
return APIResponse{}, err
|
2015-06-25 07:34:05 +02:00
|
|
|
}
|
|
|
|
|
2015-06-25 18:25:02 +02:00
|
|
|
req.Header.Set("Content-Type", w.FormDataContentType())
|
|
|
|
|
2015-07-27 00:16:45 +02:00
|
|
|
res, err := bot.Client.Do(req)
|
2015-06-25 07:34:05 +02:00
|
|
|
if err != nil {
|
2015-06-26 08:53:20 +02:00
|
|
|
return APIResponse{}, err
|
2015-06-25 07:34:05 +02:00
|
|
|
}
|
|
|
|
|
2015-06-25 18:25:02 +02:00
|
|
|
bytes, err := ioutil.ReadAll(res.Body)
|
2015-06-25 07:34:05 +02:00
|
|
|
if err != nil {
|
2015-06-26 08:53:20 +02:00
|
|
|
return APIResponse{}, err
|
2015-06-25 07:34:05 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
if bot.Debug {
|
2015-06-25 07:34:05 +02:00
|
|
|
log.Println(string(bytes[:]))
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
var apiResp APIResponse
|
2015-06-25 07:34:05 +02:00
|
|
|
json.Unmarshal(bytes, &apiResp)
|
|
|
|
|
|
|
|
return apiResp, nil
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// GetMe fetches the currently authenticated bot.
|
2015-06-26 07:57:39 +02:00
|
|
|
//
|
|
|
|
// There are no parameters for this method.
|
2015-06-26 08:53:20 +02:00
|
|
|
func (bot *BotAPI) GetMe() (User, error) {
|
2015-06-26 06:26:24 +02:00
|
|
|
resp, err := bot.MakeRequest("getMe", nil)
|
2015-06-25 07:34:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return User{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var user User
|
|
|
|
json.Unmarshal(resp.Result, &user)
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
if bot.Debug {
|
2015-06-25 07:34:05 +02:00
|
|
|
log.Printf("getMe: %+v\n", user)
|
|
|
|
}
|
|
|
|
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// SendMessage sends a Message to a chat.
|
2015-06-26 07:57:39 +02:00
|
|
|
//
|
2015-06-26 08:53:20 +02:00
|
|
|
// Requires ChatID and Text.
|
|
|
|
// DisableWebPagePreview, ReplyToMessageID, and ReplyMarkup are optional.
|
|
|
|
func (bot *BotAPI) SendMessage(config MessageConfig) (Message, error) {
|
2015-06-25 07:34:05 +02:00
|
|
|
v := url.Values{}
|
2015-06-26 08:53:20 +02:00
|
|
|
v.Add("chat_id", strconv.Itoa(config.ChatID))
|
2015-06-25 07:34:05 +02:00
|
|
|
v.Add("text", config.Text)
|
|
|
|
v.Add("disable_web_page_preview", strconv.FormatBool(config.DisableWebPagePreview))
|
2015-06-26 08:53:20 +02:00
|
|
|
if config.ReplyToMessageID != 0 {
|
|
|
|
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
|
2015-06-25 07:34:05 +02:00
|
|
|
}
|
2015-06-25 23:15:28 +02:00
|
|
|
if config.ReplyMarkup != nil {
|
|
|
|
data, err := json.Marshal(config.ReplyMarkup)
|
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
v.Add("reply_markup", string(data))
|
|
|
|
}
|
2015-06-25 07:34:05 +02:00
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
resp, err := bot.MakeRequest("SendMessage", v)
|
2015-06-25 07:34:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var message Message
|
|
|
|
json.Unmarshal(resp.Result, &message)
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
if bot.Debug {
|
|
|
|
log.Printf("SendMessage req : %+v\n", v)
|
|
|
|
log.Printf("SendMessage resp: %+v\n", message)
|
2015-06-25 07:34:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// ForwardMessage forwards a message from one chat to another.
|
2015-06-26 07:57:39 +02:00
|
|
|
//
|
2015-07-29 11:41:41 +02:00
|
|
|
// Requires ChatID (destination), FromChatID (source), and MessageID.
|
2015-06-26 08:53:20 +02:00
|
|
|
func (bot *BotAPI) ForwardMessage(config ForwardConfig) (Message, error) {
|
2015-06-25 07:34:05 +02:00
|
|
|
v := url.Values{}
|
2015-06-26 08:53:20 +02:00
|
|
|
v.Add("chat_id", strconv.Itoa(config.ChatID))
|
|
|
|
v.Add("from_chat_id", strconv.Itoa(config.FromChatID))
|
|
|
|
v.Add("message_id", strconv.Itoa(config.MessageID))
|
2015-06-25 07:34:05 +02:00
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
resp, err := bot.MakeRequest("forwardMessage", v)
|
2015-06-25 07:34:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var message Message
|
|
|
|
json.Unmarshal(resp.Result, &message)
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
if bot.Debug {
|
2015-06-25 07:34:05 +02:00
|
|
|
log.Printf("forwardMessage req : %+v\n", v)
|
|
|
|
log.Printf("forwardMessage resp: %+v\n", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// SendPhoto sends or uploads a photo to a chat.
|
2015-06-26 07:57:39 +02:00
|
|
|
//
|
2015-06-26 08:53:20 +02:00
|
|
|
// Requires ChatID and FileID OR FilePath.
|
|
|
|
// Caption, ReplyToMessageID, and ReplyMarkup are optional.
|
|
|
|
func (bot *BotAPI) SendPhoto(config PhotoConfig) (Message, error) {
|
2015-06-25 18:25:02 +02:00
|
|
|
if config.UseExistingPhoto {
|
|
|
|
v := url.Values{}
|
2015-06-26 08:53:20 +02:00
|
|
|
v.Add("chat_id", strconv.Itoa(config.ChatID))
|
|
|
|
v.Add("photo", config.FileID)
|
2015-06-25 18:25:02 +02:00
|
|
|
if config.Caption != "" {
|
|
|
|
v.Add("caption", config.Caption)
|
|
|
|
}
|
2015-06-26 08:53:20 +02:00
|
|
|
if config.ReplyToMessageID != 0 {
|
|
|
|
v.Add("reply_to_message_id", strconv.Itoa(config.ChatID))
|
2015-06-25 18:25:02 +02:00
|
|
|
}
|
|
|
|
if config.ReplyMarkup != nil {
|
|
|
|
data, err := json.Marshal(config.ReplyMarkup)
|
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
v.Add("reply_markup", string(data))
|
|
|
|
}
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
resp, err := bot.MakeRequest("SendPhoto", v)
|
2015-06-25 18:25:02 +02:00
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var message Message
|
|
|
|
json.Unmarshal(resp.Result, &message)
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
if bot.Debug {
|
|
|
|
log.Printf("SendPhoto req : %+v\n", v)
|
|
|
|
log.Printf("SendPhoto resp: %+v\n", message)
|
2015-06-25 18:25:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
params := make(map[string]string)
|
2015-06-26 08:53:20 +02:00
|
|
|
params["chat_id"] = strconv.Itoa(config.ChatID)
|
2015-06-25 18:25:02 +02:00
|
|
|
if config.Caption != "" {
|
|
|
|
params["caption"] = config.Caption
|
|
|
|
}
|
2015-06-26 08:53:20 +02:00
|
|
|
if config.ReplyToMessageID != 0 {
|
|
|
|
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
|
2015-06-25 18:25:02 +02:00
|
|
|
}
|
|
|
|
if config.ReplyMarkup != nil {
|
|
|
|
data, err := json.Marshal(config.ReplyMarkup)
|
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
params["reply_markup"] = string(data)
|
|
|
|
}
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
resp, err := bot.UploadFile("SendPhoto", params, "photo", config.FilePath)
|
2015-06-25 18:25:02 +02:00
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var message Message
|
|
|
|
json.Unmarshal(resp.Result, &message)
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
if bot.Debug {
|
|
|
|
log.Printf("SendPhoto resp: %+v\n", message)
|
2015-06-25 18:25:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// SendAudio sends or uploads an audio clip to a chat.
|
|
|
|
// If using a file, the file must be encoded as an .ogg with OPUS.
|
2015-06-26 07:57:39 +02:00
|
|
|
//
|
2015-06-26 08:53:20 +02:00
|
|
|
// Requires ChatID and FileID OR FilePath.
|
|
|
|
// ReplyToMessageID and ReplyMarkup are optional.
|
|
|
|
func (bot *BotAPI) SendAudio(config AudioConfig) (Message, error) {
|
2015-06-25 18:57:50 +02:00
|
|
|
if config.UseExistingAudio {
|
|
|
|
v := url.Values{}
|
2015-06-26 08:53:20 +02:00
|
|
|
v.Add("chat_id", strconv.Itoa(config.ChatID))
|
|
|
|
v.Add("audio", config.FileID)
|
|
|
|
if config.ReplyToMessageID != 0 {
|
|
|
|
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
|
2015-06-25 18:57:50 +02:00
|
|
|
}
|
|
|
|
if config.ReplyMarkup != nil {
|
|
|
|
data, err := json.Marshal(config.ReplyMarkup)
|
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
v.Add("reply_markup", string(data))
|
|
|
|
}
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
resp, err := bot.MakeRequest("sendAudio", v)
|
2015-06-25 18:57:50 +02:00
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var message Message
|
|
|
|
json.Unmarshal(resp.Result, &message)
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
if bot.Debug {
|
2015-06-25 18:57:50 +02:00
|
|
|
log.Printf("sendAudio req : %+v\n", v)
|
|
|
|
log.Printf("sendAudio resp: %+v\n", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
params := make(map[string]string)
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
params["chat_id"] = strconv.Itoa(config.ChatID)
|
|
|
|
if config.ReplyToMessageID != 0 {
|
|
|
|
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
|
2015-06-25 18:57:50 +02:00
|
|
|
}
|
|
|
|
if config.ReplyMarkup != nil {
|
|
|
|
data, err := json.Marshal(config.ReplyMarkup)
|
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
params["reply_markup"] = string(data)
|
|
|
|
}
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
resp, err := bot.UploadFile("sendAudio", params, "audio", config.FilePath)
|
2015-06-25 18:57:50 +02:00
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var message Message
|
|
|
|
json.Unmarshal(resp.Result, &message)
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
if bot.Debug {
|
2015-06-25 18:57:50 +02:00
|
|
|
log.Printf("sendAudio resp: %+v\n", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// SendDocument sends or uploads a document to a chat.
|
2015-06-26 07:57:39 +02:00
|
|
|
//
|
2015-06-26 08:53:20 +02:00
|
|
|
// Requires ChatID and FileID OR FilePath.
|
|
|
|
// ReplyToMessageID and ReplyMarkup are optional.
|
|
|
|
func (bot *BotAPI) SendDocument(config DocumentConfig) (Message, error) {
|
2015-06-25 18:57:50 +02:00
|
|
|
if config.UseExistingDocument {
|
2015-06-25 18:50:24 +02:00
|
|
|
v := url.Values{}
|
2015-06-26 08:53:20 +02:00
|
|
|
v.Add("chat_id", strconv.Itoa(config.ChatID))
|
|
|
|
v.Add("document", config.FileID)
|
|
|
|
if config.ReplyToMessageID != 0 {
|
|
|
|
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
|
2015-06-25 18:50:24 +02:00
|
|
|
}
|
|
|
|
if config.ReplyMarkup != nil {
|
|
|
|
data, err := json.Marshal(config.ReplyMarkup)
|
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
v.Add("reply_markup", string(data))
|
|
|
|
}
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
resp, err := bot.MakeRequest("sendDocument", v)
|
2015-06-25 18:50:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var message Message
|
|
|
|
json.Unmarshal(resp.Result, &message)
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
if bot.Debug {
|
2015-06-25 18:50:24 +02:00
|
|
|
log.Printf("sendDocument req : %+v\n", v)
|
|
|
|
log.Printf("sendDocument resp: %+v\n", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
params := make(map[string]string)
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
params["chat_id"] = strconv.Itoa(config.ChatID)
|
|
|
|
if config.ReplyToMessageID != 0 {
|
|
|
|
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
|
2015-06-25 18:50:24 +02:00
|
|
|
}
|
|
|
|
if config.ReplyMarkup != nil {
|
|
|
|
data, err := json.Marshal(config.ReplyMarkup)
|
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
params["reply_markup"] = string(data)
|
|
|
|
}
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
resp, err := bot.UploadFile("sendDocument", params, "document", config.FilePath)
|
2015-06-25 18:50:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var message Message
|
|
|
|
json.Unmarshal(resp.Result, &message)
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
if bot.Debug {
|
2015-06-25 18:50:24 +02:00
|
|
|
log.Printf("sendDocument resp: %+v\n", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// SendSticker sends or uploads a sticker to a chat.
|
2015-06-26 07:57:39 +02:00
|
|
|
//
|
2015-06-26 08:53:20 +02:00
|
|
|
// Requires ChatID and FileID OR FilePath.
|
|
|
|
// ReplyToMessageID and ReplyMarkup are optional.
|
|
|
|
func (bot *BotAPI) SendSticker(config StickerConfig) (Message, error) {
|
2015-06-25 18:50:24 +02:00
|
|
|
if config.UseExistingSticker {
|
|
|
|
v := url.Values{}
|
2015-06-26 08:53:20 +02:00
|
|
|
v.Add("chat_id", strconv.Itoa(config.ChatID))
|
|
|
|
v.Add("sticker", config.FileID)
|
|
|
|
if config.ReplyToMessageID != 0 {
|
|
|
|
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
|
2015-06-25 18:50:24 +02:00
|
|
|
}
|
|
|
|
if config.ReplyMarkup != nil {
|
|
|
|
data, err := json.Marshal(config.ReplyMarkup)
|
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
v.Add("reply_markup", string(data))
|
|
|
|
}
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
resp, err := bot.MakeRequest("sendSticker", v)
|
2015-06-25 18:50:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var message Message
|
|
|
|
json.Unmarshal(resp.Result, &message)
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
if bot.Debug {
|
2015-06-25 18:50:24 +02:00
|
|
|
log.Printf("sendSticker req : %+v\n", v)
|
|
|
|
log.Printf("sendSticker resp: %+v\n", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
params := make(map[string]string)
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
params["chat_id"] = strconv.Itoa(config.ChatID)
|
|
|
|
if config.ReplyToMessageID != 0 {
|
|
|
|
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
|
2015-06-25 18:50:24 +02:00
|
|
|
}
|
|
|
|
if config.ReplyMarkup != nil {
|
|
|
|
data, err := json.Marshal(config.ReplyMarkup)
|
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
params["reply_markup"] = string(data)
|
|
|
|
}
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
resp, err := bot.UploadFile("sendSticker", params, "sticker", config.FilePath)
|
2015-06-25 18:50:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var message Message
|
|
|
|
json.Unmarshal(resp.Result, &message)
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
if bot.Debug {
|
2015-06-25 18:50:24 +02:00
|
|
|
log.Printf("sendSticker resp: %+v\n", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// SendVideo sends or uploads a video to a chat.
|
2015-06-26 07:57:39 +02:00
|
|
|
//
|
2015-06-26 08:53:20 +02:00
|
|
|
// Requires ChatID and FileID OR FilePath.
|
|
|
|
// ReplyToMessageID and ReplyMarkup are optional.
|
|
|
|
func (bot *BotAPI) SendVideo(config VideoConfig) (Message, error) {
|
2015-06-25 18:50:24 +02:00
|
|
|
if config.UseExistingVideo {
|
|
|
|
v := url.Values{}
|
2015-06-26 08:53:20 +02:00
|
|
|
v.Add("chat_id", strconv.Itoa(config.ChatID))
|
|
|
|
v.Add("video", config.FileID)
|
|
|
|
if config.ReplyToMessageID != 0 {
|
|
|
|
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
|
2015-06-25 18:50:24 +02:00
|
|
|
}
|
|
|
|
if config.ReplyMarkup != nil {
|
|
|
|
data, err := json.Marshal(config.ReplyMarkup)
|
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
v.Add("reply_markup", string(data))
|
|
|
|
}
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
resp, err := bot.MakeRequest("sendVideo", v)
|
2015-06-25 18:50:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var message Message
|
|
|
|
json.Unmarshal(resp.Result, &message)
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
if bot.Debug {
|
2015-06-25 18:50:24 +02:00
|
|
|
log.Printf("sendVideo req : %+v\n", v)
|
|
|
|
log.Printf("sendVideo resp: %+v\n", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
params := make(map[string]string)
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
params["chat_id"] = strconv.Itoa(config.ChatID)
|
|
|
|
if config.ReplyToMessageID != 0 {
|
|
|
|
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
|
2015-06-25 18:50:24 +02:00
|
|
|
}
|
|
|
|
if config.ReplyMarkup != nil {
|
|
|
|
data, err := json.Marshal(config.ReplyMarkup)
|
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
params["reply_markup"] = string(data)
|
|
|
|
}
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
resp, err := bot.UploadFile("sendVideo", params, "video", config.FilePath)
|
2015-06-25 18:50:24 +02:00
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var message Message
|
|
|
|
json.Unmarshal(resp.Result, &message)
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
if bot.Debug {
|
2015-06-25 18:50:24 +02:00
|
|
|
log.Printf("sendVideo resp: %+v\n", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// SendLocation sends a location to a chat.
|
2015-06-26 07:57:39 +02:00
|
|
|
//
|
2015-06-26 08:53:20 +02:00
|
|
|
// Requires ChatID, Latitude, and Longitude.
|
|
|
|
// ReplyToMessageID and ReplyMarkup are optional.
|
|
|
|
func (bot *BotAPI) SendLocation(config LocationConfig) (Message, error) {
|
2015-06-25 07:34:05 +02:00
|
|
|
v := url.Values{}
|
2015-06-26 08:53:20 +02:00
|
|
|
v.Add("chat_id", strconv.Itoa(config.ChatID))
|
2015-06-25 07:34:05 +02:00
|
|
|
v.Add("latitude", strconv.FormatFloat(config.Latitude, 'f', 6, 64))
|
|
|
|
v.Add("longitude", strconv.FormatFloat(config.Longitude, 'f', 6, 64))
|
2015-06-26 08:53:20 +02:00
|
|
|
if config.ReplyToMessageID != 0 {
|
|
|
|
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
|
2015-06-25 07:34:05 +02:00
|
|
|
}
|
|
|
|
if config.ReplyMarkup != nil {
|
|
|
|
data, err := json.Marshal(config.ReplyMarkup)
|
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
v.Add("reply_markup", string(data))
|
|
|
|
}
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
resp, err := bot.MakeRequest("sendLocation", v)
|
2015-06-25 07:34:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var message Message
|
|
|
|
json.Unmarshal(resp.Result, &message)
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
if bot.Debug {
|
2015-06-25 07:34:05 +02:00
|
|
|
log.Printf("sendLocation req : %+v\n", v)
|
|
|
|
log.Printf("sendLocation resp: %+v\n", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// SendChatAction sets a current action in a chat.
|
2015-06-26 07:57:39 +02:00
|
|
|
//
|
2015-06-26 08:53:20 +02:00
|
|
|
// Requires ChatID and a valid Action (see Chat constants).
|
|
|
|
func (bot *BotAPI) SendChatAction(config ChatActionConfig) error {
|
2015-06-25 07:34:05 +02:00
|
|
|
v := url.Values{}
|
2015-06-26 08:53:20 +02:00
|
|
|
v.Add("chat_id", strconv.Itoa(config.ChatID))
|
2015-06-25 07:34:05 +02:00
|
|
|
v.Add("action", config.Action)
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
_, err := bot.MakeRequest("sendChatAction", v)
|
2015-06-25 07:34:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// GetUserProfilePhotos gets a user's profile photos.
|
2015-06-26 07:57:39 +02:00
|
|
|
//
|
2015-06-26 08:53:20 +02:00
|
|
|
// Requires UserID.
|
2015-06-26 07:57:39 +02:00
|
|
|
// Offset and Limit are optional.
|
2015-06-26 08:53:20 +02:00
|
|
|
func (bot *BotAPI) GetUserProfilePhotos(config UserProfilePhotosConfig) (UserProfilePhotos, error) {
|
2015-06-25 07:34:05 +02:00
|
|
|
v := url.Values{}
|
2015-06-26 08:53:20 +02:00
|
|
|
v.Add("user_id", strconv.Itoa(config.UserID))
|
2015-06-25 07:34:05 +02:00
|
|
|
if config.Offset != 0 {
|
|
|
|
v.Add("offset", strconv.Itoa(config.Offset))
|
|
|
|
}
|
|
|
|
if config.Limit != 0 {
|
|
|
|
v.Add("limit", strconv.Itoa(config.Limit))
|
|
|
|
}
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
resp, err := bot.MakeRequest("getUserProfilePhotos", v)
|
2015-06-25 07:34:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return UserProfilePhotos{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var profilePhotos UserProfilePhotos
|
|
|
|
json.Unmarshal(resp.Result, &profilePhotos)
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
if bot.Debug {
|
2015-06-25 07:34:05 +02:00
|
|
|
log.Printf("getUserProfilePhotos req : %+v\n", v)
|
|
|
|
log.Printf("getUserProfilePhotos resp: %+v\n", profilePhotos)
|
|
|
|
}
|
|
|
|
|
|
|
|
return profilePhotos, nil
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// GetUpdates fetches updates.
|
2015-06-26 07:52:12 +02:00
|
|
|
// If a WebHook is set, this will not return any data!
|
2015-06-26 07:57:39 +02:00
|
|
|
//
|
2015-06-26 07:52:12 +02:00
|
|
|
// Offset, Limit, and Timeout are optional.
|
2015-06-26 07:57:39 +02:00
|
|
|
// 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.
|
2015-06-26 08:53:20 +02:00
|
|
|
func (bot *BotAPI) GetUpdates(config UpdateConfig) ([]Update, error) {
|
2015-06-25 07:34:05 +02:00
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
resp, err := bot.MakeRequest("getUpdates", v)
|
2015-06-25 07:34:05 +02:00
|
|
|
if err != nil {
|
|
|
|
return []Update{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var updates []Update
|
|
|
|
json.Unmarshal(resp.Result, &updates)
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
if bot.Debug {
|
2015-06-25 07:34:05 +02:00
|
|
|
log.Printf("getUpdates: %+v\n", updates)
|
|
|
|
}
|
|
|
|
|
|
|
|
return updates, nil
|
|
|
|
}
|
|
|
|
|
2015-06-26 08:53:20 +02:00
|
|
|
// SetWebhook sets a webhook.
|
2015-06-26 07:52:12 +02:00
|
|
|
// If this is set, GetUpdates will not get any data!
|
2015-06-26 07:57:39 +02:00
|
|
|
//
|
|
|
|
// Requires Url OR to set Clear to true.
|
2015-06-26 08:53:20 +02:00
|
|
|
func (bot *BotAPI) SetWebhook(config WebhookConfig) error {
|
2015-06-26 06:26:24 +02:00
|
|
|
v := url.Values{}
|
|
|
|
if !config.Clear {
|
2015-06-26 08:53:20 +02:00
|
|
|
v.Add("url", config.URL.String())
|
2015-06-25 07:34:05 +02:00
|
|
|
}
|
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
_, err := bot.MakeRequest("setWebhook", v)
|
2015-06-25 07:34:05 +02:00
|
|
|
|
2015-06-26 06:26:24 +02:00
|
|
|
return err
|
2015-06-25 07:34:05 +02:00
|
|
|
}
|