-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer_bot.py
More file actions
55 lines (37 loc) · 1.71 KB
/
timer_bot.py
File metadata and controls
55 lines (37 loc) · 1.71 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
from decouple import config
import ptbot
from pytimeparse import parse
TG_TOKEN = config('TG_TOKEN')
TG_CHAT_ID = config('TG_CHAT_ID')
def start_timer(chat_id, time, timer_bot):
timer_bot.create_timer(time, send_message,
chat_id=chat_id, timer_bot=timer_bot)
def send_message(chat_id, timer_bot):
timer_bot.send_message(chat_id=chat_id, message='Время вышло!')
def start_timer_countdown(chat_id, time, timer_bot):
init_message = timer_bot.send_message(chat_id, '.')
timer_bot.create_countdown(time, notify_progress,
chat_id=chat_id, message_id=init_message,
max_time=time, timer_bot=timer_bot)
def notify_progress(secs_left, chat_id, message_id, max_time, timer_bot):
bar_view = render_progressbar(max_time, secs_left)
message = f'Осталось {secs_left} секунд!\n{bar_view}'
timer_bot.update_message(chat_id, message_id, message)
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 start_work(chat_id, time, timer_bot):
time = parse(time)
start_timer_countdown(chat_id, time, timer_bot=timer_bot)
start_timer(chat_id, time, timer_bot=timer_bot)
def main():
bot = ptbot.Bot(TG_TOKEN)
bot.reply_on_message(start_work, timer_bot=bot)
bot.run_bot()
if __name__ == '__main__':
main()