feat: Add EditMessageText, change client.Key to client.Token, add "bot" to token automatically, add Get and Post methods

This commit is contained in:
ptrcnull 2021-03-10 19:09:59 +01:00
parent cf691c6476
commit e60c53ded4

View file

@ -3,7 +3,6 @@ package telegram
import ( import (
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"log"
"net/http" "net/http"
"net/url" "net/url"
"strconv" "strconv"
@ -13,19 +12,36 @@ import (
const badChars = "_*[]()~`>#+-=|{}.!" const badChars = "_*[]()~`>#+-=|{}.!"
type Client struct { type Client struct {
Key string Token string
Offset int64 Offset int64
PollTimeout int64
}
func (c *Client) Get(url string) ([]byte, error) {
resp, err := http.Get("https://api.telegram.org/bot" + c.Token + url)
if err != nil {
return nil, err
}
return ioutil.ReadAll(resp.Body)
}
func (c *Client) Post(url string, data url.Values) ([]byte, error) {
res, err := http.PostForm("https://api.telegram.org/bot"+c.Token+url, data)
if err != nil {
return nil, err
}
return ioutil.ReadAll(res.Body)
} }
func (c *Client) GetUpdates() (Response, error) { func (c *Client) GetUpdates() (Response, error) {
var res Response var res Response
resp, err := http.Get("https://api.telegram.org/" + c.Key + "/getUpdates?offset=" + strconv.FormatInt(c.Offset, 10)) values := url.Values{}
if err != nil { values.Set("offset", strconv.FormatInt(c.Offset, 10))
return res, err values.Set("timeout", strconv.FormatInt(c.PollTimeout, 10))
} body, err := c.Get("/getUpdates?" + values.Encode())
body, err := ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
return res, err return res, err
} }
@ -33,7 +49,6 @@ func (c *Client) GetUpdates() (Response, error) {
err = json.Unmarshal(body, &res) err = json.Unmarshal(body, &res)
if len(res.Result) > 0 { if len(res.Result) > 0 {
log.Println("Last update: ", res.Result[len(res.Result)-1].UpdateID)
c.Offset = res.Result[len(res.Result)-1].UpdateID + 1 c.Offset = res.Result[len(res.Result)-1].UpdateID + 1
} }
@ -52,32 +67,45 @@ func (c *Client) SendMarkdownMessage(chatId, body string) (*SendMessageResponse,
return c.SendMessage(chatId, body, url.Values{"parse_mode": {"MarkdownV2"}}) return c.SendMessage(chatId, body, url.Values{"parse_mode": {"MarkdownV2"}})
} }
func (c *Client) SendMessage(chatId, body string, options ...url.Values) (ret *SendMessageResponse, err error) { func mergeValues(a url.Values, b []url.Values) {
values := url.Values{ for _, vals := range b {
"chat_id": {chatId},
"text": {body},
}
for _, vals := range options {
for key, val := range vals { for key, val := range vals {
values[key] = val a[key] = val
} }
} }
}
response, err := http.PostForm("https://api.telegram.org/"+c.Key+"/sendMessage", values) func (c *Client) SendMessage(chatId, text string, options ...url.Values) (ret *SendMessageResponse, err error) {
if err != nil { values := url.Values{
return nil, err "chat_id": {chatId},
"text": {text},
} }
mergeValues(values, options)
defer func() { body, err := c.Post("/sendMessage", values)
err = response.Body.Close()
}()
resbody, err := ioutil.ReadAll(response.Body)
if err != nil { if err != nil {
return nil, err return nil, err
} }
var res SendMessageResponse var res SendMessageResponse
err = json.Unmarshal(resbody, &res) err = json.Unmarshal(body, &res)
return &res, err
}
func (c *Client) EditMessageText(chatId, messageId, text string, options ...url.Values) (ret *SendMessageResponse, err error) {
values := url.Values{
"chat_id": {chatId},
"message_id": {messageId},
"text": {text},
}
mergeValues(values, options)
body, err := c.Post("/editMessageText", values)
if err != nil {
return nil, err
}
var res SendMessageResponse
err = json.Unmarshal(body, &res)
return &res, err return &res, err
} }