2017-07-31 87 views
1

我有一个关于目前正在处理的电报机器人的问题。我从用户的消息的格式如下:从电报信息获取message_id - node.js

update { update_id: 82618016, 
    message: 
    { message_id: 363, 
     from: { id: 22303518, first_name: 'Steve', language_code: 'de-DE' }, 
     chat: { id: 22303518, first_name: 'Steve', type: 'private' }, 
     date: 1501501753, 
     text: 'j' } } 

当我想用

$.message.chat.id 

一旦访问聊天我可以做到这一点没有任何问题的id作为一个想要得到message_id或first_name我只会得到“未定义”。

$.message.chat.first_name 

$.message.message_id 

任何人都可以帮我吗?据我所知,我正确理解了消息的结构,所以我不知道这里有什么问题。

非常感谢你提前

编辑:我增加了一点我的代码在这里:

的BOT(包括网络挂接)的主要代码是这样的:

initializeBot(); 

function initializeBot(){ 

const Telegram = require('telegram-node-bot'); 
const PingController = require('./controllers/ping'); 
const OtherwiseController = require('./controllers/otherwise'); 

const tg = new Telegram.Telegram('MY_TOKEN_IS_HERE', { 
    webhook: { 
     url: 'https://mutzi-bot.herokuapp.com', 
     port: process.env.PORT || 443, 
     host: '0.0.0.0' 
    } 
}) 

tg.router.when(new Telegram.TextCommand('/ping', 'pingCommand'), new PingController()) 
    .otherwise (new OtherwiseController()); 

} 

当OtherwiseController被调用下面的代码被称为(I它减少到要领澄清的问题。

class OtherwiseController extends Telegram.TelegramBaseController { 
    handle($){ 

     console.log($.message.chat.first_name); 
     console.log($.message.text); 
     console.log($.message.chat.id); 
     console.log($.message.message_id);   

    } 
} 

此消息

update { update_id: 82618020, 
    message: 
    { message_id: 371, 
     from: { id: 22303518, first_name: 'Steve', language_code: 'de-DE' }, 
     chat: { id: 22303518, first_name: 'Steve', type: 'private' }, 
     date: 1501509762, 
     text: 'hello' } } 

控制台输出将是:

undefined 
hello 
22303518 
undefined 
+0

你愿意分享一下你的代码吗? – tashakori

+0

@tashakori我添加了代码。先谢谢你。 – DesperateEi

+0

你有没有试过用这种方式访问​​属性:$ .message.chat [“first_name”] – tashakori

回答

1

使用下面的方法来提取您的JSON对象的键,然后就可以用适当的键来访问:

Object.keys($.message.chat);