From 36badb1a82faea7d3d3f9db97d173b68b8f2efe4 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Mon, 28 Jan 2019 23:37:01 +0100 Subject: [PATCH] Use HTML in /cerca --- strings.py | 10 ++++++++-- telegrambot.py | 36 ++++++++++++++++++++++-------------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/strings.py b/strings.py index 80398e6c..63339072 100644 --- a/strings.py +++ b/strings.py @@ -69,7 +69,13 @@ class MATCHMAKING: } class ERRORS: - INVALID_SYNTAX = "⚠ Sintassi del comando errata.\n Sintassi:
/mm [minplayers-][maxplayers] per  \\n[descrizione]
" + INVALID_SYNTAX = "⚠ Sintassi del comando errata.\nSintassi:
/mm [minplayers-][maxplayers] per {gamename} \\n[descrizione]
" NOT_ADMIN = "⚠ Non sei il creatore di questo match!" MATCH_CLOSED = "⚠ Il matchmaking per questa partita è terminato!" - UNAUTHORIZED = "⚠ Non sono autorizzato a inviare messaggi a {mention}. \nPer piacere, {mention}, inviami un messaggio in privata!" + UNAUTHORIZED = "⚠ Non sono autorizzato a inviare messaggi a {mention}.\nPer piacere, {mention}, inviami un messaggio in privata!" + + +# Diario search +class DIARIO_SEARCH: + class ERRORS: + INVALID_SYNTAX = "⚠ Non hai specificato un termine da cercare!\nSintassi:
/search {termine}
" \ No newline at end of file diff --git a/telegrambot.py b/telegrambot.py index 04523462..68f998c9 100644 --- a/telegrambot.py +++ b/telegrambot.py @@ -63,7 +63,7 @@ def catch_and_report(func: "function"): try: bot.send_message(int(config["Telegram"]["main_group"]), s(strings.TELEGRAM.ERRORS.CRITICAL_ERROR, exc_info=sys.exc_info()), - parse_mode="Markdown") + parse_mode="HTML") except Exception: logger.error(f"Double critical error: {sys.exc_info()}") sentry.user_context({ @@ -93,7 +93,8 @@ def cmd_register(bot: Bot, update: Update): try: username = update.message.text.split(" ", 1)[1] except IndexError: - bot.send_message(update.message.chat.id, s(strings.LINKING.ERRORS.INVALID_SYNTAX)) + bot.send_message(update.message.chat.id, s(strings.LINKING.ERRORS.INVALID_SYNTAX), + parse_mode="HTML") session.close() return try: @@ -101,16 +102,19 @@ def cmd_register(bot: Bot, update: Update): royal_username=username, telegram_user=update.message.from_user) except errors.NotFoundError: - bot.send_message(update.message.chat.id, s(strings.LINKING.ERRORS.NOT_FOUND)) + bot.send_message(update.message.chat.id, s(strings.LINKING.ERRORS.NOT_FOUND), + parse_mode="HTML") session.close() return except errors.AlreadyExistingError: - bot.send_message(update.message.chat.id, s(strings.LINKING.ERRORS.ALREADY_EXISTING)) + bot.send_message(update.message.chat.id, s(strings.LINKING.ERRORS.ALREADY_EXISTING), + parse_mode="HTML") session.close() return session.add(t) session.commit() - bot.send_message(update.message.chat.id, s(strings.LINKING.SUCCESSFUL)) + bot.send_message(update.message.chat.id, s(strings.LINKING.SUCCESSFUL), + parse_mode="HTML") finally: session.close() @@ -290,26 +294,30 @@ def cmd_vote(bot: Bot, update: Update): finally: session.close() + @catch_and_report def cmd_search(bot: Bot, update: Update): session = db.Session() try: - text = update.message.text.split(" ", 1)[1] - if text is None: + try: + text = update.message.text.split(" ", 1)[1] + except IndexError: + bot.send_message(update.message.chat.id, s(strings.DIARIO_SEARCH.ERRORS.INVALID_SYNTAX)) return - text = text.replace('%','\\%').replace('_','\_') + text = text.replace('%', '\\%').replace('_', '\\_') entries = session.query(db.Diario).filter(db.Diario.text.ilike('%'+text+'%')).all() - messageText = "Ecco i risultati della ricerca:\n" + msg = f"Risultati della ricerca di {text}:\n" for entry in entries[:5]: - messageText+=f"[#{entry.id}](https://ryg.steffo.eu/diario#entry-{entry.id}) di {entry.author if entry.author is not None else 'Anonimo'}\n{entry.text}\n\n" - if len(entries)>5: - messageText += "ci sono altre entrate del diario che corrispondono alla ricerca:\n" + msg += f'#{entry.id} di {entry.author or "Anonimo"}\n{entry.text}\n\n' + if len(entries) > 5: + msg += "I termini comapiono anche nelle righe:\n" for entry in entries[5:]: - messageText += f"[#{entry.id}](https://ryg.steffo.eu/diario#entry-{entry.id}) " - bot.send_message(update.message.chat.id, messageText, parse_mode="Markdown") + msg += f'#{entry.id} ' + bot.send_message(update.message.chat.id, msg, parse_mode="HTML") finally: session.close() + @catch_and_report def cmd_mm(bot: Bot, update: Update): session = db.Session()