/
query.go
160 lines (140 loc) · 3.63 KB
/
query.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package botkit
import (
"fmt"
"strconv"
"strings"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
const (
TextInputQueryKind QueryKind = iota
SingleChoiceQueryKind
MultiChoiceQueryKind
FileInputQueryKind
RetryQueryKind
)
var RetryQuery = &Query{Kind: RetryQueryKind}
type QueryKind int
type Query struct {
Name string `json:"name"`
Kind QueryKind `json:"kind"`
Text string `json:"text"`
Choices []string `json:"choices,omitempty"`
MessageID int `json:"message_id,omitempty"`
}
func NewTextInputQuery(name, text string) *Query {
return &Query{
Name: name,
Kind: TextInputQueryKind,
Text: text,
}
}
func NewSingleChoiceQuery(name, text string, choices ...string) *Query {
return &Query{
Name: name,
Kind: SingleChoiceQueryKind,
Text: text,
Choices: choices,
}
}
func NewMultiChoiceQuery(name, text string, choices ...string) *Query {
return &Query{
Name: name,
Kind: MultiChoiceQueryKind,
Text: text,
Choices: choices,
}
}
func NewFileInputQuery(name, text string) *Query {
return &Query{
Name: name,
Kind: FileInputQueryKind,
Text: text,
}
}
func (qk QueryKind) HasTextResponse() bool {
switch qk {
case TextInputQueryKind, FileInputQueryKind:
return true
default:
return false
}
}
func (qk QueryKind) HasChoiceResponse() bool {
switch qk {
case SingleChoiceQueryKind, MultiChoiceQueryKind:
return true
default:
return false
}
}
func (q *Query) toChattable(dlg *Dialog) tgbotapi.Chattable {
msgText := q.getMessageText(dlg)
msg := tgbotapi.NewMessage(dlg.chatID, msgText)
msg.ParseMode = tgbotapi.ModeMarkdownV2
msg.ReplyMarkup = q.getReplyMarkup(dlg)
return &msg
}
func (q *Query) setMessageID(messageID int) {
q.MessageID = messageID
}
func (q *Query) getMessageText(dlg *Dialog) string {
msgText := q.Text
if !dlg.isPrivate() {
msgText = fmt.Sprintf("[%s](tg://user?id=%d) %s", dlg.data.Username, dlg.userID, msgText)
}
return msgText
}
func (q *Query) getReplyMarkup(dlg *Dialog) any {
switch q.Kind {
case SingleChoiceQueryKind:
buttons := make([][]tgbotapi.InlineKeyboardButton, 0, len(q.Choices))
for i, choice := range q.Choices {
btnText := choice
btnData := strconv.Itoa(i) + ":" + q.Name
buttons = append(buttons, []tgbotapi.InlineKeyboardButton{tgbotapi.NewInlineKeyboardButtonData(btnText, btnData)})
}
return tgbotapi.NewInlineKeyboardMarkup(buttons...)
case MultiChoiceQueryKind:
dlgChoices := dlg.getQueryData(q.Name).UserChoices
buttons := make([][]tgbotapi.InlineKeyboardButton, 0, len(q.Choices)+1)
for i, choice := range q.Choices {
btnText := choice
if dlgChoices[i] {
btnText = "☒ " + btnText
} else {
btnText = "☐ " + btnText
}
btnData := strconv.Itoa(i) + ":" + q.Name
buttons = append(buttons, []tgbotapi.InlineKeyboardButton{tgbotapi.NewInlineKeyboardButtonData(btnText, btnData)})
}
buttons = append(buttons, []tgbotapi.InlineKeyboardButton{tgbotapi.NewInlineKeyboardButtonData("OK", "done:"+q.Name)})
return tgbotapi.NewInlineKeyboardMarkup(buttons...)
case TextInputQueryKind:
if dlg.isPrivate() {
return nil
}
return tgbotapi.ForceReply{
ForceReply: true,
Selective: true,
}
default:
return nil
}
}
func (q *Query) getChoiceFromCallbackData(data string) (choice int, isDone, ok bool) {
if !strings.Contains(data, ":") {
return -1, false, false
}
parts := strings.SplitN(data, ":", 2)
data, queryName := parts[0], parts[1]
if queryName != q.Name {
return -1, false, false
}
if data == "done" {
return -1, true, true
}
if choice, err := strconv.Atoi(data); err == nil {
return choice, false, true
}
return -1, false, false
}