diff --git a/.github/workflows/python-app.yml b/.github/workflows/python-app.yml index 2c52bf6..2dc3594 100644 --- a/.github/workflows/python-app.yml +++ b/.github/workflows/python-app.yml @@ -28,8 +28,6 @@ jobs: run: | # stop the build if there are Python syntax errors or undefined names flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics - # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide - flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics - name: Test with pytest run: | pytest -vv diff --git a/run-tests.sh b/run-tests.sh index 3b7bcf3..e701c8f 100755 --- a/run-tests.sh +++ b/run-tests.sh @@ -1,4 +1,4 @@ #!/bin/bash -flake8 -pytest -vv +flake8 || exit +pytest -vv || exit diff --git a/src/telegram_bot_v2.py b/src/telegram_bot_v2.py index 36beb01..ae17a84 100644 --- a/src/telegram_bot_v2.py +++ b/src/telegram_bot_v2.py @@ -1,12 +1,11 @@ -import json import asyncio from telegram import Update from telegram.ext import ( - ApplicationBuilder, - Application, - ContextTypes, - CommandHandler, - MessageHandler, + ApplicationBuilder, + Application, + ContextTypes, + CommandHandler, + MessageHandler, filters, ) @@ -26,8 +25,7 @@ def _new_gorrion(local_mode: bool, delay_mode: bool) -> Gorrion: twitter_config = Config.get_twitter_config() twitter_config.retweet_delay = delay_mode - twitter = (TwitterLocal(twitter_config) - if local_mode else Twitter(twitter_config)) + twitter = (TwitterLocal(twitter_config) if local_mode else Twitter(twitter_config)) return Gorrion(spotify, twitter, musixmatch) @@ -40,7 +38,7 @@ def __init__(self, gorrion: Gorrion) -> None: async def start(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: await self._send_message(update, context, 'Welcome to Gorrion Bot 🐦🤖') await self._send_message(update, context, f'Supported commands are: \n\n{"\n".join(self._commands)}') - + async def playing(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: song = self._gorrion.playing() @@ -75,12 +73,13 @@ async def about(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> Non await self._send_message(update, context, 'Made with ❤️ by @juanitodread') async def any_other_message(self, update: Update, context: ContextTypes.DEFAULT_TYPE) -> None: - await self._send_message(update, context, f'I can only reply to you based on the commands: \n\n{"\n".join(self._commands)}') + commands = '\n'.join(self._commands) + await self._send_message(update, context, f'I can only reply to you based on the commands: \n\n{commands}') async def _send_message(self, update: Update | object, context: ContextTypes.DEFAULT_TYPE, message: str) -> None: if not self._is_bot_owner(update.effective_chat.username): await context.bot.send_message( - chat_id=update.effective_chat.id, + chat_id=update.effective_chat.id, text='Sorry 💔. I can only chat with my creator 🧙🏼.' ) return @@ -101,7 +100,7 @@ def _is_bot_owner(self, username: str | None) -> bool: raise Exception('TELEGRAM_OWNER_USERNAME variable is wrong') return Config.TELEGRAM_OWNER_USERNAME == username - + def _setup_app(app: Application, bot: TelegramBot) -> Application: start_handler = CommandHandler('start', bot.start) @@ -129,7 +128,7 @@ def _setup_app(app: Application, bot: TelegramBot) -> Application: return app - + def _do_work_local(event, context) -> None: gorrion = _new_gorrion(local_mode=True, delay_mode=False) bot = TelegramBot(gorrion) @@ -149,7 +148,7 @@ async def _do_work_lambda(event, context) -> dict: await app.process_update(Update.de_json(event, app.bot)) except Exception as error: print(f'Error: {error}') - + return { 'status_code': 200, 'body': {} @@ -158,9 +157,9 @@ async def _do_work_lambda(event, context) -> dict: def do_work(event, context): print(f'Event: {event}') - + # run in lambda return asyncio.get_event_loop().run_until_complete(_do_work_lambda(event, context)) # run in local - # _do_work_local(event, context) + # _do_work_local(event, context)