简体   繁体   English

从电报消息中获取 message_id - node.js

[英]Get message_id from Telegram message - node.js

I have a problem concerning a Telegram bot I am currently working on.我有一个关于我目前正在使用的 Telegram 机器人的问题。 I get messages from users in the following format:我从用户那里收到以下格式的消息:

   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' } }

When I want to access the id of the chat I can do this without any problems by using当我想访问聊天的 ID 时,我可以毫无问题地使用

$.message.chat.id

As soon as a want to get the message_id or first_name I only get "undefined".一旦想要获得 message_id 或 first_name,我只会得到“未定义”。

$.message.chat.first_name

$.message.message_id

Can anyone help me here?有人能帮我一下吗? As far as I see it I understood the structure of the message correctly so I don't really know what's the problem here.据我所知,我正确理解了消息的结构,所以我真的不知道这里有什么问题。

Thank you very much in advance非常感谢您提前

EDIT: I am adding a bit more of my code here:编辑:我在这里添加更多我的代码:

The main code for the bot (including the webhook) is this:机器人(包括 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://xxx.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());

}

When the OtherwiseController gets called the following code is called (I reduced it to the essentials to clarify the problem.当调用 elseController 时,将调用以下代码(我将其简化为要点以澄清问题。

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);        

    }
}   

The console output for this message此消息的控制台输出

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' } }

would be:将是:

undefined
hello
22303518
undefined

使用以下方法提取 json 对象的键,然后您可以使用适当的键进行访问:

Object.keys($.message.chat);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM