-
Notifications
You must be signed in to change notification settings - Fork 8
/
echobot.py
110 lines (83 loc) · 2.91 KB
/
echobot.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
import os # noqa
import sys # noqa
import inspect # noqa
currentdir = os.path.dirname(
os.path.abspath(inspect.getfile(inspect.currentframe()))
) # noqa
parentdir = os.path.dirname(currentdir) # noqa
sys.path.insert(0, parentdir) # noqa
from dotenv import load_dotenv
env_file = os.path.join(os.path.dirname(__file__), "..", "..", ".env") # noqa
load_dotenv(env_file) # noqa
from swibots import (
BotApp,
BotContext,
CommandEvent,
MessageEvent,
CallbackQueryEvent,
filters,
InlineKeyboardButton,
InlineMarkup,
BotCommand,
)
from swibots.bots.handlers import (
MessageHandler,
UnknownCommandHandler,
CallbackQueryHandler,
CommandHandler,
)
import logging
env_file = os.path.join(os.path.dirname(__file__), "..", "..", ".env")
load_dotenv(env_file)
TOKEN = os.getenv("TOKEN")
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
async def echo_handler(ctx: BotContext[CommandEvent]):
text = ctx.event.params or "No args"
message = f"Your message: {text}"
await ctx.event.message.reply_text(message)
async def buttons_handler(ctx: BotContext[CommandEvent]):
message = f"Please select an option:"
inline_keyboard = [
[
InlineKeyboardButton(text="Option 1", callback_data="option1"),
InlineKeyboardButton(text="Option 2", callback_data="option2"),
],
[
InlineKeyboardButton(text="Option 3", callback_data="option3"),
InlineKeyboardButton(text="Option 4", callback_data="option4"),
],
]
inline_markup = InlineMarkup(inline_keyboard=inline_keyboard)
await ctx.event.message.reply_text(message, inline_markup=inline_markup)
async def message_handler(ctx: BotContext[MessageEvent]):
message = f"Thank you! I received your message: {ctx.event.message.message}"
await ctx.event.message.reply_text(message)
async def unkown_command_handler(ctx: BotContext[CommandEvent]):
message = f"Unknown command: {ctx.event.command}"
await ctx.event.message.reply_text(message)
async def query_callback_handler(ctx: BotContext[CallbackQueryEvent]):
message = f"Thank you! I received your callback: {ctx.event.callback_query.data}"
await ctx.event.message.edit_text(message)
app = BotApp(token=TOKEN).set_bot_commands(
[
BotCommand("echo", "Echoes back the message", True),
BotCommand("buttons", "Shows buttons", True),
BotCommand("help", "Shows help", True),
]
)
app.add_handler(
CommandHandler(
command="echo",
callback=echo_handler,
)
)
app.add_handler(CommandHandler(command="buttons", callback=buttons_handler))
app.add_handler(CallbackQueryHandler(callback=query_callback_handler))
app.add_handler(
MessageHandler(
callback=message_handler, filter=filters.text("hello") | filters.text("hi")
)
)
app.add_handler(UnknownCommandHandler(callback=unkown_command_handler))
app.run()