forked from Taralas209/telegram-timer-bot-dvmn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
67 lines (56 loc) · 2.26 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
import ptbot
import os
from pytimeparse import parse
TG_TOKEN = os.getenv('TELEGRAM_TOKEN')
TG_CHAT_ID = os.getenv('TG_CHAT_ID')
bot = ptbot.Bot(TG_TOKEN)
current_timer = None
timer_paused = False
remaining_time = 0
def wait(chat_id, question):
global current_timer, timer_paused, remaining_time
if question.lower() == 'pause':
if current_timer and not timer_paused:
timer_paused = True
bot.send_message(chat_id, "Timer paused.")
else:
bot.send_message(chat_id, "No active timer to pause.")
return
elif question.lower() == 'reset':
if current_timer:
current_timer.schedule_removal()
current_timer = None
timer_paused = False
remaining_time = 0
bot.send_message(chat_id, "Timer reset.")
else:
bot.send_message(chat_id, "No active timer to reset.")
return
reply_secs = parse(question)
remaining_time = reply_secs
message_id = bot.send_message(chat_id, "Remaining seconds: {}\n{}".format(
reply_secs, render_progressbar(reply_secs, 0)))
current_timer = bot.create_countdown(reply_secs, notify_progress, chat_id=chat_id,
message_id=message_id, reply_secs=reply_secs)
bot.create_timer(reply_secs, notify, chat_id=chat_id)
def notify_progress(secs_left, chat_id, message_id, reply_secs):
global timer_paused, remaining_time
if timer_paused:
remaining_time = secs_left
return
bot.update_message(chat_id, message_id,
"Remaining seconds: {}\n{}".format(secs_left,
render_progressbar(reply_secs, (reply_secs - secs_left))))
def render_progressbar(total, iteration, prefix='', suffix='', length=30,
fill='█', zfill='░'):
iteration = min(total, iteration)
percent = "{0:.1f}"
percent = percent.format(100 * (iteration / float(total)))
filled_length = int(length * iteration // total)
pbar = fill * filled_length + zfill * (length - filled_length)
return '{0} |{1}| {2}% {3}'.format(prefix, pbar, percent, suffix)
def notify(chat_id):
bot.send_message(chat_id, "Time's up")
bot = ptbot.Bot(TG_TOKEN)
bot.reply_on_message(wait)
bot.run_bot()