我们从Python开源项目中,提取了以下4个代码示例,用于说明如何使用telegram.ext.Filters.forwarded()。
def main(): logger.info("Loading handlers for telegram bot") # Default dispatcher (this is related to the first bot in settings.TELEGRAM_BOT_TOKENS) dp = DjangoTelegramBot.dispatcher # To get Dispatcher related to a specific bot # dp = DjangoTelegramBot.getDispatcher('BOT_n_token') #get by bot token # dp = DjangoTelegramBot.getDispatcher('BOT_n_username') #get by bot username # on different commands - answer in Telegram dp.add_handler(CommandHandler("start", start)) dp.add_handler(CommandHandler("help", help)) dp.add_handler(CommandHandler("startgroup", startgroup)) dp.add_handler(CommandHandler("me", me)) dp.add_handler(CommandHandler("chat", chat)) dp.add_handler(MessageHandler(Filters.forwarded , forwarded)) # on noncommand i.e message - echo the message on Telegram dp.add_handler(MessageHandler(Filters.text, echo)) # log all errors dp.add_error_handler(error)
def main(): updater = Updater(cfg['botapi_token']) dp = updater.dispatcher dp.add_handler(CommandHandler("start", start)) dp.add_handler(CommandHandler("help", help)) dp.add_handler(CommandHandler("ping", ping)) # on no command dp.add_handler(MessageHandler(Filters.text & (~ Filters.forwarded), echo)) # Message is text and is not forwarded # log all errors dp.add_error_handler(error) # Start the Bot updater.start_polling() # Run the bot until process receives SIGINT, SIGTERM or SIGABRT updater.idle()
def statusReceived(bot, update): logger.debug("Message Received") if update.message.new_chat_member and update.message.new_chat_member.username == bot.username: logger.info("Added To Chat %s (%s)" %(update.message.chat.title, update.message.chat.id)) newGroup = dict() chat = update.message.chat #newGroup['_id'] = chat.id newGroup['title'] = chat.title newGroup['admins'] = [chatmember.user.id for chatmember in chat.get_administrators()] logger.debug("newGroup: %s" % newGroup) MDB.groups.update({'_id':chat.id}, newGroup, upsert=True) logger.info("Added %s to the group list" % update.message.chat.title) elif update.message.new_chat_member: updateGroupData(update) logger.info("New member joined the chat.") update.message.reply_text(WELCOMETEXT, quote=False) elif update.message.left_chat_member and update.message.left_chat_member.username == bot.username: MDB.groups.remove({'_id':update.message.chat.id}) logger.info("Removing entry for %s" % (update.message.chat.title)) # When a user sends a message, it is forwarded to everyone with this method.
def forwarded(bot, update): bot.sendMessage(update.message.chat_id, text='This msg forwaded information:\n {}'.format(update.effective_message))