aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--hommaexceli_py/main.py17
-rw-r--r--hommaexceli_py/sheets_client.py6
-rw-r--r--hommaexceli_py/telegram_client.py33
-rw-r--r--poetry.lock21
-rw-r--r--pyproject.toml1
5 files changed, 65 insertions, 13 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()
diff --git a/poetry.lock b/poetry.lock
index c97ba38..39dd936 100644
--- a/poetry.lock
+++ b/poetry.lock
@@ -279,6 +279,21 @@ optional = false
python-versions = "*"
[[package]]
+name = "oauth2client"
+version = "4.1.3"
+description = "OAuth 2.0 client library"
+category = "main"
+optional = false
+python-versions = "*"
+
+[package.dependencies]
+httplib2 = ">=0.9.1"
+pyasn1 = ">=0.1.7"
+pyasn1-modules = ">=0.0.5"
+rsa = ">=3.1.4"
+six = ">=1.6.1"
+
+[[package]]
name = "oauthlib"
version = "3.1.0"
description = "A generic, spec-compliant, thorough implementation of the OAuth request-signing logic"
@@ -570,7 +585,7 @@ python-versions = "*"
[metadata]
lock-version = "1.1"
python-versions = "^3.9"
-content-hash = "612373ab4fdf2d803d339157852451554a4f6b1299283de4b898bae61ef35f2c"
+content-hash = "1da990068e41f838158d93739e8966b3b51c05b0856fdffe3a3dbff800ad30b2"
[metadata.files]
appdirs = [
@@ -700,6 +715,10 @@ mypy-extensions = [
{file = "mypy_extensions-0.4.3-py2.py3-none-any.whl", hash = "sha256:090fedd75945a69ae91ce1303b5824f428daf5a028d2f6ab8a299250a846f15d"},
{file = "mypy_extensions-0.4.3.tar.gz", hash = "sha256:2d82818f5bb3e369420cb3c4060a7970edba416647068eb4c5343488a6c604a8"},
]
+oauth2client = [
+ {file = "oauth2client-4.1.3-py2.py3-none-any.whl", hash = "sha256:b8a81cc5d60e2d364f0b1b98f958dbd472887acaf1a5b05e21c28c31a2d6d3ac"},
+ {file = "oauth2client-4.1.3.tar.gz", hash = "sha256:d486741e451287f69568a4d26d70d9acd73a2bbfa275746c535b4209891cccc6"},
+]
oauthlib = [
{file = "oauthlib-3.1.0-py2.py3-none-any.whl", hash = "sha256:df884cd6cbe20e32633f1db1072e9356f53638e4361bef4e8b03c9127c9328ea"},
{file = "oauthlib-3.1.0.tar.gz", hash = "sha256:bee41cc35fcca6e988463cacc3bcb8a96224f470ca547e697b604cc697b2f889"},
diff --git a/pyproject.toml b/pyproject.toml
index 943ad54..0082aea 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -12,6 +12,7 @@ google-auth-oauthlib = "^0.4.2"
black = "^20.8b1"
python-telegram-bot = "^13.2"
python-dotenv = "^0.15.0"
+oauth2client = "^4.1.3"
[tool.poetry.dev-dependencies]
pytest = "^5.2"