-
Notifications
You must be signed in to change notification settings - Fork 120
/
sender.go
149 lines (134 loc) · 4.28 KB
/
sender.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
package gotgbot
// Sender is a merge of the User and SenderChat fields of a message, to provide easier interaction with
// message senders from the telegram API.
type Sender struct {
// The User defined as the sender (if applicable)
User *User
// The Chat defined as the sender (if applicable)
Chat *Chat
// Whether the sender was an automatic forward; eg, a linked channel.
IsAutomaticForward bool
// The location that was sent to. Required to determine if the sender is a linked channel, an anonymous channel,
// or an anonymous admin.
ChatId int64
// The custom admin title of the anonymous group administrator sender.
// Only available if IsAnonymousAdmin is true.
AuthorSignature string
}
// GetSender populates the relevant fields of a Sender struct given a message.
func (m Message) GetSender() *Sender {
return &Sender{
User: m.From,
Chat: m.SenderChat,
IsAutomaticForward: m.IsAutomaticForward,
ChatId: m.Chat.Id,
AuthorSignature: m.AuthorSignature,
}
}
// GetSender populates the relevant fields of a Sender struct given a reaction.
func (mru MessageReactionUpdated) GetSender() *Sender {
return &Sender{
User: mru.User,
Chat: mru.ActorChat,
ChatId: mru.Chat.Id,
}
}
// GetSender populates the relevant fields of a Sender struct given a poll answer.
func (pa PollAnswer) GetSender() *Sender {
return &Sender{
User: pa.User,
Chat: pa.VoterChat,
}
}
// Id determines the sender ID.
// When a message is being sent by a chat/channel, telegram usually populates the User field with dummy values.
// For this reason, we prefer to return the Chat.Id if it is available, rather than a dummy User.Id.
func (s Sender) Id() int64 {
if s.Chat != nil {
return s.Chat.Id
}
if s.User != nil {
return s.User.Id
}
return 0
}
// Username determines the sender username.
func (s Sender) Username() string {
if s.Chat != nil {
return s.Chat.Username
}
if s.User != nil {
return s.User.Username
}
return ""
}
// Name determines the name of the sender.
// This is:
// - Chat.Title for a Chat.
// - User.FirstName + User.LastName for a User (the full name).
func (s Sender) Name() string {
if s.Chat != nil {
return s.Chat.Title
}
if s.User != nil {
if s.User.LastName == "" {
return s.User.FirstName
}
return s.User.FirstName + " " + s.User.LastName
}
return ""
}
// FirstName determines the firstname of the sender.
// This is:
// - Chat.Title for a Chat.
// - User.FirstName for a User.
func (s Sender) FirstName() string {
if s.Chat != nil {
return s.Chat.Title
}
if s.User != nil {
return s.User.FirstName
}
return ""
}
// LastName determines the firstname of the sender.
// This is:
// - empty for a Chat.
// - User.LastName for a User.
func (s Sender) LastName() string {
if s.Chat != nil {
return "" // empty; we define the "title" as being a firstname, so there is no lastname.
}
if s.User != nil {
return s.User.LastName
}
return ""
}
// IsUser returns true if the Sender is a User (including bot).
func (s Sender) IsUser() bool {
return s.Chat == nil && s.User != nil
}
// IsBot returns true if the Sender is a bot.
// Returns false if the user is a bot setup by telegram for backwards compatibility with
// the sender_chat fields.
func (s Sender) IsBot() bool {
return s.Chat == nil && s.User != nil && s.User.IsBot
}
// IsAnonymousAdmin returns true if the Sender is an anonymous admin sending to a group.
// For channel posts in a channel, see IsChannelPost.
func (s Sender) IsAnonymousAdmin() bool {
return s.Chat != nil && s.Chat.Id == s.ChatId && s.Chat.Type != "channel"
}
// IsChannelPost returns true if the Sender is a channel admin posting to that same channel.
func (s Sender) IsChannelPost() bool {
return s.Chat != nil && s.Chat.Id == s.ChatId && s.Chat.Type == "channel"
}
// IsAnonymousChannel returns true if the Sender is an anonymous channel sending to a group.
// For channel admins posting in their own channel, see IsChannelPost.
func (s Sender) IsAnonymousChannel() bool {
return s.Chat != nil && s.Chat.Id != s.ChatId && !s.IsAutomaticForward && s.Chat.Type == "channel"
}
// IsLinkedChannel returns true if the Sender is a linked channel sending to the group it is linked to.
func (s Sender) IsLinkedChannel() bool {
return s.Chat != nil && s.Chat.Id != s.ChatId && s.IsAutomaticForward
}