gpt4 book ai didi

javascript - JavaScript 中的 Telegram Web Bots 数据验证

转载 作者:行者123 更新时间:2023-12-05 05:42:28 97 4
gpt4 key购买 nike

我尝试为机器人的 Telegram 网络应用程序做用户验证脚本。我不知道如何修复它。

import sha256 from 'js-sha256'

const telegram = window.Telegram.WebApp
const bot_token = '<bot-token>'
const data_check_string = telegram.initData

var secret_key = sha256.hmac.create(bot_token).update("WebAppData")
var hash = sha256.hmac.create(data_check_string).update(secret_key).hex();

if ( hash == telegram.initDataUnsafe.hash) {
// data is from Telegram
}

最佳答案

尝试查看 node js 实现,我尝试使用官方 Telegram 伪代码对其进行很好的评论。也许对您有帮助。

但在我看来,此验证需要在后端执行,因为在另一种情况下,您会泄露您的机器人 secret token

https://gist.github.com/konstantin24121/49da5d8023532d66cc4db1136435a885

关于javascript - JavaScript 中的 Telegram Web Bots 数据验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72010778/

97 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com