From afc455c8ad470d82e296e102bd0bca9dc902acf7 Mon Sep 17 00:00:00 2001 From: campi Date: Wed, 9 Mar 2022 20:22:04 +0100 Subject: [PATCH] 2 --- libreremo_bot.py | 33 ++++++++++++--------------------- libreremo_conf.py | 6 ++++++ 2 files changed, 18 insertions(+), 21 deletions(-) create mode 100644 libreremo_conf.py diff --git a/libreremo_bot.py b/libreremo_bot.py index 1a44566..2728877 100644 --- a/libreremo_bot.py +++ b/libreremo_bot.py @@ -1,6 +1,5 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- - from telegram.ext.updater import Updater from telegram.update import Update from telegram import InlineKeyboardButton, InlineKeyboardMarkup @@ -11,6 +10,7 @@ from telegram.ext.conversationhandler import ConversationHandler from telegram.ext.callbackqueryhandler import CallbackQueryHandler from telegram.callbackquery import CallbackQuery from telegram.ext.filters import Filters +from libreremo_conf import TOKEN, CALIBRE_DIR, MIN_QUERY_LENGTH import glob import database_lib as DB import logging @@ -18,14 +18,9 @@ import logging logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) -MIN_QUERY_LENGTH = 3 - -TOKEN="**********" -updater = Updater("**********************************************", - use_context=True, arbitrary_callback_data=True) -CALIBRE_LIBRARY="/home/archino/LIBREREMO/" -db = DB.Database(CALIBRE_LIBRARY + "metadata.db") +updater = Updater(TOKEN, use_context=True, arbitrary_callback_data=True) +db = DB.Database(CALIBRE_DIR + "metadata.db") def start(update: Update, context: CallbackContext): @@ -107,7 +102,6 @@ def _get_resource(update, context): update.message.reply_text( text=text, reply_markup=reply_markup) - return handler @@ -143,10 +137,11 @@ def _get_book(update: Update, context: CallbackContext): query.message.reply_text( 'perfetto ti invio immediatamente il file! :)') try: - filepath = CALIBRE_LIBRARY + query.data + "/*.pdf" + filepath = CALIBRE_DIR + query.data + "/*.pdf" for file in glob.glob(filepath): with open(file, 'rb') as doc_pdf: context.bot.send_document(chat_id, doc_pdf) + logging.info("Sharing {}".format(file)) except FileNotFoundError: logging.info("cannot send {}".format(filepath)) query.message.reply_text( @@ -164,13 +159,15 @@ def search_by_title(update: Update, context: CallbackContext): context.user_data["resource"] = "titolo" return GET_RESOURCE +def search_by_title_default(update: Update, context: CallbackContext): + context.user_data["resource"] = "titolo" + return GET_RESOURCE def search_by_author(update: Update, context: CallbackContext): update.message.reply_text("di quale autore stai cercando i libri?") context.user_data["resource"] = "autore" return GET_RESOURCE - def search_by_class(update: Update, context: CallbackContext): update.message.reply_text("di quale corso di studio stai cercando i libri?") context.user_data["resource"] = "cds" @@ -182,10 +179,6 @@ def unknown(update: Update, context: CallbackContext): "Sorry '%s' is not a valid command" % update.message.text) -def unknown_text(update: Update, context: CallbackContext): - update.message.reply_text( - "Sorry I can't recognize you , you said '%s'" % update.message.text) - def __format_reply_markup(list_of_res): return InlineKeyboardMarkup([ [InlineKeyboardButton( @@ -193,13 +186,16 @@ def __format_reply_markup(list_of_res): callback_data = res.format_data())] for res in list_of_res]) + + GET_RESOURCE_TYPE, ASK_RESOURCE, GET_RESOURCE, GET_BOOK, GET_AUTHOR, GET_CDS = range(6) cnv_handler = ConversationHandler( entry_points = [CommandHandler('search', _ask_resource_type), CommandHandler('search_by_title', search_by_title), CommandHandler('search_by_author', search_by_author), - CommandHandler('search_by_class', search_by_class)], + CommandHandler('search_by_class', search_by_class), + MessageHandler(Filters.text, search_by_title_default)], states = { GET_RESOURCE_TYPE: [CallbackQueryHandler(_get_resource_type)], GET_RESOURCE: [MessageHandler(Filters.text, _get_resource)], @@ -215,13 +211,8 @@ cnv_handler = ConversationHandler( updater.dispatcher.add_handler(cnv_handler) updater.dispatcher.add_handler(CommandHandler('start', start)) updater.dispatcher.add_handler(CommandHandler('help', help)) -#updater.dispatcher.add_handler(CommandHandler('search_by_author', search_by_author)) -#updater.dispatcher.add_handler(CommandHandler('search_by_class', search_by_class)) updater.dispatcher.add_handler(MessageHandler(Filters.text, unknown)) updater.dispatcher.add_handler(MessageHandler(Filters.command, unknown)) # Filters out unknown commands -# Filters out unknown messages. -updater.dispatcher.add_handler(MessageHandler(Filters.text, unknown_text)) - updater.start_polling() updater.idle() diff --git a/libreremo_conf.py b/libreremo_conf.py new file mode 100644 index 0000000..8ddf0f1 --- /dev/null +++ b/libreremo_conf.py @@ -0,0 +1,6 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- + +TOKEN="**********************************************" +CALIBRE_DIR="/home/archino/LIBREREMO/" +MIN_QUERY_LENGTH = 3