DesperateEi
DesperateEi

Reputation: 144

Get message_id from Telegram message - node.js

I have a problem concerning a Telegram bot I am currently working on. 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

$.message.chat.id

As soon as a want to get the message_id or first_name I only get "undefined".

$.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:

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.

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

Upvotes: 1

Views: 2973

Answers (1)

tashakori
tashakori

Reputation: 2441

Use the below method to extract the keys of your json object, then you can access with an appropriate key:

Object.keys($.message.chat);

Upvotes: 1

Related Questions