Merge pull request #416 from quenbyako/master

[FEAT]: Add SentFrom method to Update type
bot-api-6.1
Syfaro 2021-11-08 15:38:29 -05:00 committed by GitHub
commit f413d8270c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 49 additions and 0 deletions

View File

@ -116,6 +116,55 @@ type Update struct {
ChatJoinRequest *ChatJoinRequest `json:"chat_join_request"` ChatJoinRequest *ChatJoinRequest `json:"chat_join_request"`
} }
// SentFrom returns the user who sent an update. Can be nil, if Telegram did not provide information
// about the user in the update object.
func (u *Update) SentFrom() *User {
switch {
case u.Message != nil:
return u.Message.From
case u.EditedMessage != nil:
return u.EditedMessage.From
case u.InlineQuery != nil:
return u.InlineQuery.From
case u.ChosenInlineResult != nil:
return u.ChosenInlineResult.From
case u.CallbackQuery != nil:
return u.CallbackQuery.From
case u.ShippingQuery != nil:
return u.ShippingQuery.From
case u.PreCheckoutQuery != nil:
return u.PreCheckoutQuery.From
default:
return nil
}
}
// CallbackData returns the callback query data, if it exists.
func (u *Update) CallbackData() string {
if u.CallbackQuery != nil {
return u.CallbackQuery.Data
}
return ""
}
// FromChat returns the chat where an update occured.
func (u *Update) FromChat() *Chat {
switch {
case u.Message != nil:
return u.Message.Chat
case u.EditedMessage != nil:
return u.EditedMessage.Chat
case u.ChannelPost != nil:
return u.ChannelPost.Chat
case u.EditedChannelPost != nil:
return u.EditedChannelPost.Chat
case u.CallbackQuery != nil:
return u.CallbackQuery.Message.Chat
default:
return nil
}
}
// UpdatesChannel is the channel for getting updates. // UpdatesChannel is the channel for getting updates.
type UpdatesChannel <-chan Update type UpdatesChannel <-chan Update