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-03 18:54:40 +00:00
|
|
|
func sendCommand(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-03 18:54:40 +00:00
|
|
|
func sendFile(method, token, name, path string, params url.Values) ([]byte, error) {
|
2015-07-02 18:39:39 +00:00
|
|
|
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-07-06 13:46:19 +00:00
|
|
|
func embedSendOptions(params *url.Values, options *SendOptions) {
|
|
|
|
if params == nil || options == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.ReplyTo.Id != 0 {
|
|
|
|
params.Set("reply_to_message_id", strconv.Itoa(options.ReplyTo.Id))
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.DisableWebPagePreview {
|
|
|
|
params.Set("disable_web_page_preview", "true")
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.ForceReply.Require {
|
|
|
|
forceReply, _ := json.Marshal(options.ForceReply)
|
|
|
|
params.Set("reply_markup", string(forceReply))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 18:54:40 +00:00
|
|
|
func getMe(token string) (User, error) {
|
|
|
|
me_json, err := sendCommand("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
|
|
|
|
}
|
2015-07-04 10:12:19 +00:00
|
|
|
|
|
|
|
return User{}, AuthError{bot_info.Description}
|
2015-06-26 07:34:10 +00:00
|
|
|
}
|
|
|
|
|
2015-07-03 18:54:40 +00:00
|
|
|
func 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))
|
2015-07-03 18:54:40 +00:00
|
|
|
updates_json, err := sendCommand("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
|
|
|
|
}
|