我有一个关于我正在处理的Telegram机器人的问题。我以下列格式从用户那里收到消息:
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' } }
当我想访问聊天的ID时,我可以使用
毫无问题地执行此操作$.message.chat.id
只要想要获取message_id或first_name,我就会得到“未定义”。
$.message.chat.first_name
$.message.message_id
有人可以帮我吗?据我所知,我正确理解了消息的结构,所以我真的不知道这里有什么问题。
非常感谢您提前
编辑:我在这里添加了更多代码:
机器人的主要代码(包括webhook)是这样的:
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());
}
当调用了OTHERController时,会调用以下代码(我将其简化为要点以澄清问题。
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 :(得分:1)
使用以下方法提取json对象的键,然后可以使用适当的键访问:
Object.keys($.message.chat);