aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--.env.sample1
-rw-r--r--hommaexceli_py/main.py4
-rw-r--r--hommaexceli_py/telegram_client.py31
3 files changed, 29 insertions, 7 deletions
diff --git a/.env.sample b/.env.sample
index 61d48ec..370839c 100644
--- a/.env.sample
+++ b/.env.sample
@@ -1,3 +1,4 @@
SHEETS_SPREADSHEET_ID=
SHEETS_RANGE=
TELEGRAM_TOKEN=
+TELEGRAM_USER_IDS=
diff --git a/hommaexceli_py/main.py b/hommaexceli_py/main.py
index 645c6f3..90ad361 100644
--- a/hommaexceli_py/main.py
+++ b/hommaexceli_py/main.py
@@ -1,12 +1,14 @@
import logging
from os import environ
from dotenv import load_dotenv, dotenv_values
+
+load_dotenv()
+
from telegram_client import run_telegram
logging.basicConfig(
level=logging.INFO, format="%(asctime)s | %(name)s | %(levelname)s | %(message)s"
)
-load_dotenv()
def main():
diff --git a/hommaexceli_py/telegram_client.py b/hommaexceli_py/telegram_client.py
index 34a750c..6d83469 100644
--- a/hommaexceli_py/telegram_client.py
+++ b/hommaexceli_py/telegram_client.py
@@ -3,13 +3,14 @@ import signal
import sys
import logging
from os import getenv
-from telegram.ext import Updater, CommandHandler, Job
+from telegram.ext import Updater, CommandHandler, CallbackContext
from telegram import Update
from sheets_client import authenticate_and_fetch_sheets_data
from parser import filter_only_this_weeks_rows
chats_mem_cache = set()
+ALLOWED_USER_IDS = map(lambda id: int(id), getenv("TELEGRAM_USER_IDS", "").split(","))
def _signal_handler(sig, frame):
@@ -22,8 +23,8 @@ def _signal_handler(sig, frame):
sys.exit(0)
-def _callback_alarm(context):
- chat_id = context.job.context
+def _callback_alarm(context: CallbackContext):
+ chat_id: int = context.job.context
logging.info(f"Authenticating and fetching data from Sheets...")
rows = authenticate_and_fetch_sheets_data()
@@ -37,8 +38,17 @@ def _callback_alarm(context):
context.bot.send_message(chat_id=chat_id, text=message)
-def _callback_timer(update: Update, context):
+def _callback_timer(update: Update, context: CallbackContext):
chat_id = update.message.chat_id
+ user_id = update.message.from_user.id
+
+ if user_id not in ALLOWED_USER_IDS:
+ logging.info(f"User {user_id} is not allowed to start timer.")
+ update.message.reply_text(
+ "Käyttäjälläsi ei ole oikeuksia startata HommaBottia."
+ )
+ return
+
logging.info(f"Starting timer in chat id {chat_id}...")
update.message.reply_text(
"HommaBot startattu. Uusia hommapäivityksiä viikon välein."
@@ -47,13 +57,22 @@ def _callback_timer(update: Update, context):
context.job_queue.run_repeating(
_callback_alarm,
datetime.timedelta(weeks=1),
- 1, # run immediately
+ 1, # run once immediately after 1 sec
context=chat_id,
)
-def _stop_timer(update: Update, context):
+def _stop_timer(update: Update, context: CallbackContext):
chat_id = update.message.chat_id
+ user_id = update.message.from_user.id
+
+ if user_id not in ALLOWED_USER_IDS:
+ logging.info(f"User {user_id} is not allowed to stop timer.")
+ update.message.reply_text(
+ "Käyttäjälläsi ei ole oikeuksia pysäyttää HommaBottia."
+ )
+ return
+
logging.info(f"Stopping timer in chat id {chat_id}...")
update.message.reply_text("HommaBot pysäytetty!")
chats_mem_cache.remove(chat_id)