-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathchecklist.go
More file actions
138 lines (113 loc) · 4.13 KB
/
Copy pathchecklist.go
File metadata and controls
138 lines (113 loc) · 4.13 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package botapi
import (
"context"
"github.com/gotd/td/telegram/message"
"github.com/gotd/td/telegram/message/styling"
"github.com/gotd/td/tg"
)
// InputChecklistTask describes a task to add to a checklist on creation or edit.
type InputChecklistTask struct {
// ID is the unique identifier of the task, positive and unique among the
// tasks of the checklist.
ID int
// Text is the task text, 1-100 characters after entities parsing.
Text string
// ParseMode is the formatting mode for Text.
ParseMode ParseMode
// TextEntities are explicit entities; they take precedence over ParseMode.
TextEntities []MessageEntity
}
// InputChecklist describes a checklist to create or replace.
type InputChecklist struct {
// Title is the checklist title, 1-255 characters after entities parsing.
Title string
// ParseMode is the formatting mode for Title.
ParseMode ParseMode
// TitleEntities are explicit title entities; they take precedence over
// ParseMode.
TitleEntities []MessageEntity
// Tasks are the checklist tasks, 1-30 items.
Tasks []InputChecklistTask
// OthersCanAddTasks allows other users to add tasks to the checklist.
OthersCanAddTasks bool
// OthersCanMarkTasksAsDone allows other users to mark tasks as done or undone.
OthersCanMarkTasksAsDone bool
}
// textWithEntities resolves text plus a parse mode or explicit entities into the
// MTProto TextWithEntities shape. Explicit entities take precedence.
func (b *Bot) textWithEntities(ctx context.Context, text string, mode ParseMode, explicit []MessageEntity) (tg.TextWithEntities, error) {
if len(explicit) > 0 {
return tg.TextWithEntities{Text: text, Entities: entitiesToTg(explicit)}, nil
}
msg, entities, err := b.styledMessage(ctx, text, mode)
if err != nil {
return tg.TextWithEntities{}, err
}
return tg.TextWithEntities{Text: msg, Entities: entities}, nil
}
// checklistMedia converts an InputChecklist into MTProto todo-list media.
func (b *Bot) checklistMedia(ctx context.Context, c InputChecklist) (*tg.InputMediaTodo, error) {
if len(c.Tasks) == 0 {
return nil, &Error{Code: 400, Description: "Bad Request: checklist must include at least one task"}
}
title, err := b.textWithEntities(ctx, c.Title, c.ParseMode, c.TitleEntities)
if err != nil {
return nil, err
}
list := make([]tg.TodoItem, len(c.Tasks))
for i, task := range c.Tasks {
taskTitle, err := b.textWithEntities(ctx, task.Text, task.ParseMode, task.TextEntities)
if err != nil {
return nil, err
}
list[i] = tg.TodoItem{ID: task.ID, Title: taskTitle}
}
return &tg.InputMediaTodo{Todo: tg.TodoList{
OthersCanAppend: c.OthersCanAddTasks,
OthersCanComplete: c.OthersCanMarkTasksAsDone,
Title: title,
List: list,
}}, nil
}
// SendChecklist sends a checklist on behalf of a connected business account.
func (b *Bot) SendChecklist(
ctx context.Context, businessConnectionID string, chat ChatID, checklist InputChecklist, opts ...SendOption,
) (*Message, error) {
opts = append([]SendOption{WithBusinessConnection(businessConnectionID)}, opts...)
return b.sendResolvedMedia(ctx, chat, "", func(ctx context.Context, _ []styling.StyledTextOption) (message.MediaOption, error) {
todo, err := b.checklistMedia(ctx, checklist)
if err != nil {
return nil, err
}
return message.Media(todo), nil
}, opts...)
}
// EditMessageChecklist replaces the checklist in a message sent on behalf of a
// connected business account.
func (b *Bot) EditMessageChecklist(
ctx context.Context, businessConnectionID string, chat ChatID, messageID int, checklist InputChecklist, opts ...SendOption,
) (*Message, error) {
var cfg sendConfig
for _, o := range opts {
o(&cfg)
}
peer, err := b.resolveInputPeer(ctx, chat)
if err != nil {
return nil, err
}
todo, err := b.checklistMedia(ctx, checklist)
if err != nil {
return nil, err
}
req := &tg.MessagesEditMessageRequest{Peer: peer, ID: messageID}
req.SetMedia(todo)
if cfg.markup != nil {
mkp, err := replyMarkupToTg(cfg.markup)
if err != nil {
return nil, err
}
req.SetReplyMarkup(mkp)
}
resp, err := b.businessRaw(businessConnectionID).MessagesEditMessage(ctx, req)
return b.sentMessage(ctx, peer, resp, err)
}