-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram.go
215 lines (167 loc) · 3.59 KB
/
telegram.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package telegram
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
const botApiUrl string = "https://api.telegram.org"
type ResponceResult interface{}
type Responce struct {
Ok bool
Description string
Result ResponceResult
}
type UserResponce struct {
Responce
Result *User
}
type ChatMemberResponce struct {
Responce
Result *ChatMember
}
type MessageResponce struct {
Responce
Result *TMessage
}
type UpdateResponse struct {
Responce
Result []*Update
}
type User struct {
Id int
First_Name string
Last_Name string
Username string
}
type ChatMember struct {
ChatUser *User `json:"user"`
Status string `json:"status"`
}
type TChat struct {
Id int
Type string
Title string
Username string
First_Name string
Last_Name string
}
type TMessage struct {
Message_id int
From *User
Date int
Chat *TChat
Text string
}
type Update struct {
Update_id int
Message *TMessage
}
type MessageParams struct {
Chat_id int `json:"chat_id"`
Text string `json:"text"`
}
type ChatMemberParams struct {
Chat_id int `json:"chat_id"`
User_id int `json:"user_id"`
}
type GetUpdatesParams struct {
Offset int `json:"offset"`
Limit int `json:"limit"`
Timeout int `json:"timeout"`
}
func responceFromJson(pJson []byte, res interface{}) {
err := json.Unmarshal(pJson, res)
if err != nil {
log.Fatal(err)
}
}
func dataToJson(data interface{}) []byte {
res, err := json.Marshal(data)
if err != nil {
log.Fatal(err)
}
return res
}
type BotApi struct {
BotToken string
AllowedChats []int
Roulette []int
}
func (ba *BotApi) runMethod(method string, params interface{}) []byte {
url := fmt.Sprintf("%s/bot%s/%s", botApiUrl, ba.BotToken, method)
data := dataToJson(params)
resp, err := http.Post(url, "application/json", bytes.NewBuffer(data))
if err != nil {
log.Fatal(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
resp.Body.Close()
return body
}
func (ba *BotApi) CheckMessage(m *TMessage) bool {
if m == nil {
log.Println("Nil message")
return false
}
for _, v := range ba.AllowedChats {
if v == m.Chat.Id {
return true
}
}
log.Printf("Not allowed chat: %d\n", m.Chat.Id)
return false
}
func (ba *BotApi) GetMe() User {
r := ba.runMethod("getMe", nil)
var res UserResponce
responceFromJson(r, &res)
if !res.Ok {
log.Println("Not OK\n", res.Description)
}
return *res.Result
}
func (ba *BotApi) SendMessage(chat_id int, text string) TMessage {
m := MessageParams{Chat_id: chat_id, Text: text}
r := ba.runMethod("sendMessage", m)
var mr MessageResponce
responceFromJson(r, &mr)
if !mr.Ok {
log.Println("Not OK\n", mr.Description)
}
return *mr.Result
}
func (ba *BotApi) KickChatMember(chat_id int, user_id int) bool {
r := ba.runMethod("kickChatMember", ChatMemberParams{chat_id, user_id})
var resp Responce
responceFromJson(r, &resp)
if !resp.Ok {
log.Println("Not OK\n", resp.Description)
ba.SendMessage(chat_id, resp.Description)
}
return resp.Ok
}
func (ba *BotApi) GetChatMember(chat_id int, user_id int) ChatMember {
r := ba.runMethod("getChatMember", ChatMemberParams{chat_id, user_id})
var resp ChatMemberResponce
responceFromJson(r, &resp)
if !resp.Ok {
log.Println("Not OK\n", resp.Description)
}
return *resp.Result
}
func (ba *BotApi) GetUpdates(offset int) []*Update {
p := GetUpdatesParams{Offset: offset, Limit: 100}
r := ba.runMethod("getUpdates", p)
var ur UpdateResponse
responceFromJson(r, &ur)
if !ur.Ok {
log.Println("Not OK", ur.Description)
}
return ur.Result
}