2016-02-09 47 views
3

我設置了一個電報如何使用Node.js網絡掛接到mybot /快遞:如何從電報通道接收數據到webhook?

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,機器人應該收到xyz/hook(至少這是我對程序的支持)的帖子

這裏是我必須接受職位的路線:

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

}); 

但我看到的機器人沒有發生時,在她的瀏覽器用戶訪問https://telegram.me/mybot?start=xyz並按下/start

這裏有什麼可能是錯的,以及如何解決它?

+0

是否example.com具有SSL連接的自簽名證書(HTTPS)?如果是這樣,您需要正確配置webhook:https://core.telegram.org/bots/self-signed – iuri

回答

0

數據進入req.body on 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=JSON.parse(req.body) 
text=body.message.text 
console.log(body) 
console.log(text)