-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontext.go
More file actions
121 lines (97 loc) · 3.7 KB
/
Copy pathcontext.go
File metadata and controls
121 lines (97 loc) · 3.7 KB
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
package botapi
import "context"
// Message returns the message the update carries (new/edited message or channel
// post), or nil.
func (c *Context) Message() *Message { return c.Update.EffectiveMessage() }
// Sender returns the user who produced the update (message sender or callback/
// inline-query sender), or nil when there is none.
func (c *Context) Sender() *User {
switch {
case c.Update.CallbackQuery != nil:
return &c.Update.CallbackQuery.From
case c.Update.InlineQuery != nil:
return &c.Update.InlineQuery.From
}
if m := c.Message(); m != nil {
return m.From
}
return nil
}
// Chat returns the target chat id of the update and whether one is present.
func (c *Context) Chat() (ChatID, bool) {
if m := c.Message(); m != nil {
return ID(m.Chat.ID), true
}
if bm := c.BusinessMessage(); bm != nil {
if target, ok := businessChatID(bm); ok {
return target, true
}
return ID(bm.Chat.ID), true
}
if cq := c.Update.CallbackQuery; cq != nil && cq.Message != nil {
return ID(cq.Message.Chat.ID), true
}
return nil, false
}
// Send sends a text message to the update's chat. For a business update it sends
// on behalf of the connected account.
func (c *Context) Send(text string, opts ...SendOption) (*Message, error) {
chat, ok := c.Chat()
if !ok {
return nil, &Error{Code: 400, Description: "Bad Request: update has no chat to send to"}
}
if id := c.Update.businessConnectionID(); id != "" {
opts = append([]SendOption{WithBusinessConnection(id)}, opts...)
}
return c.Bot.SendMessage(c, chat, text, opts...)
}
// Reply sends a text message to the update's chat as a reply to the incoming
// message. For a business message it replies on behalf of the connected account.
func (c *Context) Reply(text string, opts ...SendOption) (*Message, error) {
if bm := c.BusinessMessage(); bm != nil {
opts = append([]SendOption{
ReplyTo(bm.MessageID),
WithBusinessConnection(bm.BusinessConnectionID),
}, opts...)
target, ok := businessChatID(bm)
if !ok {
target = ID(bm.Chat.ID)
}
return c.Bot.SendMessage(c, target, text, opts...)
}
m := c.Message()
if m == nil {
return nil, &Error{Code: 400, Description: "Bad Request: update has no message to reply to"}
}
opts = append([]SendOption{ReplyTo(m.MessageID)}, opts...)
return c.Bot.SendMessage(c, ID(m.Chat.ID), text, opts...)
}
// AnswerCallback answers the update's callback query. It is an error to call it
// when the update is not a callback query.
func (c *Context) AnswerCallback(opts ...AnswerCallbackQueryOption) error {
cq := c.Update.CallbackQuery
if cq == nil {
return &Error{Code: 400, Description: "Bad Request: update has no callback query to answer"}
}
return c.Bot.AnswerCallbackQuery(c, cq.ID, opts...)
}
// Background returns a context tied to the bot's run lifetime, for sends that
// must outlive this handler (a timer, queue or goroutine). The handler's own
// context is per-update and may be canceled (e.g. by Timeout middleware) as soon
// as the handler returns, so do not capture it for background work.
//
// Send messages to any chat in the background with Bot.SendMessage and this
// context:
//
// ctx := c.Background()
// go func() { c.Bot.SendMessage(ctx, botapi.ID(other), "hi") }()
func (c *Context) Background() context.Context { return c.Bot.Background() }
// AnswerInline answers the update's inline query with the given results. It is
// an error to call it when the update is not an inline query.
func (c *Context) AnswerInline(results []InlineQueryResult, opts ...AnswerInlineQueryOption) error {
iq := c.Update.InlineQuery
if iq == nil {
return &Error{Code: 400, Description: "Bad Request: update has no inline query to answer"}
}
return c.Bot.AnswerInlineQuery(c, iq.ID, results, opts...)
}