|
|
@ -2,6 +2,9 @@
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import glob
|
|
|
|
import glob
|
|
|
|
import logging
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import os.path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import urllib3
|
|
|
|
|
|
|
|
|
|
|
|
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
|
|
|
|
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
|
|
|
|
from telegram import ParseMode
|
|
|
|
from telegram import ParseMode
|
|
|
@ -128,22 +131,29 @@ def _get_book(update: Update, context: CallbackContext):
|
|
|
|
parse_mode=ParseMode.HTML
|
|
|
|
parse_mode=ParseMode.HTML
|
|
|
|
)
|
|
|
|
)
|
|
|
|
query.message.reply_text(
|
|
|
|
query.message.reply_text(
|
|
|
|
'Ti invio immediatamente il file, sii paziente se il tuo libro non arriva')
|
|
|
|
'Stiamo caricando il file, sii paziente se il tuo libro non arriva subito')
|
|
|
|
filepath = CALIBRE_DIR + query.data + "/*.pdf"
|
|
|
|
filepath = CALIBRE_DIR + query.data
|
|
|
|
|
|
|
|
if os.path.isdir(filepath + "/splitted"):
|
|
|
|
|
|
|
|
filepath = filepath + "/splitted"
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
for file in glob.glob(filepath):
|
|
|
|
for file in glob.glob(filepath + "/*.pdf"):
|
|
|
|
with open(file, 'rb') as doc_pdf:
|
|
|
|
with open(file, 'rb') as doc:
|
|
|
|
context.bot.send_document(chat_id, doc_pdf)
|
|
|
|
context.bot.send_document(chat_id, doc, timeout=120)
|
|
|
|
logging.info("Sharing {}".format(file))
|
|
|
|
logging.info("Sharing {}".format(file))
|
|
|
|
except FileNotFoundError:
|
|
|
|
except urllib3.exceptions.HTTPError as e: # cannot handle exception
|
|
|
|
|
|
|
|
logging.error(e)
|
|
|
|
|
|
|
|
query.message.reply_text(
|
|
|
|
|
|
|
|
"Abbiamo qualche problema di connessione, ritenta più tardi")
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
logging.info("cannot send {}".format(filepath))
|
|
|
|
logging.info("cannot send {}".format(filepath))
|
|
|
|
|
|
|
|
logging.error(e)
|
|
|
|
query.message.reply_text(
|
|
|
|
query.message.reply_text(
|
|
|
|
"Mi dispiace non rieco a trovare il file, prova piu' tardi")
|
|
|
|
"Non riesco a mandarti il file, riprova più tardi")
|
|
|
|
return ConversationHandler.END
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _cnv_stop(update: Update, context: CallbackContext):
|
|
|
|
def _cnv_stop(update: Update, context: CallbackContext):
|
|
|
|
update.message.reply_text("ciao")
|
|
|
|
# context.bot.edit_message_reply_markup()
|
|
|
|
return ConversationHandler.END
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -178,9 +188,12 @@ def __format_reply_markup(list_of_res):
|
|
|
|
return InlineKeyboardMarkup(inline_keyboard)
|
|
|
|
return InlineKeyboardMarkup(inline_keyboard)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# def handle_error(update: Update, context: CallbackContext):
|
|
|
|
def handle_error(update: Update, context: CallbackContext):
|
|
|
|
# context.bot.send_message(chat_id=update.callback_query.message.chat_id,
|
|
|
|
context.bot.send_message(
|
|
|
|
# text="Oh no, qualcosa è andato storto, riprova più tardi...")
|
|
|
|
chat_id=update.callback_query.message.chat_id,
|
|
|
|
|
|
|
|
text="Oh no, qualcosa è andato storto, riprova più tardi...")
|
|
|
|
|
|
|
|
logging.error(context.error)
|
|
|
|
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GET_RESOURCE_TYPE, ASK_RESOURCE, GET_RESOURCE, GET_BOOK, GET_AUTHOR = range(5)
|
|
|
|
GET_RESOURCE_TYPE, ASK_RESOURCE, GET_RESOURCE, GET_BOOK, GET_AUTHOR = range(5)
|
|
|
@ -191,7 +204,7 @@ cnv_handler = ConversationHandler(
|
|
|
|
CommandHandler('search_by_author', search_by_author),
|
|
|
|
CommandHandler('search_by_author', search_by_author),
|
|
|
|
MessageHandler(Filters.text, search_by_title_default)],
|
|
|
|
MessageHandler(Filters.text, search_by_title_default)],
|
|
|
|
states={
|
|
|
|
states={
|
|
|
|
GET_RESOURCE_TYPE: [CallbackQueryHandler(_get_resource_type)],
|
|
|
|
GET_RESOURCE_TYPE: [CallbackQueryHandler(_get_resource_type), MessageHandler(Filters.text, _cnv_stop)],
|
|
|
|
GET_RESOURCE: [MessageHandler(Filters.text, _get_resource)],
|
|
|
|
GET_RESOURCE: [MessageHandler(Filters.text, _get_resource)],
|
|
|
|
GET_AUTHOR: [CallbackQueryHandler(_get_author)],
|
|
|
|
GET_AUTHOR: [CallbackQueryHandler(_get_author)],
|
|
|
|
GET_BOOK: [CallbackQueryHandler(_get_book)],
|
|
|
|
GET_BOOK: [CallbackQueryHandler(_get_book)],
|
|
|
@ -209,7 +222,7 @@ def main():
|
|
|
|
updater.dispatcher.add_handler(cnv_handler)
|
|
|
|
updater.dispatcher.add_handler(cnv_handler)
|
|
|
|
updater.dispatcher.add_handler(MessageHandler(Filters.command, unknown)) # Filters out unknown commands
|
|
|
|
updater.dispatcher.add_handler(MessageHandler(Filters.command, unknown)) # Filters out unknown commands
|
|
|
|
|
|
|
|
|
|
|
|
# updater.dispatcher.add_error_handler(handle_error)
|
|
|
|
updater.dispatcher.add_error_handler(handle_error)
|
|
|
|
|
|
|
|
|
|
|
|
updater.start_polling()
|
|
|
|
updater.start_polling()
|
|
|
|
updater.idle()
|
|
|
|
updater.idle()
|
|
|
|