Skip to content

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit c86cca1
Author: Andrey Marakulin <[email protected]>
Date:   Sat Sep 9 03:39:27 2023 +0300

    Update errors_solver.py

commit 24d4554
Author: Andrey Marakulin <[email protected]>
Date:   Sat Sep 9 03:23:38 2023 +0300

    Fix filters

commit 1c66685
Author: Andrey Marakulin <[email protected]>
Date:   Fri Sep 8 23:38:14 2023 +0300

    UNDONE FILTERS
  • Loading branch information
annndruha committed Sep 9, 2023
1 parent cbb909c commit 04821a1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 26 deletions.
26 changes: 14 additions & 12 deletions src/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
CommandHandler, MessageHandler, filters)

from src.handlers import (handler_button, handler_help, handler_md_guide,
handler_message, handler_start, native_error_handler)
handler_message, handler_start)
from src.settings import Settings

tg_log_handler = logging.FileHandler("issue_tgbot_telegram_updater.log")
Expand All @@ -29,21 +29,23 @@
settings = Settings()


class StartWithBotMention(filters.MessageFilter):
class BotMentionOrCommand(filters.MessageFilter):
def filter(self, message):
if message.text is None:
return False
return message.text.startswith(settings.BOT_NICKNAME)
if message.text_html is not None:
return message.text_html.startswith((settings.BOT_NICKNAME, '/issue'))
if message.caption_html is not None:
return message.caption_html.startswith((settings.BOT_NICKNAME, '/issue'))
return False


if __name__ == '__main__':
application = ApplicationBuilder().token(settings.BOT_TOKEN).build()
application.add_handler(CommandHandler('start', handler_start))
application.add_handler(CommandHandler('help', handler_help))
application.add_handler(CommandHandler('md_guide', handler_md_guide))
application.add_handler(CommandHandler('issue', handler_message))
application.add_handler(CallbackQueryHandler(handler_button))
application.add_handler(MessageHandler(StartWithBotMention(), handler_message))
application.add_handler(MessageHandler(filters.ChatType.PRIVATE, handler_message))
application.add_error_handler(native_error_handler)
application.add_handler(CommandHandler('start', handler_start, filters=filters.UpdateType.MESSAGE))
application.add_handler(CommandHandler('help', handler_help, filters=filters.UpdateType.MESSAGE))
application.add_handler(CommandHandler('md_guide', handler_md_guide, filters=filters.UpdateType.MESSAGE))
application.add_handler(CommandHandler('issue', handler_message, filters=filters.UpdateType.MESSAGE))

message_filter = filters.UpdateType.MESSAGE & (BotMentionOrCommand() | filters.ChatType.PRIVATE)
application.add_handler(MessageHandler(message_filter, handler_message))
application.run_polling()
6 changes: 3 additions & 3 deletions src/errors_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from gql.transport.exceptions import (TransportAlreadyConnected,
TransportError, TransportQueryError)
from telegram import Update
from telegram.error import NetworkError
from telegram.error import TelegramError
from telegram.ext import ContextTypes


Expand All @@ -20,8 +20,8 @@ def errors_solver(func):
async def wrapper(update: Update, context: ContextTypes.DEFAULT_TYPE):
try:
await func(update, context)
except NetworkError:
logging.error('Telegram server down again: telegram.error.NetworkError: Bad Gateway')
except TelegramError as err:
logging.error(f'TelegramError: {str(err.message)}')
except TransportAlreadyConnected as err:
logging.warning(f'TransportAlreadyConnected: {err.args}')
await context.bot.answer_callback_query(callback_query_id=update.callback_query.id,
Expand Down
21 changes: 12 additions & 9 deletions src/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@
github = Github(settings)


async def native_error_handler(update: object, context: ContextTypes.DEFAULT_TYPE):
logging.error("native_error_handler")


@errors_solver
@log_formatter
async def handler_start(update: Update, context: ContextTypes.DEFAULT_TYPE):
Expand Down Expand Up @@ -65,14 +61,21 @@ async def handler_message(update: Update, context: CallbackContext) -> None:
Receive a private user message or group-chat bot mention
and reply with issue template (or no title warning message).
"""
text_html = update.message.text_html or update.message.caption_html
if update.message.text_html is not None:
text_html = update.message.text_html
elif update.message.caption_html is not None:
text_html = update.message.caption_html
else:
return

text_html = text_html.removeprefix('/issue').removeprefix(settings.BOT_NICKNAME).strip()

if len(text_html) == 0:
await context.bot.send_message(chat_id=update.message.chat_id,
message_thread_id=update.message.message_thread_id,
text=ans.no_title)
return
text_html = 'Draft issue'
# await context.bot.send_message(chat_id=update.message.chat_id,
# message_thread_id=update.message.message_thread_id,
# text=ans.no_title)
# return

imessage = TgIssueMessage()
imessage.from_user(text_html)
Expand Down
5 changes: 3 additions & 2 deletions src/log_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

from telegram import Update
from telegram.ext import ContextTypes
from telegram.helpers import effective_message_type


def log_formatter(func):
Expand All @@ -24,9 +25,9 @@ async def wrapper(update: Update, context: ContextTypes.DEFAULT_TYPE):
elif update.message.caption is not None:
logging.info(f'{actor_handler} [caption]: {repr(update.message.caption)}')
else:
logging.info(f'{actor_handler} [UNKNOWN MESSAGE TYPE]')
logging.info(f'{actor_handler} [UNKNOWN MESSAGE TYPE: {effective_message_type(update)}]')
else:
logging.info(f'{actor_handler} [UNKNOWN UPDATE TYPE]')
logging.info(f'{actor_handler} [UNKNOWN UPDATE TYPE: {effective_message_type(update)}]')

await func(update, context)

Expand Down

0 comments on commit 04821a1

Please sign in to comment.