forked from igungor/telegram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype.go
286 lines (237 loc) · 7.79 KB
/
type.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package tlbot
import (
"bytes"
"fmt"
"io"
"strings"
"time"
)
type Action string
// Types of actions to broadcast
const (
Typing Action = "typing"
UploadingPhoto Action = "upload_photo"
UploadingVideo Action = "upload_video"
UploadingAudio Action = "upload_audio"
UploadingDocument Action = "upload_document"
FindingLocation Action = "find_location"
)
// User represents a Telegram user or bot.
type User struct {
ID int `json:"id"`
Username string `json:"username"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
}
// Chat represents a Telegram chat.
type Chat struct {
ID int `json:"id"`
Type string `json:"type"`
Title string `json:"title"`
Username string `json:"username"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
}
// IsGroupChat reports whether the message is originally sent from a chat group.
func (c Chat) IsGroupChat() bool { return c.Type == "group" }
type Update struct {
ID int `json:"update_id"`
Payload Message `json:"message"`
}
// Message represents a message to be sent.
type Message struct {
// Unique message identifier
ID int `json:"message_id"`
// Sender (optional. can be empty for messages sent to channel)
From User `json:"from"`
// Date is when the message was sent in Unix time
Unixtime int `json:"date"`
// Conversation the message belongs to — user in case of a private chat,
// group in case of a group chat
Chat Chat `json:"chat"`
// For forwarded messages, sender of the original message (Optional)
ForwardFrom User `json:"forward_from"`
// For forwarded messages, date the original message was sent in
// Unix time (Optional)
ForwardDate int `json:"forward_date"`
// For replies, the original message. Note that the Message
// object in this field will not contain further reply_to_message fields
// even if it itself is a reply (Optional)
ReplyTo *Message `json:"reply_to_message"`
// For text messages, the actual UTF-8 text of the message (Optional)
Text string `json:"text"`
// Message is an audio file, information about the file (Optional)
Audio Audio `json:"audio"`
// Message is a general file, information about the file (Optional)
Document Document `json:"document"`
// Message is a photo, available sizes of the photo (Optional)
Photos []Photo `json:"photo"`
// Message is a sticker, information about the sticker (Optional)
Sticker Sticker `json:"sticker"`
// Message is a video, information about the video (Optional)
Video Video `json:"video"`
// Message is a shared contact, information about the contact (Optional)
Contact Contact `json:"contact"`
// Message is a shared location, information about the location (Optional)
Location Location `json:"location"`
// A new member was added to the group, information about them
// (this member may be bot itself) (Optional)
JoinedUser User `json:"new_chat_participant"`
// A member was removed from the group, information about them
// (this member may be bot itself) (Optional)
LeftUser User `json:"left_chat_participant"`
// A group title was changed to this value (Optional)
NewChatTitle string `json:"new_chat_title"`
// A group photo was change to this value (Optional)
NewChatPhoto []Photo `json:"new_chat_photo"`
// Informs that the group photo was deleted (Optional)
ChatPhotoDeleted bool `json:"delete_chat_photo"`
// Informs that the group has been created (Optional)
GroupChatCreated bool `json:"group_chat_created"`
}
// String returns a human-readable representation of Message.
func (m Message) String() string {
var buf bytes.Buffer
if m.Chat.IsGroupChat() {
buf.WriteString(fmt.Sprintf(`From user("%v %v [%v - %v] in Group(%v [%v])" `, m.From.FirstName, m.From.LastName, m.From.Username, m.From.ID, m.Chat.Title, m.Chat.ID))
} else {
buf.WriteString(fmt.Sprintf(`From user("%v %v [%v - %v])" `, m.From.FirstName, m.From.LastName, m.From.Username, m.From.ID))
}
buf.WriteString(fmt.Sprintf("Message: %q", m.Text))
return buf.String()
}
// Command returns the command's name: the first word in the message text. If
// message text starts with a `/`, function returns the command name, or else
// empty string.
func (m Message) Command() string {
name := m.Text
i := strings.Index(name, " ")
if i >= 0 {
name = name[:i]
}
j := strings.Index(name, "/")
if j < 0 {
return ""
}
return name[j+1:]
}
// Args returns all words after the first word in the message text. First word
// is meant to be the command name and can be accessed with Command method.
func (m Message) Args() []string {
args := strings.TrimSpace(m.Text)
i := strings.Index(args, " ")
if i < 0 {
return nil
}
return strings.Fields(args[i+1:])
}
// IsService reports whether the message is a Telegram service message, not a
// user sent message.
func (m Message) IsService() bool {
switch {
case m.NewChatTitle != "":
return true
case len(m.NewChatPhoto) > 0:
return true
case m.JoinedUser != User{}:
return true
case m.LeftUser != User{}:
return true
case m.GroupChatCreated:
return true
case m.ChatPhotoDeleted:
return true
}
return false
}
// IsReply reports whether the message is a reply to another message.
func (m Message) IsReply() bool {
return m.ReplyTo != nil
}
// Time returns the moment of message in UTC time.
func (m Message) Time() time.Time {
return time.Unix(int64(m.Unixtime), 0).UTC()
}
type File struct {
// File is embedded in most of the types. So a `File` prefix is used
FileID string `json:"file_id"`
FileSize int `json:"file_size"`
FilePath string `json:"file_path"`
Name string `json:"-"`
Body io.Reader `json:"-"`
URL string `json:"-"`
}
// Exists reports whether the file is already at Telegram servers.
func (f File) Exists() bool { return f.FileID != "" }
// Photo represents one size of a photo or a file/sticker thumbnail.
type Photo struct {
File
Width int `json:"width"`
Height int `json:"height"`
Caption string `json:"caption"`
}
// Audio represents an audio file.
type Audio struct {
File
Duration int `json:"duration"`
Performer string `json:"performer"`
Title string `json:"title"`
MimeType string `json:"mime_type"`
}
// Document represents a general file (as opposed to photos and audio files).
type Document struct {
File
Filename string `json:"file_name"`
Thumbnail Photo `json:"thumb"`
MimeType string `json:"mime_type"`
}
// Sticker represents a sticker.
type Sticker struct {
File
Width int `json:"width"`
Height int `json:"height"`
Thumbnail Photo `json:"thumb"`
}
// Video represents a video file.
type Video struct {
File
Width int `json:"width"`
Height int `json:"height"`
Duration int `json:"duration"`
Thumbnail Photo `json:"thumb"`
MimeType string `json:"mime_type"`
Caption string `json:"caption"`
}
// Voice represents an voice note.
type Voice struct {
File
Duration int `json:"duration"`
MimeType string `json:"mime_type"`
}
// Location represents a point on the map.
type Location struct {
Lat float64 `json:"latitude"`
Long float64 `json:"longitude"`
}
// Venue represents a venue
type Venue struct {
Location Location `json:"location"`
Title string `json:"title"`
Address string `json:"address"`
FoursquareID string `json:"foursquare_id"`
}
// Contact represents a phone contact.
type Contact struct {
PhoneNumber string `json:"phone_number"`
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
UserID string `json:"user_id"`
}
type ReplyMarkup struct {
Keyboard [][]string `json:"keyboard,omitempty"`
Resize bool `json:"resize_keyboard,omitempty"`
OneTime bool `json:"one_time_keyboard,omitempty"`
Selective bool `json:"selective,omitempty"`
Hide bool `json:"hide_keyboard,omitempty"`
ForceReply bool `json:"force_reply,omitempty"`
}