我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用telegram.ext.Filters.document()。
def filef(bot,update): # get file id file = bot.getFile(update.message.document.file_id) # create a randon temp file name tf = tempfile.mkstemp() # download file file.download(tf[1]) # read file content try: tfco = open(tf[1],"r") tfc = tfco.read() tfco.close() except Exception as e: # if it is not text file bot.sendMessage(chat_id=update.message.chat_id,reply_to_message_id=update.message.message_id,text=" ?? Please send me a text file") # get user name for author try: author = update.message.from_user.first_name + " " + update.message.from_user.last_name except Exception: author = "Anonymous user" # call paste function url = paste(tfc , author) # replay the url to user bot.sendMessage(chat_id=update.message.chat_id,reply_to_message_id=update.message.message_id,text=url)
def send_media(bot, chat_id, media_type, media_id, text, reply_to_id=None): if media_type == "audio": bot.send_audio(chat_id=chat_id, audio=media_id, reply_to_message_id=reply_to_id) elif media_type == "document": bot.send_document(chat_id=chat_id, document=media_id, caption=text, reply_to_message_id=reply_to_id) elif media_type == "photo": bot.send_photo(chat_id=chat_id, photo=media_id, caption=text, reply_to_message_id=reply_to_id) elif media_type == "sticker": bot.send_sticker(chat_id=chat_id, sticker=media_id, reply_to_message_id=reply_to_id) elif media_type == "video": bot.send_video(chat_id=chat_id, video=media_id, caption=text, reply_to_message_id=reply_to_id) elif media_type == "voice": bot.send_voice(chat_id=chat_id, voice=media_id, reply_to_message_id=reply_to_id)
def main(): updater = Updater("INSERT TOKEN HERE") dp = updater.dispatcher dp.add_handler(CommandHandler("start", cmd_start)) dp.add_handler(CommandHandler("md", cmd_markdown, allow_edited=True)) dp.add_handler(CommandHandler("markdown", cmd_markdown, allow_edited=True)) dp.add_handler(CommandHandler("pin", cmd_pin, allow_edited=True)) dp.add_handler(CommandHandler("welcome", cmd_welcome, allow_edited=True)) dp.add_handler(CommandHandler("goodbye", cmd_goodbye, allow_edited=True)) dp.add_handler(CommandHandler("del_welcome", cmd_clear_welcome)) dp.add_handler(CommandHandler("del_goodbye", cmd_clear_goodbye)) dp.add_handler(CommandHandler("set_bot_admin", cmd_set_bot_admin, allow_edited=True)) dp.add_handler(CommandHandler("remove_bot_admin", cmd_remove_bot_admin, allow_edited=True)) dp.add_handler(CommandHandler("settings", cmd_settings)) dp.add_handler(CommandHandler("shortcut", cmd_shortcut_set, allow_edited=True)) dp.add_handler(CommandHandler("del_shortcut", cmd_shortcut_del, allow_edited=True)) dp.add_handler(CommandHandler("shortcuts", cmd_shortcut_getall, allow_edited=True)) dp.add_handler(MessageHandler(Filters.audio | Filters.command | Filters.contact | Filters.document | Filters.photo | Filters.sticker | Filters.text | Filters.video | Filters.voice | Filters.status_update, msg_parse, allow_edited=True)) dp.add_handler(CallbackQueryHandler(inline_button_callback)) dp.add_error_handler(error) updater.start_polling() updater.idle()
def main(): # manage conversation conv_handler = ConversationHandler( entry_points=[CommandHandler('start', start)], states={ second: [MessageHandler(Filters.text,second)], }, fallbacks=[CommandHandler('cancel', cancel)]) # it handle start command start_handler = CommandHandler('start', start) # handle all text second_handler = MessageHandler(Filters.text , second) filef_handler = MessageHandler(Filters.document, filef) # handle cancel cancel_handler = CommandHandler('cancel', cancel) # handle cancel about_handler = CommandHandler('about', about) # handle dispatcher dispatcher.add_handler(InlineQueryHandler(inlinequery)) dispatcher.add_handler(start_handler) dispatcher.add_handler(second_handler) dispatcher.add_handler(conv_handler) dispatcher.add_handler(cancel_handler) dispatcher.add_handler(about_handler) dispatcher.add_handler(filef_handler) # run updater.start_polling() updater.idle() updater.stop()
def del_document(bot, update): chat = update.effective_chat if sql.is_locked(chat.id, "document") and can_delete(chat, bot.id): update.effective_message.delete()