从任意用户访问user_data

时间:2018-12-28 12:54:37

标签: python-telegram-bot

我需要从不是消息作者的tgUser访问user_data。所以我想我不能使用pass_user_data方法。

它遵循一些示例代码(我尚未测试,仅用于验证)。想象一下,该机器人在某个聊天组中,而/ ask却不是按下按钮的人:

def ask(bot, update, user_data):
    keyboard = [[InlineKeyboardButton("Yes", callback_data=str(update.effective_user.id),
                 InlineKeyboardButton("No", callback_data='n')]]
    reply_markup = InlineKeyboardMarkup(keyboard)
    update.message.reply_text('You like him/her?', reply_markup=reply_markup)

    # save some data which I want to retrieve/change
    # when a button was pressed by another user
    user_data['likes'] = 0


def button(bot, update, user_data):
    query = update.callback_query
    query.answer()

    # here I want to change the user_data from the tgUser who did /ask
    # I don't want to change the user_data from the tgUser who pressed the button
    # so this will lead to some exception because it's not the user_data I want to access...
    if query.data != 'n':
        # actually I want to access the user_data from user id int(query.data)
        user_data['likes'] += 1

updater = Updater("TOKEN")
updater.dispatcher.add_handler(CommandHandler('ask', ask, pass_user_data=True))
updater.dispatcher.add_handler(CallbackQueryHandler(button, pass_user_data=True))
updater.start_polling()
updater.idle()

这将导致异常,因为“按钮”中的user_data不是“询问”中的user_data。在“按钮”中,旨在从“询问”中更改user_data,而不是从按钮按下器中更改user_data。那么如何才能找到正确的user_data?

0 个答案:

没有答案