gpt4 book ai didi

node.js - 如何从 Telegram channel 接收数据到 webhook?

转载 作者:搜寻专家 更新时间:2023-11-01 00:06:38 27 4
gpt4 key购买 nike

我已经使用 node.js/express 将 Telegram webhook 设置为 mybot:

app.get('/hook', function (req, res) {    

url='https://api.telegram.org/bot17xxxxx/setwebhook?url=https://example.com/hook'
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
response.emit('close');
});
});

当我获取 https://example.com/hook 时,我可以接收到我的机器人控制台:

{"ok":true,"result":true,"description":"Webhook was set"}

现在我想从机器人接收数据,以便当用户访问 https://telegram.me/mybot?start=xyz 并按 /start 时,机器人应该在 /hook 的帖子中收到 xyz(至少这是我对程序的理解)

这是我必须收到邮件的路线:

app.post("/hook", function(req, res) {
console.log(body);

});

但是当用户在她的浏览器中访问 https://telegram.me/mybot?start=xyz 并按下 /start 时,我发现机器人没有任何反应。

这里有什么问题,如何解决?

最佳答案

数据来自 req.body,时间为 https://example.com/hook .所以你需要使用(req.body)

app.post("/hook", function(req, res) {
console.log(req.body);

});

会有那样的东西

{"update_id":1111111111,"message":{"message_id":2222,"from":{"id":333333333333,"is_bot":false,"first_name":"Username","last_name":"Lastname","username":"username","language_code":"en},"chat":{"id":1111111111,"first_name":"Username","last_name":"Lastname","username":"username","type":"private"},"date":1518592199,"text":"xyz"}}

如果你不使用类似 bodyParser 的东西,你可以 bodyParser middleware你应该解析它

参见简单示例。 body 中的所有信息和 text

中的文本
body=JSON.parse(req.body)
text=body.message.text
console.log(body)
console.log(text)

关于node.js - 如何从 Telegram channel 接收数据到 webhook?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35285600/

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