如何在 Telegraf 的内联键盘中单击按钮时传递变量?

How to pass variable on button click in Inline Keyboard in Telegraf?

我发现这个解决方案有助于解决我的问题:

function makeMenu(clientId){
  let clientButton = [Markup.callbackButton(' '+clientId+' Information', 'info-'+clientId)]
  return Markup.inlineKeyboard([clientButton])
}

bot.action(/^[client]+(-[a-z]+)?$/, ctx => {
  console.log(ctx.match[1].split('-')[1] )
})

但这是一个糟糕的解决方案或变通方法,因为我需要传递一长串参数,并且电报的 api 有限制,无法传递最多 64 个字节的字符串。

您面临的问题的一种解决方案是将大数据放入数据库中,并将 Id(或对该数据的引用)作为回调数据传递,并使用您发布的代码。 示例代码为:

function makeMenu(clientId){
  const id = storeDataToDB('info-'+clientId) // store the large data to DB in here
  let clientButton = [Markup.callbackButton(' '+clientId+' Information', id)]
  return Markup.inlineKeyboard([clientButton])
}
bot.action(/^[client]+(-[a-z]+)?$/, ctx => {
  const data = getDataFromDB(ctx.match[1].split('-')[1]) // fetch the data from DB and continue..
  console.log(data)
})

您可以使用 firebase、mongodb 或任何其他数据库..(只需确保 ID 符合电报规定的限制)