From c1f0f34aa8852366e7a2cd330efd8998278ba22b Mon Sep 17 00:00:00 2001 From: Fabio De Simone Date: Mon, 28 Jan 2019 22:15:19 +0100 Subject: [PATCH] added /cerca --- telegrambot.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/telegrambot.py b/telegrambot.py index dc1ba54b..1ec9d644 100644 --- a/telegrambot.py +++ b/telegrambot.py @@ -291,6 +291,24 @@ 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: + return + entries = session.query(db.Diario).filter(db.Diario.text.like('%'+text+'%')).all() + messageText = "Ecco i risulati della ricerca:\n" + for entry in entries[:5]: + messageText+="[#{entry.id}](https://ryg.steffo.eu/diario#entry-{entry.id}) di {entry.author}\n{entry.text}\n\n" + if len(entries)>5: + messageText += "ci sono altre entrate del diario che corrispondono alla ricerca:\n" + for entry in entries[5:]: + messageText += "[#{entry.id}](https://ryg.steffo.eu/diario#entry-{entry.id}) " + bot.send_message(update.message.chat.id, messageText, parse_mode="Markdown") + finally: + session.close() @catch_and_report def cmd_mm(bot: Bot, update: Update): @@ -719,6 +737,7 @@ def process(arg_discord_connection): u.dispatcher.add_handler(CommandHandler("r", cmd_roll)) u.dispatcher.add_handler(CommandHandler("mm", cmd_mm)) u.dispatcher.add_handler(CommandHandler("matchmaking", cmd_mm)) + u.dispatcher.add_handler(CommandHandler("cerca", cmd_search)) u.dispatcher.add_handler(CallbackQueryHandler(on_callback_query)) logger.info("Handlers registered.") u.start_polling()