如何向 Telegram Web App 传递参数

问题描述 投票:0回答:2

我有一个机器人,它通过两个按钮发送消息:添加和删除(例如)。这些按钮可打开 Telegram Web 应用程序,该应用程序是我使用 @botfather

/newapp
添加的,我在其中放置了指向我的 Web 应用程序的链接。

const { Telegraf, Markup } = require('telegraf')
require('dotenv').config()

const bot = new Telegraf(process.env.BOT_TOKEN)

bot.command('/command', ctx => {
  ctx.reply('Open web app', Markup.inlineKeyboard([
    Markup.button.url('Add', 'https://t.me/bot_name/app_name),
    Markup.button.url('Delete', 'https://t.me/bot_name/app_name)
  ]))
})

我需要的只是将参数传递给网络应用程序。例如,对于每个按钮,我需要下一个 URL:

https://t.me/bot_name/app_name?type=add
https://t.me/bot_name/app_name?type=delete

但是如果我这样做,Web 应用程序的 URL 中仍然没有这些参数。这些参数是动态的,这就是为什么我无法创建具有不同 URL 的单独应用程序,例如

https://web_app.com/add
https://web_app.com/delete

此外,我无法使用

Markup.button.url('Button', 'https://web_app.com?type=add')
,因为它将在 浏览器 中打开 - 不像 Telegram Web 应用程序

telegram telegram-bot telegraf
2个回答
1
投票

嗯,我目前正在处理相同的场景,并将查询参数传递给电报网络应用程序就可以了。使用

Telegraf
库:

ctx.reply('Open web app', {
  reply_markup: {
    resize_keyboard: true,
    inline_keyboard: [
      {
        text: 'Date only',
        web_app: {
          url: 'https://expented.github.io/tgdtp/?hide=time'
        }
      },
      {
        text: 'Date range',
        web_app: {
          url: 'https://expented.github.io/tgdtp/?min=2022-05-01&max=2022-05-20'
        }
      }
    ]
  }
)

带有查询参数的电报网络应用程序存储库示例


0
投票

url格式应该是这样的。

t.me/yourbot/yourapp?startapp=312

您应该在 WebApp.initData 中看到。

start_param: "312"

© www.soinside.com 2019 - 2024. All rights reserved.