forked from MayNiklas/canyonNotify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
129 lines (95 loc) · 3.92 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
125
126
127
128
129
#!/usr/bin/env python
# pylint: disable=C0116
import logging
from telegram import Update
from telegram.ext import CallbackContext
from telegram.ext import CommandHandler
from telegram.ext import Updater
from . import config
from . import parser
# My bike check
# Config with token
# Enable logging
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=config.log_level
)
logger = logging.getLogger(__name__)
# chat_id : seconds
interval_map = {}
def start(update: Update, _: CallbackContext) -> None:
update.message.reply_text(
'Hi! Use /notify to be notified \n /unnotify to stop bein notified \n /set to set interval')
def run_check(context: CallbackContext) -> None:
"""Run the check and message if there is a change"""
job = context.job
if parser.update():
context.bot.send_message(job.context, text=parser.status())
def get_status(update: Update, _: CallbackContext) -> None:
"""Run the check and message"""
parser.update()
update.message.reply_text(parser.status())
def remove_job_if_exists(name: str, context: CallbackContext) -> 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
def set_notify(update: Update, context: CallbackContext) -> None:
"""Create a repeated job in the queue"""
chat_id = update.message.chat_id
job_removed = remove_job_if_exists(str(chat_id), context)
# Try to find interval
try:
interval = interval_map[chat_id]
except:
interval = config.interval
context.job_queue.run_repeating(run_check, interval=interval,
context=chat_id, name=str(chat_id))
text = 'Notify was succesfully restarted!' if job_removed \
else 'Notify was succesfully started!'
update.message.reply_text(text)
def set_interval(update: Update, context: CallbackContext) -> None:
"""Modify the check interval"""
chat_id = update.message.chat_id
try:
# args[0] should contain the time for the interval in seconds
interval = int(context.args[0])
if interval < 0:
update.message.reply_text('Sorry we can not go back to future!')
return
interval_map[chat_id] = interval
text = f'Interval successfully set to {interval}!'
update.message.reply_text(text)
except (IndexError, ValueError):
update.message.reply_text('Usage: /set <seconds>')
# Restart notify
set_notify(update, context)
def unset_notify(update: Update, context: CallbackContext) -> None:
"""Remove the job if the user changed their mind."""
chat_id = update.message.chat_id
job_removed = remove_job_if_exists(str(chat_id), context)
text = 'Notify successfully cancelled!' if job_removed else 'You have no active notify.'
update.message.reply_text(text)
def main() -> None:
"""Run bot."""
# Create the Updater and pass it your bot's token.
updater = Updater(config.token)
# Get the dispatcher to register handlers
dispatcher = updater.dispatcher
# on different commands - answer in Telegram
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(CommandHandler("help", start))
dispatcher.add_handler(CommandHandler("set", set_interval))
dispatcher.add_handler(CommandHandler("notify", set_notify))
dispatcher.add_handler(CommandHandler("status", get_status))
dispatcher.add_handler(CommandHandler("unnotify", unset_notify))
# Start the Bot
updater.start_polling()
# Block until you press Ctrl-C or the process receives SIGINT, SIGTERM or
# SIGABRT. This should be used most of the time, since start_polling() is
# non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()