-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext.go
More file actions
84 lines (64 loc) · 1.47 KB
/
Copy pathcontext.go
File metadata and controls
84 lines (64 loc) · 1.47 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
package maxbot
import (
"context"
maxbot "github.com/max-messenger/max-bot-api-client-go/v2"
"github.com/max-messenger/max-bot-api-client-go/v2/model"
)
type HandlerFunc func(Context) error
type Context interface {
Update() model.Update
Send(text string, opts ...Option) error
Edit(text string, opts ...Option) error
Delete(opts ...Option) error
Context() context.Context
}
type nativeContext struct {
ctx context.Context
b *maxbot.Api
u model.Update
}
func NewContext(ctx context.Context, b *maxbot.Api, u model.Update) Context {
return &nativeContext{
ctx: ctx,
b: b,
u: u,
}
}
func (c *nativeContext) Update() model.Update {
return c.u
}
func (c *nativeContext) Send(text string, opts ...Option) error {
msg := maxbot.NewMessage().
SetText(text).
SetUser(c.u.UserID).
SetChat(c.u.ChatID)
for _, opt := range opts {
opt(msg)
}
_, err := c.b.Messages.Send(c.ctx, msg)
return err
}
func (c *nativeContext) Edit(text string, opts ...Option) error {
msg := maxbot.NewMessage().
SetText(text).
SetUser(c.u.UserID).
SetChat(c.u.ChatID)
for _, opt := range opts {
opt(msg)
}
_, err := c.b.Messages.Send(c.ctx, msg)
return err
}
func (c *nativeContext) Delete(opts ...Option) error {
msg := maxbot.NewMessage().
SetUser(c.u.UserID).
SetChat(c.u.ChatID)
for _, opt := range opts {
opt(msg)
}
_, err := c.b.Messages.DeleteMessage(c.ctx, c.u.MessageID)
return err
}
func (c *nativeContext) Context() context.Context {
return c.ctx
}