aboutsummaryrefslogtreecommitdiffstats
path: root/hommaexceli_py/telegram_client.py
diff options
context:
space:
mode:
authorJan Tuomi <jan.tuomi@valuemotive.com>2021-02-14 17:33:28 +0200
committerJan Tuomi <jan.tuomi@valuemotive.com>2021-02-14 17:33:28 +0200
commitbd2f7811b1f1633e150642222a9e07b5dd7a375a (patch)
tree47a69858c37de8a61f05870fbce1a81ec8270526 /hommaexceli_py/telegram_client.py
parentb26e3a76db789dd52dbbf696b1983cb990765e48 (diff)
Fix job removal on /stop
Diffstat (limited to 'hommaexceli_py/telegram_client.py')
-rw-r--r--hommaexceli_py/telegram_client.py21
1 files changed, 15 insertions, 6 deletions
diff --git a/hommaexceli_py/telegram_client.py b/hommaexceli_py/telegram_client.py
index a0f2af8..bf5a614 100644
--- a/hommaexceli_py/telegram_client.py
+++ b/hommaexceli_py/telegram_client.py
@@ -7,6 +7,7 @@ from typing import Union
from googleapiclient.discovery import Resource
from telegram.ext import Updater, CommandHandler, CallbackContext
from telegram import Update
+from telegram.ext.jobqueue import Job
from sheets_client import (
authenticate_sheets,
fetch_sheet_data,
@@ -16,7 +17,7 @@ from data_parser import filter_only_this_week, process_rows
from functools import partial
-chats_mem_cache = set()
+chats_mem_cache = {}
ALLOWED_USER_IDS = [
@@ -27,7 +28,7 @@ ALLOWED_USER_IDS = [
def _signal_handler(sig, frame):
print("You pressed Ctrl+C!")
updater = frame.f_locals["updater"]
- for chat_id in chats_mem_cache:
+ for chat_id in chats_mem_cache.keys():
updater.bot.send_message(chat_id=chat_id, text="HommaBot-palvelin sammutettu!")
updater.stop()
@@ -71,8 +72,8 @@ def _callback_timer(update: Update, context: CallbackContext, sheets_service: Re
update.message.reply_text(
"HommaBot startattu. Uusia hommapäivityksiä viikon välein."
)
- chats_mem_cache.add(chat_id)
- context.job_queue.run_repeating(
+
+ job = context.job_queue.run_repeating(
partial(_callback_alarm, sheets_service=sheets_service),
datetime.timedelta(weeks=1),
1, # run once immediately after 1 sec
@@ -80,11 +81,16 @@ def _callback_timer(update: Update, context: CallbackContext, sheets_service: Re
context=chat_id,
)
+ chats_mem_cache[chat_id] = job
+
def _stop_timer(update: Update, context: CallbackContext):
chat_id = update.message.chat_id
user_id = update.message.from_user.id
+ if chat_id not in chats_mem_cache:
+ return
+
if user_id not in ALLOWED_USER_IDS:
logging.info(f"User {user_id} is not allowed to stop timer.")
update.message.reply_text(
@@ -94,8 +100,11 @@ def _stop_timer(update: Update, context: CallbackContext):
logging.info(f"Stopping timer in chat id {chat_id}...")
update.message.reply_text("HommaBot pysäytetty!")
- chats_mem_cache.remove(chat_id)
- context.job_queue.stop()
+
+ job: Job = chats_mem_cache[chat_id]
+ job.schedule_removal()
+
+ del chats_mem_cache[chat_id]
def run_telegram():