示例#1
0
def add_handlers(dispatcher: tg.ext.Dispatcher):
    start_handler = tge.CommandHandler('start', start_callback)
    dispatcher.add_handler(start_handler)

    tree_handler = tge.CommandHandler('tree', tree_callback)
    dispatcher.add_handler(tree_handler)

    callback_handler = tge.CallbackQueryHandler(query_callback)
    dispatcher.add_handler(callback_handler)

    link_handler = tge.MessageHandler(
        filters=tgFilters.text &
        (tgFilters.entity(tg.MessageEntity.URL)
         | tgFilters.entity(tg.MessageEntity.TEXT_LINK)),
        callback=link_callback)
    dispatcher.add_handler(link_handler)
示例#2
0
def main():
    import json

    with open('config.json') as json_config_file:
        json_config = json.load(json_config_file)
    TELEGRAM_TOKEN = json_config['TELEGRAM_TOKEN']
    # Create the Updater and pass it your bot's token.
    # Make sure to set use_context=True to use the new context based callbacks
    # Post version 12 this will no longer be necessary
    updater = Updater(TELEGRAM_TOKEN, use_context=True)

    # Get the dispatcher to register handlers
    dp = updater.dispatcher

    # on different commands - answer in Telegram
    dp.add_handler(CommandHandler("start", start))
    dp.add_handler(CommandHandler("help", help))
    dp.add_handler(CommandHandler("get_download_history",
                                  get_download_history))
    dp.add_handler(
        MessageHandler(
            Filters.text
            & (Filters.entity(MessageEntity.URL)
               | Filters.entity(MessageEntity.TEXT_LINK)),
            get_link,
        ))
    dp.add_handler(MessageHandler(Filters.text, get_message))

    # on noncommand i.e message - echo the message on Telegram
    dp.add_handler(InlineQueryHandler(inlinequery))

    # log all errors
    dp.add_error_handler(error)

    # Start the Bot
    updater.start_polling()

    # Block until the user presses Ctrl-C or the process receives SIGINT,
    # SIGTERM or SIGABRT. This should be used most of the time, since
    # start_polling() is non-blocking and will stop the bot gracefully.
    updater.idle()
示例#3
0
                context.bot.send_message(update.message.chat_id,
                                         f"some error occured {username}")

            try:
                audio = open(filename, "rb")

                context.bot.send_message(update.message.chat_id,
                                         f"Task in Progress {username}")
                context.bot.send_audio(update.message.chat_id, audio)
                context.bot.send_message(update.message.chat_id,
                                         f"Task Completed {username}")
                audio.close()
                os.remove(filename)
            except Exception as e:
                context.bot.send_message(update.message.chat_id,
                                         f"Some error occured {username}")
        else:
            context.bot.send_message(update.message.chat_id,
                                     f"Some error occured {username}")


start_handler = CommandHandler("start", start)
message_handler = MessageHandler(Filters.entity("url"),
                                 internal_download,
                                 run_async=True)

dispatcher.add_handler(start_handler)
dispatcher.add_handler(message_handler)

updater.start_polling(poll_interval=0.5, read_latency=0.5)
示例#4
0
@message_handler(Filters.audio)
def handle_audio(update, context):
    update.message.reply_text('Thanks, adding file to queue...')
    LOG.info('Audio of type %s received %s', update.message.audio.mime_type,
             update.message.audio)

    context.bot.send_chat_action(chat_id=update.effective_message.chat_id,
                                 action=ChatAction.RECORD_AUDIO)
    fn = util.download_shared_file(context.bot, update.message.audio)
    util.add_to_queue(fn)
    update.message.reply_text('Song added to queue!')


# TODO(shoeffner): Add proper filter,
# TODO(shoeffner): handle multiple links from different sites?
@message_handler(Filters.entity(MessageEntity.TEXT_LINK))
def handle_youtube(update, context):
    print(update.message)
    for entity in update.message.entities:
        if entity.type != 'text_link':
            continue
        if re.match(r'^https?://(www\.)?youtu(\.be|be\.com)/', entity.url):
            context.bot.send_chat_action(
                chat_id=update.effective_message.chat_id,
                action=ChatAction.RECORD_AUDIO)
            fn = util.download_youtube_video(entity.url)
            util.add_to_queue(fn)
            update.message.reply_text('Song added to queue!')


@command
示例#5
0
 def __bindEvents(self):
     self.magi.callbackQuery(
         lambda bot, update: self.commands(bot, update, source="query"))
     self.magi.messageFilter(Filters.entity("mention"))(
         lambda bot, update: self.commands(bot, update, source="chat"))