-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtelebot_context_wrap.go
50 lines (42 loc) · 1.23 KB
/
telebot_context_wrap.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
package fsm
import (
tele "gopkg.in/telebot.v3"
)
// fsmInternalKey needed for catch context requests.
const fsmInternalKey = "__fsm"
// wrapperContext wraps telebot context and adds fsm
// context inside.
// By this wrapper you can get context from any handler
// under this wrapper.
//
// But this very not recommend, because it more internal
// mechanism.
// Also, it will have many consequences, because others
// middlewares can set data with same key (I think it
// would have been done by accident).
//
// The developers of the package make no guarantee
// of use outside of this package.
type wrapperContext struct {
tele.Context
fsmCtx Context
}
func newWrapperContext(context tele.Context, fsmCtx Context) *wrapperContext {
return &wrapperContext{Context: context, fsmCtx: fsmCtx}
}
func (w *wrapperContext) Get(key string) any {
if key == fsmInternalKey {
return w.fsmCtx
}
return w.Context.Get(key)
}
func (w *wrapperContext) FSMContext() Context { return w.fsmCtx }
// tryUnwrapContext tries get fsm.Context from telebot.Context.
func tryUnwrapContext(c tele.Context) (Context, bool) {
wrapped, ok := c.(*wrapperContext)
if ok {
return wrapped.fsmCtx, true
}
ctx, ok := c.Get(fsmInternalKey).(Context)
return ctx, ok
}