|
|
|
@ -4,8 +4,9 @@ import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"log"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/url"
|
|
|
|
|
"strconv"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func api_getMe(token string) (User, error) {
|
|
|
|
@ -40,9 +41,7 @@ func api_getMe(token string) (User, error) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func api_getUpdates(token string,
|
|
|
|
|
offset int,
|
|
|
|
|
updates chan<- Update) error {
|
|
|
|
|
func api_getUpdates(token string, offset int, updates chan<- Update) error {
|
|
|
|
|
command := fmt.Sprintf("getUpdates?offset=%d", offset)
|
|
|
|
|
request := "https://api.telegram.org/bot" + token + "/" + command
|
|
|
|
|
|
|
|
|
@ -64,9 +63,11 @@ func api_getUpdates(token string,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = json.Unmarshal(updates_json, &updates_recieved)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !updates_recieved.Ok {
|
|
|
|
|
log.Println(FetchError{updates_recieved.Description})
|
|
|
|
|
return FetchError{updates_recieved.Description}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -76,3 +77,40 @@ func api_getUpdates(token string,
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func api_sendMessage(token string, recipient User, text string) error {
|
|
|
|
|
resource := "https://api.telegram.org/bot" + token + "/sendMessage"
|
|
|
|
|
|
|
|
|
|
params := url.Values{}
|
|
|
|
|
params.Set("chat_id", strconv.FormatInt(int64(recipient.Id), 10))
|
|
|
|
|
params.Set("text", text)
|
|
|
|
|
|
|
|
|
|
request := resource + "?" + params.Encode()
|
|
|
|
|
|
|
|
|
|
resp, err := http.Get(request)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
response_json, err := ioutil.ReadAll(resp.Body)
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|