-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
80 lines (61 loc) · 2.14 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
import logging
import sys
from logging.handlers import WatchedFileHandler
from telegram.ext import (
ApplicationBuilder,
CommandHandler,
ConversationHandler,
MessageHandler,
filters,
)
from commands.start import building_selection, start, street_selection
from commands.stop import handle_stop
from commands.subscription import show_subscription
from env import load_bot_token
# Constants
LOG_FILE = "bot.log"
# Conversation states
STREET, BUILDING = range(2)
def configure_logging() -> None:
"""Configure logging for the application."""
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
handlers=[WatchedFileHandler(
LOG_FILE), logging.StreamHandler(sys.stdout)],
)
httpx_logger = logging.getLogger("httpx")
httpx_logger.setLevel(logging.WARNING)
logging.info("Logging is configured.")
def setup_bot(token: str):
"""Initialize and set up the Telegram bot with handlers."""
application = ApplicationBuilder().token(token).build()
# Conversation handler for /start command
start_conv_handler = ConversationHandler(
entry_points=[CommandHandler("start", start)],
states={
STREET: [MessageHandler(filters.TEXT & ~filters.COMMAND, street_selection)],
BUILDING: [
MessageHandler(filters.TEXT & ~filters.COMMAND,
building_selection)
],
},
fallbacks=[],
allow_reentry=True,
)
# Register handlers
application.add_handler(start_conv_handler)
application.add_handler(CommandHandler("subscription", show_subscription))
application.add_handler(CommandHandler("stop", handle_stop))
logging.info("Bot handlers are set up.")
return application
def main() -> None:
configure_logging()
application = setup_bot(load_bot_token())
logging.info("Bot setup completed. Starting polling...")
# Run the bot until the user presses Ctrl-C
application.run_polling()
logging.info("Bot polling has ended.")
if __name__ == "__main__":
main()