有没有办法将参数传递给Telegram Web应用的直接链接?

huangapple go评论82阅读模式
英文:

Is there any way to pass parameter to the telegram web app direct link?

问题

我已经注册了一个通过直接链接启动的Web应用程序,链接格式如下:

https://t.me/botusername/appname

此链接可以打开一个根据bot father配置的固定URL,但我想传递一些参数,例如:
https://t.me/botusername/appname?someId=id

在文档中(ref),我可以找到这个方法,它允许您传递参数并从start_param字段中获取它:
https://t.me/botusername/appname?startapp=command

但这仅适用于通过附件菜单启动的Web应用程序。普通的Web应用程序无法通过附件菜单启动,它说:

附件菜单集成目前仅适用于Telegram广告平台上的主要广告商。但是,在测试服务器环境中,所有机器人都可以使用它。

有谁可以在这方面提供帮助?我想要打开我的Web应用程序的动态路由,我认为可以使用start_param来实现。

英文:

I have registered a web app which is launched by the direct link, the link format is like this:

https://t.me/botusername/appname

This link can open a fixed url as configured using the bot father, but i want to pass some parameters to it, for example:
https://t.me/botusername/appname?someId=id

In the documentation(ref), I can find this method, which lets you pass param and get it from start_param field :
https://t.me/botusername/appname?startapp=command

But this works only for web apps launched via attachment menu. And normal web apps can not be launched via attachment menu, It says:
> Attachment menu integration is currently only available for major advertisers on the Telegram Ad Platform. However, all bots can use it in the test server environment.

Can anyone help in this regard, I want to open a dynamic route of my web app, and i thought It is possible with start_param.

答案1

得分: 0

以下是翻译好的部分:

  1. 如果您没有使用直接链接,并且您的机器人不应该在电报群中发送链接,只在其私人聊天中使用,那么您可以使用内联键盘选项并添加 web_app: web_link_with_params。它将以 TWA(Trusted Web Activity) 方式打开。注意:链接应该是网站的网址,而不是直接的 TWA 链接。

  2. 如果您使用直接链接,则可以传递 startapp 参数,您可以在 start_param 字段和 GET 参数 tgWebAppStartParam 中获取此参数。

例如:对于 Web 应用程序:

Web 应用程序直接链接:https://t.me/botusername/appname?startapp=someParamValue

let startParam = window.Telegram.WebApp.initDataUnsafe.start_param

注意:您只能传递一个参数,其值的长度目前限制为 64 个符号。因此,如果您想处理重定向,您需要使用该值编写自己的逻辑。

英文:

After spending some time on this, I found these solutions:

  1. If you are not using direct link and your bot is not supposed to send links in a telegram group, i.e used only in its private chat, then you can use inline keyboard option and add a web_app: web_link_with_params. It will be opened as twa. Note: The link should be the website url not direct twa link.

  2. If you are using direct link then you can pass startapp param, you can get this parameter in start_param field and in the GET parameter tgWebAppStartParam.

Eg: For web apps:

Web app direct link: https://t.me/botusername/appname?startapp=someParamValue

let startParam = window.Telegram.WebApp.initDataUnsafe.start_param

Note: You can pass only one param and the length of its value is limited to 64 Symbols as of now. So, if you want to handle redirect, you need to have your own logic using that value.

huangapple
  • 本文由 发表于 2023年6月16日 09:35:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76486463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定