From ba3e7a59c1304c78d5bbf026b4ff851fdf4d2750 Mon Sep 17 00:00:00 2001 From: ptrcnull Date: Fri, 2 Oct 2020 14:53:42 +0200 Subject: [PATCH] feat: Initial commit --- client.go | 68 +++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 +++ types.go | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 147 insertions(+) create mode 100644 client.go create mode 100644 go.mod create mode 100644 types.go diff --git a/client.go b/client.go new file mode 100644 index 0000000..c8258b2 --- /dev/null +++ b/client.go @@ -0,0 +1,68 @@ +package telegram + +import ( + "encoding/json" + "io/ioutil" + "log" + "net/http" + "net/url" + "strconv" +) + +type Client struct { + Key string + Offset int64 +} + +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) + if err != nil { + return res, err + } + + 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 + } + + return res, err +} + +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 { + for key, val := range vals { + values[key] = val + } + } + + response, err := http.PostForm("https://api.telegram.org/"+c.Key+"/sendMessage", values) + if err != nil { + return nil, err + } + + defer func() { + err = response.Body.Close() + }() + + resbody, err := ioutil.ReadAll(response.Body) + if err != nil { + return nil, err + } + + var res SendMessageResponse + err = json.Unmarshal(resbody, &res) + return &res, err +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6a42c77 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.ddd.codes/ptrcnull/telegram + +go 1.15 diff --git a/types.go b/types.go new file mode 100644 index 0000000..a4ec679 --- /dev/null +++ b/types.go @@ -0,0 +1,76 @@ +package telegram + +type Response struct { + Ok bool `json:"ok"` + Result []Update `json:"result"` +} + +type Author struct { + ID int64 `json:"id"` + IsBot bool `json:"is_bot"` + FirstName string `json:"first_name"` + LastName string `json:"last_name"` + Username string `json:"username"` +} + +type Update struct { + UpdateID int64 `json:"update_id"` + Message *Message `json:"message"` + EditedMessage *Message `json:"edited_message"` +} + +type Chat struct { + ID int64 `json:"id"` + Title string `json:"title"` + Type string `json:"type"` +} + +type Sticker struct { + Width int `json:"width"` + Height int `json:"height"` + SetName string `json:"set_name"` + IsAnimated bool `json:"is_animated"` + FileId string `json:"file_id"` + FileUniqueId string `json:"file_unique_id"` + FileSize int64 `json:"file_size"` +} + +type File struct { + FileID string `json:"file_id"` + FileUniqueId string `json:"file_unique_id"` + FileSize int64 `json:"file_size"` + Width int `json:"width"` + Height int `json:"height"` +} + +type PollOption struct { + Text string `json:"text"` + VoterCount int `json:"voter_count"` +} + +type Poll struct { + ID string `json:"id"` + Question string `json:"question"` + Options []PollOption `json:"options"` + IsClosed bool `json:"is_closed"` +} + +type Message struct { + MessageID int64 `json:"message_id"` + From Author `json:"from"` + Text string `json:"text"` + Caption *string `json:"caption"` + Chat *Chat `json:"chat"` + Sticker *Sticker `json:"sticker"` + Photo []File `json:"photo"` + Document *File `json:"document"` + NewChatTitle *string `json:"new_chat_title"` + LeftChatParticipant *Author `json:"left_chat_participant"` + NewChatParticipant *Author `json:"new_chat_participant"` + ReplyToMessage *Message `json:"reply_to_message"` +} + +type SendMessageResponse struct { + Ok bool `json:"ok"` + Result Message `json:"result"` +}