import logging import os from subprocess import CalledProcessError from telegram.ext import Updater, MessageHandler, Filters from download_utils import APK, BadPackageNameException logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) logger = logging.getLogger(__name__) def error_callback(update, context): logger.warning('Update "%s" caused error "%s"', update, context.error) def get_package(update, context): logger.info("from {}: {}".format(update.message.chat_id, update.message.text)) # "history" if update.message.text == '/start': update.message.reply_text("To start downloading APK file send me a PlayStore link ok a package name. :)") return apk = APK(update.message.text, conf=CONF, conf_file=CONF_FILE) msg = context.bot.send_message(text=f"Downloading {apk.package_name}...", chat_id=update.message.chat_id) try: apk.download() with apk.send() as files: for f in files: context.bot.send_document(chat_id=update.message.chat_id, document=open(f, 'rb')) except BadPackageNameException as e: # instead of editing the text message we delete and resend one so a new notification will trigger context.bot.send_message(text="Bad package name: {}".format(e), chat_id=update.message.chat_id) except CalledProcessError: context.bot.send_message(text="Failed to send APK file", chat_id=update.message.chat_id) context.bot.delete_message(chat_id=update.message.chat_id, message_id=msg.message_id) if os.environ.get('CONF_FOLDER') is None: logger.error("No conf folder available") exit(1) TOKEN = None with open(os.path.join(os.environ['CONF_FOLDER'], 'bot.token')) as f: TOKEN = f.read().strip() CONF = { 'yes': True, 'verbose': False, 'tokencachefile': os.path.join(os.environ['CONF_FOLDER'], 'token.cache'), } CONF_FILE = os.path.join(os.environ['CONF_FOLDER'], 'gplaycli.conf') updater = Updater(token=TOKEN) updater.dispatcher.add_handler(MessageHandler(Filters.text, get_package)) updater.dispatcher.add_error_handler(error_callback) updater.start_polling() updater.idle()