Skip to content

Commit f413d82

Browse files
authored
Merge pull request #416 from quenbyako/master
[FEAT]: Add SentFrom method to Update type
2 parents 2a2f7c5 + 8806fb3 commit f413d82

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

types.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,55 @@ type Update struct {
116116
ChatJoinRequest *ChatJoinRequest `json:"chat_join_request"`
117117
}
118118

119+
// SentFrom returns the user who sent an update. Can be nil, if Telegram did not provide information
120+
// about the user in the update object.
121+
func (u *Update) SentFrom() *User {
122+
switch {
123+
case u.Message != nil:
124+
return u.Message.From
125+
case u.EditedMessage != nil:
126+
return u.EditedMessage.From
127+
case u.InlineQuery != nil:
128+
return u.InlineQuery.From
129+
case u.ChosenInlineResult != nil:
130+
return u.ChosenInlineResult.From
131+
case u.CallbackQuery != nil:
132+
return u.CallbackQuery.From
133+
case u.ShippingQuery != nil:
134+
return u.ShippingQuery.From
135+
case u.PreCheckoutQuery != nil:
136+
return u.PreCheckoutQuery.From
137+
default:
138+
return nil
139+
}
140+
}
141+
142+
// CallbackData returns the callback query data, if it exists.
143+
func (u *Update) CallbackData() string {
144+
if u.CallbackQuery != nil {
145+
return u.CallbackQuery.Data
146+
}
147+
return ""
148+
}
149+
150+
// FromChat returns the chat where an update occured.
151+
func (u *Update) FromChat() *Chat {
152+
switch {
153+
case u.Message != nil:
154+
return u.Message.Chat
155+
case u.EditedMessage != nil:
156+
return u.EditedMessage.Chat
157+
case u.ChannelPost != nil:
158+
return u.ChannelPost.Chat
159+
case u.EditedChannelPost != nil:
160+
return u.EditedChannelPost.Chat
161+
case u.CallbackQuery != nil:
162+
return u.CallbackQuery.Message.Chat
163+
default:
164+
return nil
165+
}
166+
}
167+
119168
// UpdatesChannel is the channel for getting updates.
120169
type UpdatesChannel <-chan Update
121170

0 commit comments

Comments
 (0)