-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.go
95 lines (83 loc) · 2.09 KB
/
context.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
package telebot
import (
"sync"
)
// HandlerFunc represents a handler function, which is used to handle actual endpoints.
type HandlerFunc func(Context) error
// nativeContext is a native implementation of the Context interface; "context" is taken by context package, maybe there is a better name.
type nativeContext struct {
b *bot
u Update
lock sync.RWMutex
store map[string]any
}
func (c *nativeContext) Sender() *User {
switch {
case c.u.CallbackQuery != nil:
return c.u.CallbackQuery.Sender
case c.Message() != nil:
return c.Message().From
case c.u.Query != nil:
return &c.u.Query.From
case c.u.InlineResult != nil:
return &c.u.InlineResult.From
case c.u.ShippingQuery != nil:
return &c.u.ShippingQuery.From
case c.u.PreCheckoutQuery != nil:
return &c.u.PreCheckoutQuery.From
case c.u.PollAnswer != nil:
return c.u.PollAnswer.User
case c.u.MyChatMember != nil:
return &c.u.MyChatMember.From
case c.u.ChatMember != nil:
return &c.u.ChatMember.From
case c.u.ChatJoinRequest != nil:
return &c.u.ChatJoinRequest.From
default:
return nil
}
}
func (c *nativeContext) Bot() Bot {
return c.b
}
func (c *nativeContext) Update() Update {
return c.u
}
func (c *nativeContext) Message() *AccessibleMessage {
switch {
case c.u.Message != nil:
return c.u.Message
case c.u.CallbackQuery != nil:
return c.u.CallbackQuery.Message.AccessibleMessage
case c.u.EditedMessage != nil:
return c.u.EditedMessage
case c.u.ChannelPost != nil:
if c.u.ChannelPost.PinnedMessage != nil {
return c.u.ChannelPost.PinnedMessage.AccessibleMessage
}
return c.u.ChannelPost
case c.u.EditedChannelPost != nil:
return c.u.EditedChannelPost
default:
return nil
}
}
func (c *nativeContext) Callback() *Callback {
return c.u.CallbackQuery
}
func (c *nativeContext) Query() *InlineQuery {
return c.u.Query
}
func (c *nativeContext) Set(key string, value any) {
c.lock.Lock()
defer c.lock.Unlock()
if c.store == nil {
c.store = make(map[string]any)
}
c.store[key] = value
}
func (c *nativeContext) Get(key string) any {
c.lock.RLock()
defer c.lock.RUnlock()
return c.store[key]
}