2015-06-26 07:34:10 +00:00
|
|
|
package telebot
|
|
|
|
|
|
|
|
import (
|
2015-07-02 18:39:39 +00:00
|
|
|
"bytes"
|
2015-06-26 07:34:10 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-07-02 18:39:39 +00:00
|
|
|
"io"
|
2015-06-26 07:34:10 +00:00
|
|
|
"io/ioutil"
|
2015-07-02 18:39:39 +00:00
|
|
|
"mime/multipart"
|
2015-06-26 07:34:10 +00:00
|
|
|
"net/http"
|
2015-06-26 16:12:54 +00:00
|
|
|
"net/url"
|
2015-07-02 18:39:39 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-06-26 16:12:54 +00:00
|
|
|
"strconv"
|
2015-06-26 07:34:10 +00:00
|
|
|
)
|
|
|
|
|
2015-07-02 18:39:39 +00:00
|
|
|
func api_GET(method string, token string, params url.Values) ([]byte, error) {
|
2015-06-26 19:20:08 +00:00
|
|
|
url := fmt.Sprintf("https://api.telegram.org/bot%s/%s?%s",
|
|
|
|
token, method, params.Encode())
|
2015-06-26 07:34:10 +00:00
|
|
|
|
2015-06-26 19:20:08 +00:00
|
|
|
resp, err := http.Get(url)
|
2015-06-26 07:34:10 +00:00
|
|
|
if err != nil {
|
2015-06-26 19:20:08 +00:00
|
|
|
return []byte{}, err
|
2015-06-26 07:34:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
2015-06-26 19:20:08 +00:00
|
|
|
json, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return json, nil
|
|
|
|
}
|
|
|
|
|
2015-07-02 18:39:39 +00:00
|
|
|
func api_POST(method, token, name, path string, params url.Values) ([]byte, error) {
|
|
|
|
file, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
body := &bytes.Buffer{}
|
|
|
|
writer := multipart.NewWriter(body)
|
|
|
|
part, err := writer.CreateFormFile(name, filepath.Base(path))
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = io.Copy(part, file); err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for field, values := range params {
|
|
|
|
if len(values) > 0 {
|
|
|
|
writer.WriteField(field, values[0])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = writer.Close(); err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
url := fmt.Sprintf("https://api.telegram.org/bot%s/%s", token, method)
|
|
|
|
req, err := http.NewRequest("POST", url, body)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("Content-Type", writer.FormDataContentType())
|
|
|
|
|
|
|
|
client := &http.Client{}
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
json, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return []byte{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return json, nil
|
|
|
|
}
|
|
|
|
|
2015-06-26 19:20:08 +00:00
|
|
|
func api_getMe(token string) (User, error) {
|
2015-07-02 18:39:39 +00:00
|
|
|
me_json, err := api_GET("getMe", token, url.Values{})
|
2015-06-26 07:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return User{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var bot_info struct {
|
|
|
|
Ok bool
|
|
|
|
Result User
|
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(me_json, &bot_info)
|
|
|
|
if err != nil {
|
|
|
|
return User{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if bot_info.Ok {
|
|
|
|
return bot_info.Result, nil
|
|
|
|
} else {
|
|
|
|
return User{}, AuthError{bot_info.Description}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-26 16:12:54 +00:00
|
|
|
func api_getUpdates(token string, offset int, updates chan<- Update) error {
|
2015-06-26 19:20:08 +00:00
|
|
|
params := url.Values{}
|
2015-07-02 18:39:39 +00:00
|
|
|
params.Set("offset", strconv.Itoa(offset))
|
|
|
|
updates_json, err := api_GET("getUpdates", token, params)
|
2015-06-26 07:34:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var updates_recieved struct {
|
|
|
|
Ok bool
|
|
|
|
Result []Update
|
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(updates_json, &updates_recieved)
|
2015-06-26 16:12:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-06-26 07:34:10 +00:00
|
|
|
|
|
|
|
if !updates_recieved.Ok {
|
|
|
|
return FetchError{updates_recieved.Description}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, update := range updates_recieved.Result {
|
|
|
|
updates <- update
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-06-26 16:12:54 +00:00
|
|
|
|
|
|
|
func api_sendMessage(token string, recipient User, text string) error {
|
|
|
|
params := url.Values{}
|
2015-07-02 18:39:39 +00:00
|
|
|
params.Set("chat_id", strconv.Itoa(recipient.Id))
|
2015-06-26 16:12:54 +00:00
|
|
|
params.Set("text", text)
|
2015-07-02 18:39:39 +00:00
|
|
|
response_json, err := api_GET("sendMessage", token, params)
|
2015-06-26 16:12:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var response_recieved struct {
|
|
|
|
Ok bool
|
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(response_json, &response_recieved)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !response_recieved.Ok {
|
|
|
|
return SendError{response_recieved.Description}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2015-07-02 09:05:50 +00:00
|
|
|
|
|
|
|
func api_forwardMessage(token string, recipient User, message Message) error {
|
|
|
|
params := url.Values{}
|
2015-07-02 18:39:39 +00:00
|
|
|
params.Set("chat_id", strconv.Itoa(recipient.Id))
|
|
|
|
params.Set("from_chat_id", strconv.Itoa(message.Origin().Id))
|
|
|
|
params.Set("message_id", strconv.Itoa(message.Id))
|
|
|
|
|
|
|
|
response_json, err := api_GET("forwardMessage", token, params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var response_recieved struct {
|
|
|
|
Ok bool
|
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(response_json, &response_recieved)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !response_recieved.Ok {
|
|
|
|
return SendError{response_recieved.Description}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func api_sendPhoto(token string, recipient User, photo *Photo) error {
|
|
|
|
params := url.Values{}
|
|
|
|
params.Set("chat_id", strconv.Itoa(recipient.Id))
|
|
|
|
params.Set("caption", photo.Caption)
|
|
|
|
|
|
|
|
var response_json []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if photo.Exists() {
|
|
|
|
params.Set("photo", photo.FileId)
|
|
|
|
response_json, err = api_GET("sendPhoto", token, params)
|
|
|
|
} else {
|
|
|
|
response_json, err = api_POST("sendPhoto", token, "photo",
|
|
|
|
photo.filename, params)
|
|
|
|
}
|
2015-07-02 09:05:50 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var response_recieved struct {
|
|
|
|
Ok bool
|
2015-07-02 18:39:39 +00:00
|
|
|
Result Message
|
2015-07-02 09:05:50 +00:00
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(response_json, &response_recieved)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !response_recieved.Ok {
|
|
|
|
return SendError{response_recieved.Description}
|
|
|
|
}
|
|
|
|
|
2015-07-02 18:39:39 +00:00
|
|
|
thumbnails := &response_recieved.Result.Photo
|
|
|
|
photo.File = (*thumbnails)[len(*thumbnails)-1].File
|
|
|
|
|
2015-07-02 09:05:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
2015-07-03 12:27:18 +00:00
|
|
|
|
|
|
|
func api_sendAudio(token string, recipient User, audio *Audio) error {
|
|
|
|
params := url.Values{}
|
|
|
|
params.Set("chat_id", strconv.Itoa(recipient.Id))
|
|
|
|
|
|
|
|
var response_json []byte
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if audio.Exists() {
|
|
|
|
params.Set("audio", audio.FileId)
|
|
|
|
response_json, err = api_GET("sendAudio", token, params)
|
|
|
|
} else {
|
|
|
|
response_json, err = api_POST("sendAudio", token, "audio",
|
|
|
|
audio.filename, params)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var response_recieved struct {
|
|
|
|
Ok bool
|
|
|
|
Result Message
|
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(response_json, &response_recieved)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !response_recieved.Ok {
|
|
|
|
return SendError{response_recieved.Description}
|
|
|
|
}
|
|
|
|
|
|
|
|
*audio = response_recieved.Result.Audio
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|