Skip to content

Commit

Permalink
add helper to possible inaccessible messages to turn them into regula…
Browse files Browse the repository at this point in the history
…r messages
  • Loading branch information
PaulSonOfLars committed Jan 8, 2024
1 parent 158f3df commit e99a8f6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
10 changes: 10 additions & 0 deletions custom_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ func (im InaccessibleMessage) Reply(b *Bot, text string, opts *SendMessageOpts)
return b.SendMessage(im.Chat.Id, text, opts)
}

// ToMessage is a helper function to simplify dealing with telegram's message nonsense.
// It populates a standard message object with all of InaccessibleMessage's shared fields.
func (im InaccessibleMessage) ToMessage() *Message {
return &Message{
MessageId: im.MessageId,
Date: im.Date,
Chat: im.Chat,
}
}

// SendMessage is a helper function to easily call Bot.SendMessage in a chat.
func (c Chat) SendMessage(b *Bot, text string, opts *SendMessageOpts) (*Message, error) {
return b.SendMessage(c.Id, text, opts)
Expand Down
11 changes: 7 additions & 4 deletions ext/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ type Context struct {
// such as admin checks.
Data map[string]interface{}

// EffectiveMessage is the message which triggered the update, if available (eg: update contains an accessible message).
// EffectiveMessage is the message which triggered the update, if available.
// If the message is an InaccessibleMessage (eg, from a callbackquery), the message contents may be inaccessible.
EffectiveMessage *gotgbot.Message
// EffectiveChat is the chat the update was triggered in, if possible.
EffectiveChat *gotgbot.Chat
Expand Down Expand Up @@ -71,10 +72,12 @@ func NewContext(update *gotgbot.Update, data map[string]interface{}) *Context {
user = &update.CallbackQuery.From

if update.CallbackQuery.Message != nil {
// Note: This check means that we do not populate the EffectiveMessage field in the case of an inacessible message.
m, ok := update.CallbackQuery.Message.(gotgbot.Message)
if ok {
switch m := update.CallbackQuery.Message.(type) {
case gotgbot.Message:
msg = &m
case gotgbot.InaccessibleMessage:
// Note: This conversion means that EffectiveMessage may not contain all Message fields
msg = m.ToMessage()
}

tmpC := update.CallbackQuery.Message.GetChat()
Expand Down

0 comments on commit e99a8f6

Please sign in to comment.