为什么电报不向webhook发送消息?

时间:2016-02-08 14:53:56

标签: node.js telegram telegram-bot

在我的VPS上使用node-telegram-bot-api,我正试图从电报频道获取消息。

以下是代码:

var TelegramBot = require ( 'node-telegram-bot-api');
var token = '1793xxxxxxxxxxxxxxxxxxxxx';

var __dirname ='/etc/nginx/ssl'

var options = {
  webHook: {
    port: 443,
    key: __dirname+'/key.pem',
    cert: __dirname+'/crt.pem'
  }
};


var bot = new TelegramBot(token, options);
bot.setWebHook('1.3.4.5:443/bot1793xxxxxxxxxxxxxxxxxxxxx', __dirname+'/crt.pem');


bot.on ( 'message', function (msg) {
    var chatId = msg.chat.id;
    console.log (msg);
    bot.sendMessage (chatId, "Hello!", {caption: "I'm a bot!"});

});

当我从浏览器获得此链接时:

https://telegram.me/MyExampleBot?start=abcd

我希望机器人在用户点击abcd按钮时收到来自包含start的频道的消息,但我在机器人控制台中没有收到任何消息。 将bot.setWebHook添加到代码时出现问题。没有它,只要用户在频道中键入内容,我就会收到一条消息。

我的代码遵循示例here,所以我真的不知道它有什么问题。真的很感激你的提示。

1 个答案:

答案 0 :(得分:1)

我遇到了同样的问题。电报没有向我的机器人发送任何东西......

由于证书不正确。根据此生成您的证书: https://core.telegram.org/bots/self-signed

只需将“YOURDOMAIN.EXAMPLE”替换为有效值即可。我只使用了我的IP,之后就可以了。

另外,您可以从node-telegram-bot-api查看调试信息,以了解发生的情况:

  

npm install debug
  DEBUG = node-telegram-bot-api node bot.js

它将为您生成调试输出。

希望它有所帮助。