aboutsummaryrefslogtreecommitdiffstats
path: root/hommaexceli_py/telegram_client.py
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2021-02-12 14:52:00 +0200
committerJan Tuomi <jans.tuomi@gmail.com>2021-02-12 14:52:00 +0200
commit6d69d7f19c3014318734e618baff5ba958fe2344 (patch)
tree82051fd081b976d4c78829d343cc7cec93db7616 /hommaexceli_py/telegram_client.py
parentaf5b4941705fd08be5e72ad5c06e8a3ab8b50da7 (diff)
Implement Sheets and Telegram clients
Diffstat (limited to 'hommaexceli_py/telegram_client.py')
-rw-r--r--hommaexceli_py/telegram_client.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/hommaexceli_py/telegram_client.py b/hommaexceli_py/telegram_client.py
new file mode 100644
index 0000000..0c7fcf2
--- /dev/null
+++ b/hommaexceli_py/telegram_client.py
@@ -0,0 +1,57 @@
+import datetime
+import signal
+import sys
+from os import getenv
+from telegram.ext import Updater, CommandHandler, Job
+from telegram import Update
+
+chats_mem_cache = set()
+
+
+def _signal_handler(sig, frame):
+ print("You pressed Ctrl+C!")
+ updater = frame.f_locals["updater"]
+ for chat_id in chats_mem_cache:
+ updater.bot.send_message(chat_id=chat_id, text="HommaBot-palvelin sammutettu!")
+
+ updater.stop()
+ sys.exit(0)
+
+
+def _callback_alarm(context):
+ context.bot.send_message(chat_id=context.job.context, text="Alarm")
+
+
+def _callback_timer(update: Update, context):
+ update.message.reply_text(
+ "HommaBot startattu. Uusia hommapäivityksiä viikon välein."
+ )
+ chats_mem_cache.add(update.message.chat_id)
+ context.job_queue.run_repeating(
+ _callback_alarm,
+ datetime.timedelta(seconds=1),
+ context=update.message.chat_id,
+ )
+
+
+def _stop_timer(update: Update, context):
+ update.message.reply_text("HommaBot pysäytetty!")
+ chats_mem_cache.remove(update.message.chat_id)
+ context.job_queue.stop()
+
+
+def run_telegram():
+ updater = Updater(getenv("TELEGRAM_TOKEN"))
+ updater.dispatcher.add_handler(
+ CommandHandler("start", _callback_timer, pass_job_queue=True)
+ )
+ updater.dispatcher.add_handler(
+ CommandHandler("stop", _stop_timer, pass_job_queue=True)
+ )
+
+ updater.start_polling()
+
+ signal.signal(signal.SIGINT, _signal_handler)
+ # while true:
+ # time.sleep(1)
+ signal.pause()