forked from tucnak/telebot
/
message.go
205 lines (162 loc) · 5.29 KB
/
message.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
package telebot
import (
"time"
)
// Message object represents a message.
type Message struct {
ID int `json:"message_id"`
// For message sent to channels, Sender may be empty
Sender User `json:"from"`
Unixtime int `json:"date"`
// For forwarded messages, sender of the original message.
OriginalSender User `json:"forward_from"`
// For forwarded messages, chat of the original message when forwarded from a channel.
OriginalChat Chat `json:"forward_from_chat"`
// For forwarded messages, unixtime of the original message.
OriginalUnixtime int `json:"forward_date"`
// For replies, ReplyTo represents the original message.
// Note that the Message object in this field will not
// contain further ReplyTo fields even if it
// itself is a reply.
ReplyTo *Message `json:"reply_to_message"`
// For a text message, the actual UTF-8 text of the message
Text string `json:"text"`
// For an audio recording, information about it.
Audio Audio `json:"audio"`
// For a general file, information about it.
Document Document `json:"document"`
// For a photo, available thumbnails.
Photo []Thumbnail `json:"photo"`
// For a sticker, information about it.
Sticker Sticker `json:"sticker"`
// For a video, information about it.
Video Video `json:"video"`
// For a contact, contact information itself.
Contact Contact `json:"contact"`
// For a location, its longitude and latitude.
Location Location `json:"location"`
// A group chat message belongs to, empty if personal.
Chat Chat `json:"chat"`
// For a service message, represents a user,
// that just got added to chat, this message came from.
//
// Sender leads to User, capable of invite.
//
// UserJoined might be the Bot itself.
UserJoined User `json:"new_chat_member"`
// For a service message, represents a user,
// that just left chat, this message came from.
//
// If user was kicked, Sender leads to a User,
// capable of this kick.
//
// UserLeft might be the Bot itself.
UserLeft User `json:"left_chat_member"`
// For a service message, represents a new title
// for chat this message came from.
//
// Sender would lead to a User, capable of change.
NewChatTitle string `json:"new_chat_title"`
// For a service message, represents all available
// thumbnails of new chat photo.
//
// Sender would lead to a User, capable of change.
NewChatPhoto []Thumbnail `json:"new_chat_photo"`
// For a service message, true if chat photo just
// got removed.
//
// Sender would lead to a User, capable of change.
ChatPhotoDeleted bool `json:"delete_chat_photo"`
// For a service message, true if group has been created.
//
// You would recieve such a message if you are one of
// initial group chat members.
//
// Sender would lead to creator of the chat.
ChatCreated bool `json:"group_chat_created"`
// For a service message, true if super group has been created.
//
// You would recieve such a message if you are one of
// initial group chat members.
//
// Sender would lead to creator of the chat.
SuperGroupCreated bool `json:"supergroup_chat_created"`
// For a service message, true if channel has been created.
//
// You would recieve such a message if you are one of
// initial channel administrators.
//
// Sender would lead to creator of the chat.
ChannelCreated bool `json:"channel_chat_created"`
// For a service message, the destination (super group) you
// migrated to.
//
// You would recieve such a message when your chat has migrated
// to a super group.
//
// Sender would lead to creator of the migration.
MigrateTo int64 `json:"migrate_to_chat_id"`
// For a service message, the Origin (normal group) you migrated
// from.
//
// You would recieve such a message when your chat has migrated
// to a super group.
//
// Sender would lead to creator of the migration.
MigrateFrom int64 `json:"migrate_from_chat_id"`
Entities []MessageEntity `json:"entities,omitempty"`
Caption string `json:"caption,omitempty"`
}
// Origin returns an origin of message: group chat / personal.
func (m *Message) Origin() User {
// if m.IsPersonal() {
// return m.Chat
// }
return m.Sender
}
// Time returns the moment of message creation in local time.
func (m *Message) Time() time.Time {
return time.Unix(int64(m.Unixtime), 0)
}
// IsForwarded says whether message is forwarded copy of another
// message or not.
func (m *Message) IsForwarded() bool {
return m.OriginalSender != User{} || m.OriginalChat != Chat{}
}
// IsReply says whether message is reply to another message or not.
func (m *Message) IsReply() bool {
return m.ReplyTo != nil
}
// IsPersonal returns true, if message is a personal message,
// returns false if sent to group chat.
func (m *Message) IsPersonal() bool {
return !m.Chat.IsGroupChat()
}
// IsService returns true, if message is a service message,
// returns false otherwise.
//
// Service messages are automatically sent messages, which
// typically occur on some global action. For instance, when
// anyone leaves the chat or chat title changes.
func (m *Message) IsService() bool {
service := false
if (m.UserJoined != User{}) {
service = true
}
if (m.UserLeft != User{}) {
service = true
}
if m.NewChatTitle != "" {
service = true
}
if len(m.NewChatPhoto) > 0 {
service = true
}
if m.ChatPhotoDeleted {
service = true
}
if m.ChatCreated {
service = true
}
return service
}