diff options
Diffstat (limited to 'hommaexceli_py')
| -rw-r--r-- | hommaexceli_py/main.py | 17 | ||||
| -rw-r--r-- | hommaexceli_py/sheets_client.py | 6 | ||||
| -rw-r--r-- | hommaexceli_py/telegram_client.py | 33 |
3 files changed, 44 insertions, 12 deletions
diff --git a/hommaexceli_py/main.py b/hommaexceli_py/main.py index 9c16ba5..645c6f3 100644 --- a/hommaexceli_py/main.py +++ b/hommaexceli_py/main.py @@ -1,11 +1,22 @@ -from dotenv import load_dotenv +import logging +from os import environ +from dotenv import load_dotenv, dotenv_values +from telegram_client import run_telegram +logging.basicConfig( + level=logging.INFO, format="%(asctime)s | %(name)s | %(levelname)s | %(message)s" +) load_dotenv() -from telegram_client import run_telegram - def main(): + logging.info("Starting hommaexceli_py...") + logging.info("Using environment:") + + env = dotenv_values() + for env_key in env.keys(): + logging.info(f"{env_key}={env[env_key]}") + run_telegram() diff --git a/hommaexceli_py/sheets_client.py b/hommaexceli_py/sheets_client.py index 6ce6a16..b30eb3d 100644 --- a/hommaexceli_py/sheets_client.py +++ b/hommaexceli_py/sheets_client.py @@ -1,5 +1,6 @@ import pickle import os.path +import logging from os import getenv from datetime import date from dataclasses import dataclass @@ -39,6 +40,7 @@ def _row_to_dataclass(values): def authenticate_and_fetch_sheets_data(): + logging.info("Reading Google credentials and authenticating...") creds = None # The file token.pickle stores the user's access and refresh tokens, and is # created automatically when the authorization flow completes for the first @@ -57,9 +59,10 @@ def authenticate_and_fetch_sheets_data(): with open("token.pickle", "wb") as token: pickle.dump(creds, token) - service = build("sheets", "v4", credentials=creds) + service = build("sheets", "v4", credentials=creds, cache_discovery=False) # Call the Sheets API + logging.info("Calling Sheets API to fetch data...") sheet = service.spreadsheets() result = ( sheet.values() @@ -71,4 +74,5 @@ def authenticate_and_fetch_sheets_data(): ) rows = result.get("values", []) + logging.info(f"Fetched {len(rows)} rows of data") return map(_row_to_dataclass, rows) diff --git a/hommaexceli_py/telegram_client.py b/hommaexceli_py/telegram_client.py index 108f525..5e845a5 100644 --- a/hommaexceli_py/telegram_client.py +++ b/hommaexceli_py/telegram_client.py @@ -1,6 +1,7 @@ import datetime import signal import sys +import logging from os import getenv from telegram.ext import Updater, CommandHandler, Job from telegram import Update @@ -20,41 +21,57 @@ def _signal_handler(sig, frame): def _callback_alarm(context): + chat_id = context.job.context + logging.info(f"Authenticating and fetching data from Sheets...") rows = authenticate_and_fetch_sheets_data() + + logging.info(f"Sending sheet data to chat id {chat_id}...") message = "\n".join(map(str, rows)) - context.bot.send_message(chat_id=context.job.context, text=message) + context.bot.send_message(chat_id=chat_id, text=message) def _callback_timer(update: Update, context): + chat_id = update.message.chat_id + logging.info(f"Starting timer in chat id {chat_id}...") update.message.reply_text( "HommaBot startattu. Uusia hommapäivityksiä viikon välein." ) - chats_mem_cache.add(update.message.chat_id) + chats_mem_cache.add(chat_id) context.job_queue.run_repeating( _callback_alarm, - datetime.timedelta(seconds=10), - context=update.message.chat_id, + datetime.timedelta(weeks=1), + context=chat_id, ) def _stop_timer(update: Update, context): + chat_id = update.message.chat_id + logging.info(f"Stopping timer in chat id {chat_id}...") update.message.reply_text("HommaBot pysäytetty!") - chats_mem_cache.remove(update.message.chat_id) + chats_mem_cache.remove(chat_id) context.job_queue.stop() def run_telegram(): - updater = Updater(getenv("TELEGRAM_TOKEN")) + tg_token = getenv("TELEGRAM_TOKEN") + logging.info("Creating Telegram bot object...") + updater = Updater(tg_token) + + logging.info("Registering Telegram bot /start handler...") updater.dispatcher.add_handler( CommandHandler("start", _callback_timer, pass_job_queue=True) ) + + logging.info("Registering Telegram bot /stop handler...") updater.dispatcher.add_handler( CommandHandler("stop", _stop_timer, pass_job_queue=True) ) + logging.info("Starting Telegram bot (HommaBot)...") updater.start_polling() + logging.info("Registering SIGINT handler...") signal.signal(signal.SIGINT, _signal_handler) - # while true: - # time.sleep(1) + + logging.info("Bot started successfully. Pausing main thread... (this is expected)") signal.pause() |
