我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用telegram.ext.Filters.photo()。
def photo_filter_callback(bot, update): user_id = update.message.from_user.id lang_id = mysql_select_language(user_id) try: image = update.message.photo[-1] path = '{1}download/{0}.jpg'.format(image.file_id, qr_folder_path) #print(image) newFile = bot.getFile(image.file_id) newFile.download(path) qr = account_by_qr(path) account = qr[0] print(account) if ('xrb_' in account): lang_keyboard(lang_id, bot, update.message.chat_id, lang_text('qr_send', lang_id).format(account)) sleep(1) if (len(qr) > 1): send_destination(bot, update, account, True) sleep(0.3) print(qr[1]) send_amount(bot, update, qr[1], True) else: send_destination(bot, update, account) elif (('NULL' in account) or (account is None) or (account is False)): lang_keyboard(lang_id, bot, update.message.chat_id, lang_text('qr_recognize_error', lang_id)) else: lang_keyboard(lang_id, bot, update.message.chat_id, lang_text('qr_account_error', lang_id)) #print(account) logging.info('QR by file: {0}'.format(account)) except UnicodeEncodeError: lang_keyboard(lang_id, bot, update.message.chat_id, lang_text('text_decode_error', lang_id))
def check_cat(bot, update): update.message.reply_text("??????????? ????") photo_file = bot.getFile(update.message.photo[-1].file_id) filename = os.path.join('downloads', '{}.jpg'.format(photo_file.file_id)) photo_file.download(filename) if img_has_cat(filename): update.message.reply_text("?? ???????? ???? ?????, ???????? ? ??????????.") new_filename = os.path.join('cats', '{}.jpg'.format(photo_file.file_id)) os.rename(filename, new_filename) else: os.remove(filename) update.message.reply_text("???????, ????? ?? ??????????!")
def start_bot(): my_bot = Updater(settings.TELEGRAM_API_KEY) dp = my_bot.dispatcher dp.add_handler(CommandHandler("start", reply_to_start_command)) dp.add_handler(MessageHandler(Filters.photo, check_cat)) my_bot.start_polling() my_bot.idle()
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 del_photo(bot, update): chat = update.effective_chat if sql.is_locked(chat.id, "photo") and can_delete(chat, bot.id): update.effective_message.delete()
def photo_from_telegram(bot: telegram.Bot, update: telegram.Update): if update.message: message: telegram.Message = update.message edited = False else: message: telegram.Message = update.edited_message edited = True tg_group_id = message.chat_id # telegram group id forward_index = get_forward_index(tg_group_id=tg_group_id) if edited: recall_message(forward_index, message) reply_entity = list() file_id = message.photo[-1].file_id pic_url = tg_get_pic_url(file_id, 'jpg') if JQ_MODE: reply_entity.append({ 'type': 'image', 'data': {'file': file_id + '.jpg'} }) if message.caption: reply_entity.append({ 'type': 'text', 'data': {'text': message.caption} }) else: if message.caption: reply_entity.append({ 'type': 'text', 'data': {'text': '[ ??, ?????' + pic_url + ' ]' + message.caption} }) else: reply_entity.append({ 'type': 'text', 'data': {'text': '[ ??, ?????' + pic_url + ' ]'} }) qq_message_id = send_from_tg_to_qq(forward_index, message=reply_entity, tg_group_id=tg_group_id, tg_user=message.from_user, tg_forward_from=message, tg_reply_to=message.reply_to_message, edited=edited) global_vars.mdb.append_message(qq_message_id, message.message_id, forward_index, 0)
def main(): global mClient, mDatabase mClient = MongoClient(mongoURI) mDatabase = mClient[mDatabase] try: serverInfo = mClient.server_info() logger.info("Mongo Connection Achieved") logger.debug("Connected to Mongo Server: %s" % serverInfo) except: logger.error("Could not connect to Mongo Server at %s" % mongoURI) raise updater = Updater(authToken) dp = updater.dispatcher dp.add_handler(CommandHandler("start", start)) dp.add_handler(CommandHandler("help", help)) charCreationHandler = ConversationHandler( entry_points=[CommandHandler('meAbout',meAbout, pass_user_data=True)], states={ TEXTRESP:[MessageHandler(Filters.text, meTextResp, pass_user_data=True), MessageHandler(Filters.all, redoState, pass_user_data=True)], PHOTORESP:[MessageHandler(Filters.photo or Filters.sticker, mePhotoResp, pass_user_data=True), MessageHandler(Filters.all, redoState, pass_user_data=True)], CHOOSE:[RegexHandler("^(Name, Discription, Photo, Bio, Cancel)$", meChoose, pass_user_data=True)] }, fallbacks=[CommandHandler('cancel',meAboutCancel, pass_user_data=True)] ) structCreationHandler = ConversationHandler( entry_points=[CommandHandler('newStruct', editStructStart, pass_user_data=True)], states={STRUCTNAME:[MessageHandler(Filters.text, editStructSetName, pass_user_data=True), MessageHandler(Filters.all, redoState, pass_user_data=True)] STRUCTDESC:[MessageHandler(Filters.text, editStructSetDesc, pass_user_data=True), MessageHandler(Filters.all, redoState, pass_user_data=True)], STRUCTDOOR:[MessageHandler(Filters.text, editStructSetDoor, pass_user_data=True), MessageHandler(Filters.all, redoState, pass_user_data=True)]}, fallbacks=[CommandHandler("cancel", cancel)]) roomCreationHandler = ConversationHandler( entry_points=[ConversationHandler("editRoom", editRoomStart)], states={}, fallbacks=[CommandHandler("cancel", cancel)]) dp.add_handler(CommandHandler("roomAddObjDisc", roomAddObjDisc, allow_edited=True)) dp.add_handler(CommandHandler("roomRemObjDisc", roomRemObjDisc)) dp.add_handler(CommandHandler("roomAddTempDisc", roomAddTempDisc, allow_edited=True)) dp.add_handler(CommandHandler("roomRemTempDisc", roomRemTempDisc)) dp.add_handler(CommandHandler("doorLock",doorLock)) dp.add_handler(CommandHandler("doorUnlock", doorUnlock)) dp.add_handler(CommandHandler("doorKnock", doorKnock)) updater.start_polling() updater.idle() mClient.close()