-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
edit.go
102 lines (83 loc) · 2.57 KB
/
edit.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
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"
)
// EditMessageBuilder creates edit message builder.
type EditMessageBuilder struct {
builder *Builder
id int
}
func (b *EditMessageBuilder) editTextRequest(
p tg.InputPeerClass,
msg string,
entities []tg.MessageEntityClass,
) *tg.MessagesEditMessageRequest {
return &tg.MessagesEditMessageRequest{
NoWebpage: b.builder.noWebpage,
Peer: p,
ID: b.id,
Message: msg,
ReplyMarkup: b.builder.replyMarkup,
Entities: entities,
ScheduleDate: b.builder.scheduleDate,
}
}
// Text edits message.
func (b *EditMessageBuilder) Text(ctx context.Context, msg string) (tg.UpdatesClass, error) {
p, err := b.builder.peer(ctx)
if err != nil {
return nil, xerrors.Errorf("peer: %w", err)
}
upd, err := b.builder.sender.editMessage(ctx, b.editTextRequest(p, msg, nil))
if err != nil {
return nil, xerrors.Errorf("edit styled text message: %w", err)
}
return upd, nil
}
// Textf formats and edits message .
func (b *EditMessageBuilder) Textf(ctx context.Context, format string, args ...interface{}) (tg.UpdatesClass, error) {
return b.Text(ctx, formatMessage(format, args...))
}
// StyledText edits message using given message.
func (b *EditMessageBuilder) StyledText(ctx context.Context, texts ...StyledTextOption) (tg.UpdatesClass, error) {
p, err := b.builder.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.builder.sender.editMessage(ctx, b.editTextRequest(p, msg, entities))
if err != nil {
return nil, xerrors.Errorf("edit styled text message: %w", err)
}
return upd, nil
}
// Media edits message using given media and text.
func (b *EditMessageBuilder) Media(ctx context.Context, media MediaOption) (tg.UpdatesClass, error) {
p, err := b.builder.peer(ctx)
if err != nil {
return nil, xerrors.Errorf("peer: %w", err)
}
attachment, err := b.builder.applySingleMedia(ctx, p, media)
if err != nil {
return nil, err
}
req := b.editTextRequest(p, attachment.Message, attachment.Entities)
req.Media = attachment.Media
upd, err := b.builder.sender.editMessage(ctx, req)
if err != nil {
return nil, xerrors.Errorf("send media: %w", err)
}
return upd, nil
}
// Edit edits message by ID.
func (b *Builder) Edit(id int) *EditMessageBuilder {
return &EditMessageBuilder{builder: b, id: id}
}