-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
124 lines (79 loc) · 3.86 KB
/
main.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
import logging
from uuid import uuid4
import os
import datetime
from telegram import Update, InlineQueryResultArticle, InputTextMessageContent
from telegram.constants import ParseMode
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters, InlineQueryHandler
import pytz
from settings import ACCESS_TOKEN
from settings import ADMIN_ID
import messages
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.WARNING
)
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(chat_id=update.effective_chat.id, text=messages.start)
await subscribe(update, context)
async def help(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(chat_id=update.effective_chat.id, text=messages.commands_description)
def subscribed(chat_id: int, context: ContextTypes.DEFAULT_TYPE) -> bool:
jobs = context.job_queue.get_jobs_by_name(str(chat_id))
return len(jobs) > 0
async def subscribe(update: Update, context: ContextTypes.DEFAULT_TYPE):
if subscribed(update.effective_chat.id, context):
return
await context.bot.send_message(chat_id=update.effective_chat.id, text=messages.subscribed_successfully)
job_name = str(update.effective_chat.id)
tz = pytz.timezone('Europe/Minsk')
morning_time = datetime.time(hour=5, minute=0, second=0, tzinfo=tz)
context.job_queue.run_daily(send_motivation, morning_time, chat_id=update.effective_chat.id, name=job_name)
def remove_job_if_exists(name: str, context: ContextTypes.DEFAULT_TYPE) -> bool:
"""Remove job with given name. Returns whether job was removed."""
current_jobs = context.job_queue.get_jobs_by_name(name)
if not current_jobs:
return False
for job in current_jobs:
job.schedule_removal()
return True
async def reset(update: Update, context: ContextTypes.DEFAULT_TYPE):
chat_id = update.effective_chat.id
remove_job_if_exists(str(chat_id), context)
await context.bot.send_message(chat_id=chat_id, text=messages.reset_successfully)
async def send_motivation(context: ContextTypes.DEFAULT_TYPE) -> None:
job = context.job
answer = messages.get_daily_motivation_message()
await context.bot.send_message(chat_id=job.chat_id, text=answer, parse_mode=ParseMode.HTML)
async def stats(update: Update, context: ContextTypes.DEFAULT_TYPE):
if (update.message.from_user.id != int(ADMIN_ID)):
await context.bot.send_message(chat_id=update.effective_chat.id, text=messages.not_admin)
return
text = f"""
📊 Bot Statistics
Total users: {len(context.job_queue.jobs())}
"""
await context.bot.send_message(chat_id=update.effective_chat.id, text=text)
async def unknown(update: Update, context: ContextTypes.DEFAULT_TYPE):
await context.bot.send_message(chat_id=update.effective_chat.id, text=messages.unknown_command)
def main() -> None:
application = ApplicationBuilder().token(ACCESS_TOKEN).build()
start_handler = CommandHandler('start', start)
application.add_handler(start_handler)
help_handler = CommandHandler('help', help)
application.add_handler(help_handler)
subscribe_handler = CommandHandler('subscribe', subscribe)
application.add_handler(subscribe_handler)
reset_handler = CommandHandler('reset', reset)
application.add_handler(reset_handler)
stats_handler = CommandHandler('stats', stats)
application.add_handler(stats_handler)
# Unknown command
unknown_handler = MessageHandler(filters.COMMAND, unknown)
application.add_handler(unknown_handler)
# Unknown message
text_message_handler = MessageHandler(filters.TEXT & ~filters.COMMAND, unknown)
application.add_handler(text_message_handler)
application.run_polling()
if __name__ == '__main__':
main()