2015-06-26 06:26:24 +02:00
|
|
|
package tgbotapi
|
2015-06-25 07:34:05 +02:00
|
|
|
|
|
|
|
import (
|
2015-09-07 18:20:43 +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-09-07 18:20:43 +02:00
|
|
|
"github.com/technoweenie/multipartstreamer"
|
|
|
|
"io"
|
2015-06-25 07:34:05 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"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-09-07 18:20:43 +02:00
|
|
|
File interface{}
|
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
|
2015-08-05 03:55:48 +02:00
|
|
|
Duration int
|
2015-08-18 03:40:42 +02:00
|
|
|
Performer string
|
|
|
|
Title string
|
2015-06-26 08:53:20 +02:00
|
|
|
ReplyToMessageID int
|
2015-06-26 06:26:24 +02:00
|
|
|
ReplyMarkup interface{}
|
|
|
|
UseExistingAudio bool
|
|
|
|
FilePath string
|
2015-09-07 18:20:43 +02:00
|
|
|
File interface{}
|
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-09-07 18:20:43 +02:00
|
|
|
File interface{}
|
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-09-07 18:20:43 +02:00
|
|
|
File interface{}
|
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
|
2015-08-05 03:44:09 +02:00
|
|
|
Duration int
|
|
|
|
Caption string
|
2015-06-26 08:53:20 +02:00
|
|
|
ReplyToMessageID int
|
2015-06-26 06:26:24 +02:00
|
|
|
ReplyMarkup interface{}
|
|
|
|
UseExistingVideo bool
|
|
|
|
FilePath string
|
2015-09-07 18:20:43 +02:00
|
|
|
File interface{}
|
2015-06-26 08:53:20 +02:00
|
|
|
FileID string
|
2015-06-26 06:26:24 +02:00
|
|
|
}
|
|
|
|
|
2015-08-18 03:40:42 +02:00
|
|
|
// VoiceConfig contains information about a SendVoice request.
|
|
|
|
type VoiceConfig struct {
|
|
|
|
ChatID int
|
|
|
|
Duration int
|
|
|
|
ReplyToMessageID int
|
|
|
|
ReplyMarkup interface{}
|
|
|
|
UseExistingVoice bool
|
|
|
|
FilePath string
|
2015-09-07 18:20:43 +02:00
|
|
|
File interface{}
|
2015-08-18 03:40:42 +02:00
|
|
|
FileID string
|
|
|
|
}
|
|
|
|
|
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 {
|
2015-09-07 18:44:29 +02:00
|
|
|
Clear bool
|
|
|
|
URL *url.URL
|
|
|
|
Certificate interface{}
|
2015-06-25 07:34:05 +02:00
|
|
|
}
|
|
|
|
|
2015-09-07 18:20:43 +02:00
|
|
|
// FileBytes contains information about a set of bytes to upload as a File.
|
|
|
|
type FileBytes struct {
|
|
|
|
Name string
|
|
|
|
Bytes []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// FileReader contains information about a reader to upload as a File.
|
|
|
|
// If Size is -1, it will read the entire Reader into memory to calculate a Size.
|
|
|
|
type FileReader struct {
|
|
|
|
Name string
|
|
|
|
Reader io.Reader
|
|
|
|
Size int64
|
|
|
|
}
|
|
|
|
|
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-09-07 18:20:43 +02:00
|
|
|
// 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) {
|
2015-08-10 14:41:44 +02:00
|
|
|
ms := multipartstreamer.New()
|
|
|
|
ms.WriteFields(params)
|
2015-09-07 18:20:43 +02:00
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
2015-06-25 07:34:05 +02:00
|
|
|
|
2015-08-10 14:41:44 +02:00
|
|
|
req, err := http.NewRequest("POST", fmt.Sprintf(APIEndpoint, bot.Token, endpoint), nil)
|
|
|
|
ms.SetupRequest(req)
|
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-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-08-10 14:41:44 +02:00
|
|
|
defer res.Body.Close()
|
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-09-07 18:20:43 +02:00
|
|
|
// Requires ChatID and FileID OR File.
|
2015-06-26 08:53:20 +02:00
|
|
|
// Caption, ReplyToMessageID, and ReplyMarkup are optional.
|
2015-09-07 18:20:43 +02:00
|
|
|
// File should be either a string, FileBytes, or FileReader.
|
2015-06-26 08:53:20 +02:00
|
|
|
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-09-07 18:20:43 +02:00
|
|
|
var file interface{}
|
|
|
|
if config.FilePath == "" {
|
|
|
|
file = config.File
|
|
|
|
} else {
|
|
|
|
file = config.FilePath
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := bot.UploadFile("SendPhoto", params, "photo", file)
|
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.
|
2015-08-18 03:40:42 +02:00
|
|
|
// If using a file, the file must be in the .mp3 format.
|
|
|
|
//
|
2015-09-07 18:20:43 +02:00
|
|
|
// When the fields title and performer are both empty and
|
|
|
|
// the mime-type of the file to be sent is not audio/mpeg,
|
|
|
|
// the file must be an .ogg file encoded with OPUS.
|
2015-08-10 23:02:29 +02:00
|
|
|
// You may use the tgutils.EncodeAudio func to assist you with this, if needed.
|
2015-06-26 07:57:39 +02:00
|
|
|
//
|
2015-09-07 18:20:43 +02:00
|
|
|
// Requires ChatID and FileID OR File.
|
2015-06-26 08:53:20 +02:00
|
|
|
// ReplyToMessageID and ReplyMarkup are optional.
|
2015-09-07 18:20:43 +02:00
|
|
|
// File should be either a string, FileBytes, or FileReader.
|
2015-06-26 08:53:20 +02:00
|
|
|
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
|
|
|
}
|
2015-08-05 03:55:48 +02:00
|
|
|
if config.Duration != 0 {
|
|
|
|
v.Add("duration", strconv.Itoa(config.Duration))
|
|
|
|
}
|
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-08-18 03:40:42 +02:00
|
|
|
if config.Performer != "" {
|
|
|
|
v.Add("performer", config.Performer)
|
|
|
|
}
|
|
|
|
if config.Title != "" {
|
|
|
|
v.Add("title", config.Title)
|
|
|
|
}
|
2015-06-25 18:57:50 +02:00
|
|
|
|
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
|
|
|
}
|
2015-08-18 03:40:42 +02:00
|
|
|
if config.Duration != 0 {
|
|
|
|
params["duration"] = strconv.Itoa(config.Duration)
|
|
|
|
}
|
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-08-18 03:40:42 +02:00
|
|
|
if config.Performer != "" {
|
|
|
|
params["performer"] = config.Performer
|
|
|
|
}
|
|
|
|
if config.Title != "" {
|
|
|
|
params["title"] = config.Title
|
|
|
|
}
|
2015-06-25 18:57:50 +02:00
|
|
|
|
2015-09-07 18:20:43 +02:00
|
|
|
var file interface{}
|
|
|
|
if config.FilePath == "" {
|
|
|
|
file = config.File
|
|
|
|
} else {
|
|
|
|
file = config.FilePath
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := bot.UploadFile("sendAudio", params, "audio", file)
|
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-09-07 18:20:43 +02:00
|
|
|
// Requires ChatID and FileID OR File.
|
2015-06-26 08:53:20 +02:00
|
|
|
// ReplyToMessageID and ReplyMarkup are optional.
|
2015-09-07 18:20:43 +02:00
|
|
|
// File should be either a string, FileBytes, or FileReader.
|
2015-06-26 08:53:20 +02:00
|
|
|
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-09-07 18:20:43 +02:00
|
|
|
var file interface{}
|
|
|
|
if config.FilePath == "" {
|
|
|
|
file = config.File
|
|
|
|
} else {
|
|
|
|
file = config.FilePath
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := bot.UploadFile("sendDocument", params, "document", file)
|
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-08-18 03:40:42 +02:00
|
|
|
// SendVoice sends or uploads a playable voice to a chat.
|
|
|
|
// If using a file, the file must be encoded as an .ogg with OPUS.
|
2015-08-18 03:48:17 +02:00
|
|
|
// You may use the tgutils.EncodeAudio func to assist you with this, if needed.
|
2015-08-18 03:40:42 +02:00
|
|
|
//
|
2015-09-07 18:20:43 +02:00
|
|
|
// Requires ChatID and FileID OR File.
|
2015-08-18 03:40:42 +02:00
|
|
|
// ReplyToMessageID and ReplyMarkup are optional.
|
2015-09-07 18:20:43 +02:00
|
|
|
// File should be either a string, FileBytes, or FileReader.
|
2015-08-18 03:40:42 +02:00
|
|
|
func (bot *BotAPI) SendVoice(config VoiceConfig) (Message, error) {
|
|
|
|
if config.UseExistingVoice {
|
|
|
|
v := url.Values{}
|
|
|
|
v.Add("chat_id", strconv.Itoa(config.ChatID))
|
|
|
|
v.Add("voice", config.FileID)
|
|
|
|
if config.ReplyToMessageID != 0 {
|
|
|
|
v.Add("reply_to_message_id", strconv.Itoa(config.ReplyToMessageID))
|
|
|
|
}
|
|
|
|
if config.Duration != 0 {
|
|
|
|
v.Add("duration", strconv.Itoa(config.Duration))
|
|
|
|
}
|
|
|
|
if config.ReplyMarkup != nil {
|
|
|
|
data, err := json.Marshal(config.ReplyMarkup)
|
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
v.Add("reply_markup", string(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := bot.MakeRequest("sendVoice", v)
|
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var message Message
|
|
|
|
json.Unmarshal(resp.Result, &message)
|
|
|
|
|
|
|
|
if bot.Debug {
|
|
|
|
log.Printf("SendVoice req : %+v\n", v)
|
|
|
|
log.Printf("SendVoice resp: %+v\n", message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return message, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
params := make(map[string]string)
|
|
|
|
|
|
|
|
params["chat_id"] = strconv.Itoa(config.ChatID)
|
|
|
|
if config.ReplyToMessageID != 0 {
|
|
|
|
params["reply_to_message_id"] = strconv.Itoa(config.ReplyToMessageID)
|
|
|
|
}
|
|
|
|
if config.Duration != 0 {
|
|
|
|
params["duration"] = strconv.Itoa(config.Duration)
|
|
|
|
}
|
|
|
|
if config.ReplyMarkup != nil {
|
|
|
|
data, err := json.Marshal(config.ReplyMarkup)
|
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
params["reply_markup"] = string(data)
|
|
|
|
}
|
|
|
|
|
2015-09-07 18:20:43 +02:00
|
|
|
var file interface{}
|
|
|
|
if config.FilePath == "" {
|
|
|
|
file = config.File
|
|
|
|
} else {
|
|
|
|
file = config.FilePath
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := bot.UploadFile("SendVoice", params, "voice", file)
|
2015-08-18 03:40:42 +02:00
|
|
|
if err != nil {
|
|
|
|
return Message{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var message Message
|
|
|
|
json.Unmarshal(resp.Result, &message)
|
|
|
|
|
|
|
|
if bot.Debug {
|
|
|
|
log.Printf("SendVoice 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-09-07 18:20:43 +02:00
|
|
|
// Requires ChatID and FileID OR File.
|
2015-06-26 08:53:20 +02:00
|
|
|
// ReplyToMessageID and ReplyMarkup are optional.
|
2015-09-07 18:20:43 +02:00
|
|
|
// File should be either a string, FileBytes, or FileReader.
|
2015-06-26 08:53:20 +02:00
|
|
|
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-09-07 18:20:43 +02:00
|
|
|
var file interface{}
|
|
|
|
if config.FilePath == "" {
|
|
|
|
file = config.File
|
|
|
|
} else {
|
|
|
|
file = config.FilePath
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := bot.UploadFile("sendSticker", params, "sticker", file)
|
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-09-07 18:20:43 +02:00
|
|
|
// Requires ChatID and FileID OR File.
|
2015-06-26 08:53:20 +02:00
|
|
|
// ReplyToMessageID and ReplyMarkup are optional.
|
2015-09-07 18:20:43 +02:00
|
|
|
// File should be either a string, FileBytes, or FileReader.
|
2015-06-26 08:53:20 +02:00
|
|
|
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
|
|
|
}
|
2015-08-05 03:44:09 +02:00
|
|
|
if config.Duration != 0 {
|
|
|
|
v.Add("duration", strconv.Itoa(config.Duration))
|
|
|
|
}
|
|
|
|
if config.Caption != "" {
|
|
|
|
v.Add("caption", config.Caption)
|
|
|
|
}
|
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-09-07 18:20:43 +02:00
|
|
|
var file interface{}
|
|
|
|
if config.FilePath == "" {
|
|
|
|
file = config.File
|
|
|
|
} else {
|
|
|
|
file = config.FilePath
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := bot.UploadFile("sendVideo", params, "video", file)
|
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-09-07 18:44:29 +02:00
|
|
|
func (bot *BotAPI) SetWebhook(config WebhookConfig) (APIResponse, error) {
|
|
|
|
if config.Certificate == nil {
|
|
|
|
v := url.Values{}
|
|
|
|
if !config.Clear {
|
|
|
|
v.Add("url", config.URL.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return bot.MakeRequest("setWebhook", v)
|
|
|
|
}
|
|
|
|
|
|
|
|
params := make(map[string]string)
|
2015-09-07 19:47:31 +02:00
|
|
|
params["url"] = config.URL.String()
|
2015-09-07 18:44:29 +02:00
|
|
|
|
|
|
|
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 {
|
2015-09-07 19:32:16 +02:00
|
|
|
log.Printf("setWebhook resp: %+v\n", apiResp)
|
2015-06-25 07:34:05 +02:00
|
|
|
}
|
|
|
|
|
2015-09-07 18:44:29 +02:00
|
|
|
return apiResp, nil
|
2015-06-25 07:34:05 +02:00
|
|
|
}
|