机器人对象的Telegram Web应用程序不包含用户数据

发布于 2025-01-26 12:28:09 字数 1501 浏览 2 评论 0原文

我已经为机器人创建了一个Web应用程序,还集成了电报“ Web App脚本”

https:https:https: //telegram.org/js/telegram-web-app.js

我的Web应用程序的代码如下所示。

我从 window.telegram.webapp 收到的唯一数据是:

使用回复标记键盘:

{
  "initData": "",
  "initDataUnsafe": {
    
  },
  "version": "1.0",
  "colorScheme": "dark",
  "themeParams": {
    "bg_color": "#17212b",
    "button_color": "#5288c1",
    "button_text_color": "#ffffff",
    "hint_color": "#708499",
    "link_color": "#6ab3f3",
    "text_color": "#f5f5f5"
  },
  "isExpanded": true,
  "viewportHeight": 621,
  "viewportStableHeight": 621,
  "MainButton": {
    "text": "CONTINUE",
    "color": "#5288c1",
    "textColor": "#ffffff",
    "isVisible": false,
    "isProgressVisible": false,
    "isActive": true
  }
}

使用inline按钮:

{
  "initData": "query_id=AAFdL6MsAAAAA122voywCSO1y&user=%7B%22id%22%3A748891997%2C%22first_name%22%3A%22%F0%9D%97%A6%F0%9D%97%94%F0%9D%97%A0%F0%9D%97%9C%22%2C%22last_name%22%3A%22%22%2C%22username%22%3A%22samyar%22%2C%22language_code%22%3A%22en%22%7D&auth_date=1651749255&hash=34f3a0de3e0dc7b8d5735d0a85b265763c5c490917180fef40e4df61c819949e",
  "initDataUnsafe": {
    "query_id": "AAFdL6MsAAAAAFbvoywCSO1y",
    "user": {
      "id": 748291957,
      "first_name": "
              

I have created a web app for my bot and also integrated the telegram "web app Script"

https://telegram.org/js/telegram-web-app.js

The code of my web app is shown in the below.

The only data I'm receiving from window.Telegram.WebApp is this :

using reply Markup keyboard:

{
  "initData": "",
  "initDataUnsafe": {
    
  },
  "version": "1.0",
  "colorScheme": "dark",
  "themeParams": {
    "bg_color": "#17212b",
    "button_color": "#5288c1",
    "button_text_color": "#ffffff",
    "hint_color": "#708499",
    "link_color": "#6ab3f3",
    "text_color": "#f5f5f5"
  },
  "isExpanded": true,
  "viewportHeight": 621,
  "viewportStableHeight": 621,
  "MainButton": {
    "text": "CONTINUE",
    "color": "#5288c1",
    "textColor": "#ffffff",
    "isVisible": false,
    "isProgressVisible": false,
    "isActive": true
  }
}

using inline button :

{
  "initData": "query_id=AAFdL6MsAAAAA122voywCSO1y&user=%7B%22id%22%3A748891997%2C%22first_name%22%3A%22%F0%9D%97%A6%F0%9D%97%94%F0%9D%97%A0%F0%9D%97%9C%22%2C%22last_name%22%3A%22%22%2C%22username%22%3A%22samyar%22%2C%22language_code%22%3A%22en%22%7D&auth_date=1651749255&hash=34f3a0de3e0dc7b8d5735d0a85b265763c5c490917180fef40e4df61c819949e",
  "initDataUnsafe": {
    "query_id": "AAFdL6MsAAAAAFbvoywCSO1y",
    "user": {
      "id": 748291957,
      "first_name": "????????????????",
      "last_name": "",
      "username": "samyar",
      "language_code": "en"
    },
    "auth_date": "1651749255",
    "hash": "34f3a0de3e0dc7b8d5735d0a85b265265cdc490917280fef40e4df61c819949e"
  },
  "version": "1.0",
  "colorScheme": "dark",
  "themeParams": {
    "bg_color": "#17212b",
    "button_color": "#5288c1",
    "button_text_color": "#ffffff",
    "hint_color": "#708499",
    "link_color": "#6ab3f3",
    "text_color": "#f5f5f5"
  },
  "isExpanded": true,
  "viewportHeight": 621,
  "viewportStableHeight": 621,
  "MainButton": {
    "text": "CONTINUE",
    "color": "#5288c1",
    "textColor": "#ffffff",
    "isVisible": false,
    "isProgressVisible": false,
    "isActive": true
  }
}

I need to access many other options, but I don't have.i just need the user's profile info.

The WebApp :

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <script src="https://telegram.org/js/telegram-web-app.js"></script>
    <title>Web app</title>
  </head>
  <body>
    <div class="user-info">
      <!-- Data -->
      <img id="profile" />
      <h1 id="name"></h1>
      <h3 id="username"></h3>
      <!--  -->
      <p id="test">
        This page is a simple webapp integrated with telegram bot ????
      </p>
    </div>
    <script>
      const profileEl = document.getElementById("profile");
      const nameEl = document.getElementById("name");
      const usernameEl = document.getElementById("username");
      const test = document.getElementById("test");
      window.Telegram.WebApp.ready();
      test.innerText = `${JSON.stringify(window.Telegram.WebApp)}`;

      const { first_name, last_name, username, photo_url } =
        window.Telegram.WebAppUser;

      // set the profile
      profileEl.src = photo_url;

      nameEl.innerText = `${first_name} ${last_name}`;

      usernameEl.innerText = username;
    </script>
  </body>
</html>

The bot code :
basically, it's just going to send an replyMarkupKeyboard


@app.on_message(filters.private)
async def hello(client, message):
    # await message.reply("Just a web app integrated withen telegram! ????",
    #                     reply_markup=InlineKeyboardMarkup(
    #             [
    #                 [
    #                     InlineKeyboardButton(
    #                       "Open Web app ????",web_app=WebAppInfo(url="https://telegramwebapp.netlify.app"))
    #                 ]
    #             ]
    #         ))
    await message.reply("Just a web app integrated withen telegram! ????",
                        reply_markup=ReplyKeyboardMarkup(
                [
                    [
                        KeyboardButton(
                          "Open Web app ????",
                          web_app=WebAppInfo(url="https://telegramwebapp.netlify.app"))
                    ]
                ]
            ))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

︶葆Ⅱㄣ 2025-02-02 12:28:09

const {first_name,last_name,用户名} = window.telegram.webapp.initdataunsafe.user;

对我来说很好。

const { first_name, last_name, username } = window.Telegram.WebApp.initDataUnsafe.user;

Works fine for me.

爱冒险 2025-02-02 12:28:09

我也偶然发现了这个问题。对于我的情况,通过菜单按钮

https://core.telegram.org/bots/webapps#launching-web-apps-from-the-menu-button

通过这种方法,我得到了完整的用户信息

I also stumbled upon this problem. For my case it was a good decision to implement web app by menu button

https://core.telegram.org/bots/webapps#launching-web-apps-from-the-menu-button

By this method I got the full user information

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文