feat: Initial commit
This commit is contained in:
commit
ba3e7a59c1
3 changed files with 147 additions and 0 deletions
68
client.go
Normal file
68
client.go
Normal file
|
@ -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
|
||||
}
|
3
go.mod
Normal file
3
go.mod
Normal file
|
@ -0,0 +1,3 @@
|
|||
module git.ddd.codes/ptrcnull/telegram
|
||||
|
||||
go 1.15
|
76
types.go
Normal file
76
types.go
Normal file
|
@ -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"`
|
||||
}
|
Loading…
Reference in a new issue