-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
request.go
103 lines (84 loc) · 2.42 KB
/
request.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
96
97
98
99
100
101
102
103
package message
import (
"context"
"golang.org/x/xerrors"
"github.com/gotd/td/telegram/message/peer"
"github.com/gotd/td/tg"
)
// RequestBuilder is an intermediate builder to make different RPC calls using Sender.
type RequestBuilder struct {
Builder
}
// ScreenshotNotify sends notification about screenshot.
// Parameter msgID is an ID of message that was screenshotted, can be 0.
func (b *RequestBuilder) ScreenshotNotify(ctx context.Context, msgID int) (tg.UpdatesClass, error) {
p, err := b.peer(ctx)
if err != nil {
return nil, xerrors.Errorf("peer: %w", err)
}
upd, err := b.sender.sendScreenshotNotification(ctx, &tg.MessagesSendScreenshotNotificationRequest{
Peer: p,
ReplyToMsgID: msgID,
})
if err != nil {
return nil, xerrors.Errorf("send screenshot notify: %w", err)
}
return upd, nil
}
// PeerSettings returns peer settings.
func (b *RequestBuilder) PeerSettings(ctx context.Context) (*tg.PeerSettings, error) {
p, err := b.peer(ctx)
if err != nil {
return nil, xerrors.Errorf("peer: %w", err)
}
settings, err := b.sender.getPeerSettings(ctx, p)
if err != nil {
return nil, xerrors.Errorf("get peer settings: %w", err)
}
return settings, nil
}
type startBotBuilder struct {
bot tg.InputUserClass
param string
}
// StartBotOption is an option for StartBot method.
type StartBotOption func(s *startBotBuilder)
// StartBotInputUser sets InputUserClass to start bot.
func StartBotInputUser(bot tg.InputUserClass) func(s *startBotBuilder) {
return func(s *startBotBuilder) {
s.bot = bot
}
}
// StartBotParam sets deeplink param to start bot.
func StartBotParam(param string) func(s *startBotBuilder) {
return func(s *startBotBuilder) {
s.param = param
}
}
// StartBot starts a conversation with a bot using a deep linking parameter.
func (b *RequestBuilder) StartBot(ctx context.Context, opts ...StartBotOption) (tg.UpdatesClass, error) {
p, err := b.peer(ctx)
if err != nil {
return nil, xerrors.Errorf("peer: %w", err)
}
sb := startBotBuilder{}
for _, opt := range opts {
opt(&sb)
}
if sb.bot == nil {
user, ok := peer.ToInputUser(p)
if !ok {
return nil, xerrors.Errorf("unexpected peer type %T, try to pass input user manually", p)
}
sb.bot = user
}
upd, err := b.sender.startBot(ctx, &tg.MessagesStartBotRequest{
Peer: p,
Bot: sb.bot,
StartParam: sb.param,
})
if err != nil {
return nil, xerrors.Errorf("start bot: %w", err)
}
return upd, nil
}