-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
103 lines (88 loc) · 2.7 KB
/
bot.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
#!/usr/bin/env python
"""
Bot starting script
"""
import os
import sys
from telegram import Update
from telegram.ext import (
Application,
MessageHandler,
CommandHandler,
ConversationHandler,
filters,
)
from logger import logger
from config import bot_token
from commands import COMMANDS
from handlers import (
start,
add,
search,
recents,
restart,
get_all_items,
get_item,
get_item_quantity,
last_purchased_item,
items_usage,
get_price,
help_command,
PRICE,
QUANTITY,
ALL_ITEMS,
SEARCH,
ITEM,
)
from db import DB
async def post_init(app: Application):
"""After the app starts polling"""
await app.bot.set_my_commands(COMMANDS)
def main() -> None:
"""Run the bot."""
# Create the Application and pass it your bot's token.
application = Application.builder().token(bot_token).post_init(post_init).build()
application.bot_data["restart"] = False
db = DB()
db.setup()
conv_handler = ConversationHandler(
entry_points=[CommandHandler("start", start)],
states={
ITEM: [MessageHandler(filters.TEXT & ~filters.COMMAND, get_item)],
QUANTITY: [
MessageHandler(filters.TEXT & ~filters.COMMAND, get_item_quantity)
],
PRICE: [MessageHandler(filters.TEXT & ~filters.COMMAND, get_price)],
},
fallbacks=[MessageHandler(filters.TEXT, help_command)],
)
bulk_conv_handler = ConversationHandler(
entry_points=[CommandHandler("add", add)],
states={
ALL_ITEMS: [MessageHandler(filters.TEXT & ~filters.COMMAND, get_all_items)]
},
fallbacks=[MessageHandler(filters.TEXT, help_command)],
)
search_handler = ConversationHandler(
entry_points=[CommandHandler("search", search)],
states={
SEARCH: [
MessageHandler(filters.TEXT & ~filters.COMMAND, last_purchased_item)
],
},
fallbacks=[MessageHandler(filters.TEXT, help_command)],
)
application.add_handler(conv_handler)
application.add_handler(bulk_conv_handler)
application.add_handler(search_handler)
application.add_handler(CommandHandler("restart", restart))
application.add_handler(CommandHandler("recents", recents))
application.add_handler(CommandHandler("items_usage", items_usage))
application.add_handler(CommandHandler("help", help_command))
# Run the bot until the user presses Ctrl-C
application.run_polling(allowed_updates=Update.ALL_TYPES)
if application.bot_data["restart"]:
logger.info("Restarting the bot!!!")
os.execl(sys.executable, sys.executable, *sys.argv)
if __name__ == "__main__":
main()