-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
text.go
71 lines (59 loc) · 1.7 KB
/
text.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
package message
import (
"context"
"golang.org/x/xerrors"
"github.com/gotd/td/telegram/message/entity"
"github.com/gotd/td/telegram/message/styling"
"github.com/gotd/td/tg"
)
func (b *Builder) sendRequest(
p tg.InputPeerClass,
msg string,
entities []tg.MessageEntityClass,
) *tg.MessagesSendMessageRequest {
return &tg.MessagesSendMessageRequest{
NoWebpage: b.noWebpage,
Silent: b.silent,
Background: b.background,
ClearDraft: b.clearDraft,
Peer: p,
ReplyToMsgID: b.replyToMsgID,
Message: msg,
ReplyMarkup: b.replyMarkup,
Entities: entities,
ScheduleDate: b.scheduleDate,
}
}
// Text sends text message.
func (b *Builder) Text(ctx context.Context, msg string) (tg.UpdatesClass, error) {
p, err := b.peer(ctx)
if err != nil {
return nil, xerrors.Errorf("peer: %w", err)
}
upd, err := b.sender.sendMessage(ctx, b.sendRequest(p, msg, nil))
if err != nil {
return nil, xerrors.Errorf("send text: %w", err)
}
return upd, nil
}
// Textf formats and sends text message.
func (b *Builder) Textf(ctx context.Context, format string, args ...interface{}) (tg.UpdatesClass, error) {
return b.Text(ctx, formatMessage(format, args...))
}
// StyledText sends styled text message.
func (b *Builder) StyledText(ctx context.Context, texts ...StyledTextOption) (tg.UpdatesClass, error) {
p, err := b.peer(ctx)
if err != nil {
return nil, xerrors.Errorf("peer: %w", err)
}
tb := entity.Builder{}
if err := styling.Perform(&tb, texts...); err != nil {
return nil, err
}
msg, entities := tb.Complete()
upd, err := b.sender.sendMessage(ctx, b.sendRequest(p, msg, entities))
if err != nil {
return nil, xerrors.Errorf("send styled text: %w", err)
}
return upd, nil
}