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