-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbot8.py
More file actions
58 lines (37 loc) · 1.66 KB
/
bot8.py
File metadata and controls
58 lines (37 loc) · 1.66 KB
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
import logging
from telegram.ext import Updater, CommandHandler, messagequeue
import settings
logging.basicConfig(level=logging.INFO, filename="bot.log", format='%(asctime)s - %(levelname)s - %(message)s')
chats = []
def reply_to_start_command(bot, update):
update.message.reply_text("Привет!")
def subscribe_command(bot, update):
if update.message.chat_id not in chats:
chats.append(update.message.chat_id)
update.message.reply_text("Вы подписались на уведомления")
def alarm_command(bot, update, args, job_queue):
try:
seconds = abs(int(args[0]))
job_queue.run_once(alarm, seconds, context=update.message.chat_id)
except (IndexError, ValueError):
update.message.reply_text("Введите число секунд после команды /alarm")
def alarm(bot, job):
bot.sendMessage(chat_id=job.context, text="Сработал будильник!")
@messagequeue.queuedmessage
def my_test(bot, job):
for chat_id in chats:
bot.sendMessage(chat_id=chat_id, text="Уведомление")
def start_bot():
my_bot = Updater(settings.TELEGRAM_API_KEY)
my_bot.bot._msg_queue = messagequeue.MessageQueue()
my_bot.bot._is_messages_queued_default = True
jobs = my_bot.job_queue
jobs.run_repeating(my_test, interval=5)
dp = my_bot.dispatcher
dp.add_handler(CommandHandler("start", reply_to_start_command))
dp.add_handler(CommandHandler("subscribe", subscribe_command))
dp.add_handler(CommandHandler("alarm", alarm_command, pass_args=True, pass_job_queue=True))
my_bot.start_polling()
my_bot.idle()
if __name__ == "__main__":
start_bot()