From c3c7f298f4dcd70ab39a39a4ed9e0326154b6664 Mon Sep 17 00:00:00 2001 From: Firdaus Hakimi Date: Mon, 18 Mar 2024 23:53:42 +0800 Subject: [PATCH 1/4] src: __main__: Use relative import to avoid the need to cd into src --- src/__main__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/__main__.py b/src/__main__.py index 59b177a..a3adb55 100644 --- a/src/__main__.py +++ b/src/__main__.py @@ -1,4 +1,4 @@ -from Bot import main +from .Bot import main if __name__ == "__main__": main() From 22481039c03ea1db1b25603138d4cb29b411c07d Mon Sep 17 00:00:00 2001 From: Firdaus Hakimi Date: Tue, 19 Mar 2024 00:08:27 +0800 Subject: [PATCH 2/4] Reformat with black --- src/Bot.py | 19 ++++++++++--------- tests/test_bing.py | 6 ++++-- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/src/Bot.py b/src/Bot.py index 87fcdb9..fe270a3 100644 --- a/src/Bot.py +++ b/src/Bot.py @@ -2,17 +2,18 @@ from telethon import TelegramClient, events from os import getenv + def main() -> None: - load_dotenv() + load_dotenv() - api_id = getenv("API_ID") - api_hash = getenv("API_HASH") - bot_token = getenv("BOT_TOKEN") + api_id = getenv("API_ID") + api_hash = getenv("API_HASH") + bot_token = getenv("BOT_TOKEN") - app = TelegramClient('app', api_id, api_hash).start(bot_token=bot_token) + app = TelegramClient("app", api_id, api_hash).start(bot_token=bot_token) - @app.on(events.NewMessage(incoming=True, pattern='/start')) - async def start(event): - await event.reply("Hello!") + @app.on(events.NewMessage(incoming=True, pattern="/start")) + async def start(event): + await event.reply("Hello!") - app.run_until_disconnected() + app.run_until_disconnected() diff --git a/tests/test_bing.py b/tests/test_bing.py index b30337d..64aec6d 100644 --- a/tests/test_bing.py +++ b/tests/test_bing.py @@ -24,7 +24,9 @@ def generate_response() -> str: try: response = client.chat.completions.create( model="gpt-4.0-turbo", - messages=[{"role": "user", "content": "Say hi, with your response starting with START and ending with END"}], + messages=[ + {"role": "user", "content": "Say hi, with your response starting with START and ending with END"} + ], ) except: print("ERROR: Could not create a prompt!") @@ -36,7 +38,7 @@ class TestOutput: def test_output(self): response = generate_response() - if (len(response) > 0): + if len(response) > 0: print("✅ Bing is up!") else: print("❌ Bing is down...") From 6fbea30667eb8466bdafef5e8f64abdd80ed0eab Mon Sep 17 00:00:00 2001 From: Firdaus Hakimi Date: Tue, 19 Mar 2024 00:10:57 +0800 Subject: [PATCH 3/4] src: Bot: Guard against missing credentials --- src/Bot.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Bot.py b/src/Bot.py index fe270a3..5e48688 100644 --- a/src/Bot.py +++ b/src/Bot.py @@ -10,6 +10,9 @@ def main() -> None: api_hash = getenv("API_HASH") bot_token = getenv("BOT_TOKEN") + if not all([api_id, api_hash, bot_token]): + raise ValueError("Could not get all required credentials from env!") + app = TelegramClient("app", api_id, api_hash).start(bot_token=bot_token) @app.on(events.NewMessage(incoming=True, pattern="/start")) From 41b8fd9c94ae6c2facc0d523946665c2f1171607 Mon Sep 17 00:00:00 2001 From: Firdaus Hakimi Date: Tue, 19 Mar 2024 00:14:27 +0800 Subject: [PATCH 4/4] src: Bot: Satisfy type checker --- src/Bot.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Bot.py b/src/Bot.py index 5e48688..8de4032 100644 --- a/src/Bot.py +++ b/src/Bot.py @@ -6,14 +6,14 @@ def main() -> None: load_dotenv() - api_id = getenv("API_ID") - api_hash = getenv("API_HASH") - bot_token = getenv("BOT_TOKEN") + api_id = getenv("API_ID", 0) + api_hash = getenv("API_HASH", "") + bot_token = getenv("BOT_TOKEN", "") if not all([api_id, api_hash, bot_token]): raise ValueError("Could not get all required credentials from env!") - app = TelegramClient("app", api_id, api_hash).start(bot_token=bot_token) + app = TelegramClient("app", int(api_id), api_hash).start(bot_token=bot_token) @app.on(events.NewMessage(incoming=True, pattern="/start")) async def start(event):