-
Notifications
You must be signed in to change notification settings - Fork 6
/
3_buttons_with_callback.py
181 lines (161 loc) · 5.04 KB
/
3_buttons_with_callback.py
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# %% [markdown]
"""
# Telegram: 3. Buttons with Callback
This tutorial demonstrates, how to add an inline keyboard and utilize
inline queries.
Here, %mddoclink(api,messengers.telegram.message,TelegramMessage)
class is used to represent telegram message,
%mddoclink(api,messengers.telegram.message,TelegramUI) and
%mddoclink(api,messengers.telegram.message,RemoveKeyboard)
classes are used for configuring additional telegram message features.
Different %mddoclink(api,script.core.message,message)
classes are used for representing different common message features,
like Attachment, Audio, Button, Image, etc.
"""
# %pip install dff[telegram]
# %%
import os
import dff.script.conditions as cnd
from dff.script import TRANSITIONS, RESPONSE
from dff.pipeline import Pipeline
from dff.script.core.message import Button
from dff.messengers.telegram import (
PollingTelegramInterface,
TelegramUI,
TelegramMessage,
)
from dff.messengers.telegram.message import _ClickButton
from dff.utils.testing.common import is_interactive_mode
# %% [markdown]
"""
If you want to send an inline keyboard to your Telegram chat,
set `is_inline` field of the `TelegramUI` instance to `True`
(note that it is inline by default, so you could also omit it).
Pushing a button of an inline keyboard results in a callback
query being sent to your bot. The data of the query
is stored in the `callback_query` field of a user `TelegramMessage`.
"""
# %%
script = {
"root": {
"start": {
TRANSITIONS: {
("general", "keyboard"): (
lambda ctx, _: ctx.last_request.text
in ("/start", "/restart")
),
},
},
"fallback": {
RESPONSE: TelegramMessage(
text="Finishing test, send /restart command to restart"
),
TRANSITIONS: {
("general", "keyboard"): (
lambda ctx, _: ctx.last_request.text
in ("/start", "/restart")
)
},
},
},
"general": {
"keyboard": {
RESPONSE: TelegramMessage(
**{
"text": "Starting test! What's 9 + 10?",
"ui": TelegramUI(
buttons=[
Button(text="19", payload="correct"),
Button(text="21", payload="wrong"),
],
is_inline=True,
),
}
),
TRANSITIONS: {
("general", "success"): cnd.exact_match(
TelegramMessage(callback_query="correct")
),
("general", "fail"): cnd.exact_match(
TelegramMessage(callback_query="wrong")
),
},
},
"success": {
RESPONSE: TelegramMessage(text="Success!"),
TRANSITIONS: {("root", "fallback"): cnd.true()},
},
"fail": {
RESPONSE: TelegramMessage(
text="Incorrect answer, type anything to try again"
),
TRANSITIONS: {("general", "keyboard"): cnd.true()},
},
},
}
# this variable is only for testing
happy_path = (
(
TelegramMessage(text="/start"),
TelegramMessage(
text="Starting test! What's 9 + 10?",
ui=TelegramUI(
buttons=[
Button(text="19", payload="correct"),
Button(text="21", payload="wrong"),
],
),
),
),
(
TelegramMessage(callback_query=_ClickButton(button_index=1)),
TelegramMessage(text="Incorrect answer, type anything to try again"),
),
(
TelegramMessage(text="try again"),
TelegramMessage(
text="Starting test! What's 9 + 10?",
ui=TelegramUI(
buttons=[
Button(text="19", payload="correct"),
Button(text="21", payload="wrong"),
],
),
),
),
(
TelegramMessage(callback_query=_ClickButton(button_index=0)),
TelegramMessage(text="Success!"),
),
(
TelegramMessage(text="Yay!"),
TelegramMessage(
text="Finishing test, send /restart command to restart"
),
),
(
TelegramMessage(text="/restart"),
TelegramMessage(
text="Starting test! What's 9 + 10?",
ui=TelegramUI(
buttons=[
Button(text="19", payload="correct"),
Button(text="21", payload="wrong"),
],
),
),
),
)
interface = PollingTelegramInterface(token=os.environ["TG_BOT_TOKEN"])
# %%
pipeline = Pipeline.from_script(
script=script,
start_label=("root", "start"),
fallback_label=("root", "fallback"),
messenger_interface=interface,
)
def main():
pipeline.run()
if __name__ == "__main__" and is_interactive_mode():
# prevent run during doc building
main()