电报从消息中添加和检索元数据

Telegram add and retrieve metadata from message

您好,我正在寻找一种方法来存储用户 session/metadata 且延迟时间最少,而且不会让我付出太多代价。

简要问题描述。

我有一个机器人可以帮助用户从 Google 云端硬盘下载文件。

它使用 AWS lambda 函数的 Webhook。

为用户提供可点击的文件名,例如

/File.pdf

点击后,需要下载并发送给用户。

问题是我需要一种方法来知道用户选择了什么文件,而不必使用数据库或按名称遍历我的所有文件。

例如有没有办法将元数据添加到可点击消息中?这样我就可以将该元数据添加到可点击的内容中,如果用户单击 /File.pdf,我将能够提取元数据。

您可以像 in this example 一样发送 InlineKeyboardButton 并在 callback_data 中设置您需要的任何内容。当用户单击该按钮时 - 您的机器人将在更新中收到该数据:

button_list = [
    InlineKeyboardButton("File1.pdf", callback_data="https://drive.google.com/invoice.pdf"),
    InlineKeyboardButton("File2.pdf", callback_data="https://drive.google.com/presentation.pdf"),
    InlineKeyboardButton("File3.pdf", callback_data="https://drive.google.com/report.pdf")
]
reply_markup = InlineKeyboardMarkup(button_list)
bot.send_message(chat_id=chat_id, "Files list:", reply_markup=reply_markup)


# in update handler:
def some_update_handler(update, context):
    url = update.callback_query.data
    # ...
    # further processing

当 Telegram bot 用户应该看到一些不错的消息,但不应该看到发送给 Telegram bot 的一些内部值时,这在任何其他情况下也很有用。