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

标签 node.js telegram telegram-bot

我已经使用 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/

相关文章:

javascript - 如何在不重新启动 Express.js 项目的情况下刷新 API 端点

node.js - 如何将 websocket 连接代理到 NodeJS 中的其他 websockets

c# - 使用 Telegram Bot C# 在我的 channel 中搜索文件

telegram-bot - Telegram bot : ChatId ? 没有得到 ChatId 而是 {"ok":true ,"result":[]}

node.js - React错误找不到模块 './lib/api/node.js'

node.js - 在 Node 中解析ByteBuffer?

javascript - 创建将收听 Telegram channel 的应用程序

python - 如何发送带有标题/文本的 Telegram 媒体组

c - 如何通过cygwin在windows中编译telegram-cli?

python - 如何在 Telegram Bot 中用键盘回复?