Node-telegram-bot-api: Get message_id of the message that is being sent

Created on 13 Feb 2019  ·  9Comments  ·  Source: yagop/node-telegram-bot-api

Question

Hello what i am trying to do is sending a message to two users and then edit both messages when they press the inline keyboard button, i already managed to edit the messsage of the user that pressed the button but it dosen't edit the other message sent to the second user, i imagine that is becuase the two messages dont share the same message_id so i need to capture both id's so i can use that variable on the edit_messge parameters

question sorry! my bad!

Most helpful comment

Promise.all([
    bot.sendMessage(user_id1,' text' ,{
      reply_markup: {
          inline_keyboard: [    

            [{text: "✅Aceptar conseción",callback_data: "acepto"},
            {text: "❌Rechazar conseción",callback_data: "rechazo"}]

        ],
      },
  }) ,
  bot.sendMessage(user_id2,' text' ,{
    reply_markup: {
        inline_keyboard: [    

          [{text: "✅Aceptar conseción",callback_data: "acepto"},
          {text: "❌Rechazar conseción",callback_data: "rechazo"}]

      ],
    },
})  ,
]).then((results) => {
    console.log(results); // [Message, Message]
    pr = results[0].message_id;
    console.log('id 1: '+pr);
   pr2 = results[1].message_id;
    console.log('id 1: '+pr2);
});
.....

bot.editMessageText('La conseción ya ha sido aprobada✅', {message_id: pr, chat_id: 377055235, reply_markup: {
  inline_keyboard: [],
}});
bot.editMessageText('La conseción ya ha sido aprobada✅', {message_id: pr2, chat_id: 463470649, reply_markup: {
  inline_keyboard: [],
}});

Thanks i made some modificactions to the code that @sidelux post because that was giving me a polling error when i pressed the keyboard button but now it works, but still thankyou so much for your help, i understand that the id's will be lost the moment that some one send another keyboard but it's ok and thanks to you @alexandercerutti for the idea of using promises

All 9 comments

Can you show us your code please?

bot.sendMessage(user1_id,'➡️Número de teléfono: '+fide[0].numero+'\n ➡️Conseción:'+fide[0].subsidio+'\n ➡️N° de meses de nuevo contrato:'+fide[0].meses+'\n ➡️Segmento Valor:'+larry[0].seg_valor+'\n ➡️Días de mora: '+larry[0].mora+'\n ➡️Nombre del cliente: '+larry[0].nombre_cliente+'\n ➡️Fecha de alta:'+larry[0].fecha_alta+'\n ➡️Blindaje:'+larry[0].blindaje ,{
      reply_markup: {
          inline_keyboard: [    

            [{text: "✅Aceptar conseción",callback_data: "acepto"},
            {text: "❌Rechazar conseción",callback_data: "rechazo"}]

        ],
      },
  });



  bot.sendMessage(user2_id,'➡️Número de teléfono: '+fide[0].numero+'\n ➡️Conseción:'+fide[0].subsidio+'\n ➡️N° de meses de nuevo contrato:'+fide[0].meses+'\n ➡️Segmento Valor:'+larry[0].seg_valor+'\n ➡️Días de mora: '+larry[0].mora+'\n ➡️Nombre del cliente: '+larry[0].nombre_cliente+'\n ➡️Fecha de alta:'+larry[0].fecha_alta+'\n ➡️Blindaje:'+larry[0].blindaje ,{
    reply_markup: {
        inline_keyboard: [    

          [{text: "✅Aceptar conseción",callback_data: "acepto"},
          {text: "❌Rechazar conseción",callback_data: "rechazo"}]

      ],
    },
});

bot.on("callback_query", (callbackQuery) => {

 const message_id= callbackQuery.message.message_id;

  console.log(idmensaje);


  const button_data = callbackQuery.data;

if (button_data == 'acepto'){

bot.editMessageText('La conseción ya ha sido aprobada✅', {message_id: message_id, chat_id: user1_id, reply_markup: {
  inline_keyboard: [],
}});
bot.editMessageText('La conseción ya ha sido aprobada✅', {message_id: message_id, chat_id: user2_id, reply_markup: {
  inline_keyboard: [],
}});

}

});

First those are the two messages i send and the edit message works only on the user that press the button i need it to edit both messages when only one user press the button

Look at this library API: where the methods return a "Promise", you can use a Javascript Promise. A promise is a way to execute things after an async operation has been executed.

You can use Promise.all() and pass it an array of Promises (like the two .sendMessage) and get in its .then() callback as a value, an array of the contents returned by the promises.

Promise.all([
    bot.sendMessage(...),
    bot.sendMessage(...),
]).then((results) => {
    console.log(results); // [Message, Message]
});

Message is a Telegram Bot API Message.

Well, you should save both message_ids... When user click the inline button only the id of that message is sent.
So:

var message_id_1 = callbackQuery.message.message_id;
var message_id_2 = message_id_stored_somewhere;

bot.editMessageText('La conseción ya ha sido aprobada✅', {message_id: message_id_1, chat_id: user1_id, reply_markup: {
    inline_keyboard: [],
}});
bot.editMessageText('La conseción ya ha sido aprobada✅', {message_id: message_id_2, chat_id: user2_id, reply_markup: {
    inline_keyboard: [],
}});

Well, you should save both message_ids... When user click the inline button only the id of that message is sent.
So:

var message_id_1 = callbackQuery.message.message_id;
var message_id_2 = message_id_stored_somewhere;

bot.editMessageText('La conseción ya ha sido aprobada✅', {message_id: message_id_1, chat_id: user1_id, reply_markup: {
    inline_keyboard: [],
}});
bot.editMessageText('La conseción ya ha sido aprobada✅', {message_id: message_id_2, chat_id: user2_id, reply_markup: {
    inline_keyboard: [],
}});

Well my problem is how to store that message_id or both id's in case the second user is the one that press the button, i already used the promise to see the info of both messages and understand how de message_id is created but id dont know how to store the data in a variable to be used when some one press the button

Just use a file or a database, or a global array like:

message_ids[account_id][0] = first_id;
message_ids[account_id][1] = second_id;

But you lost it when you restart your bot, i suggest to use a permanent resource.

Well ill be constantly sending the keyboards so the variables should rewrite themselves so it doesnt has to be an permanent data, so i just need to store both variables to be used in the moment, but to be honest i have no idea of how to do it so if you could give me an example i would be grateful.

Maybe something like this:

var message_ids = [];

...

Promise.all([
    bot.sendMessage(...),
    bot.sendMessage(...),
]).then((results) => {
    console.log(results); // [Message, Message]
    message_ids[message.from.id][0] = results[0].message_id;
    message_ids[message.from.id][1] = results[1].message_id;
});

...

var message_id_1 = message_ids[callbackQuery.message.from.id][0];
var message_id_2 = message_ids[callbackQuery.message.from.id][1];

bot.editMessageText('La conseción ya ha sido aprobada✅', {message_id: message_id_1, chat_id: user1_id, reply_markup: {
    inline_keyboard: [],
}});
bot.editMessageText('La conseción ya ha sido aprobada✅', {message_id: message_id_2, chat_id: user2_id, reply_markup: {
    inline_keyboard: [],
}});

Is not tested but it should working as concept.

Promise.all([
    bot.sendMessage(user_id1,' text' ,{
      reply_markup: {
          inline_keyboard: [    

            [{text: "✅Aceptar conseción",callback_data: "acepto"},
            {text: "❌Rechazar conseción",callback_data: "rechazo"}]

        ],
      },
  }) ,
  bot.sendMessage(user_id2,' text' ,{
    reply_markup: {
        inline_keyboard: [    

          [{text: "✅Aceptar conseción",callback_data: "acepto"},
          {text: "❌Rechazar conseción",callback_data: "rechazo"}]

      ],
    },
})  ,
]).then((results) => {
    console.log(results); // [Message, Message]
    pr = results[0].message_id;
    console.log('id 1: '+pr);
   pr2 = results[1].message_id;
    console.log('id 1: '+pr2);
});
.....

bot.editMessageText('La conseción ya ha sido aprobada✅', {message_id: pr, chat_id: 377055235, reply_markup: {
  inline_keyboard: [],
}});
bot.editMessageText('La conseción ya ha sido aprobada✅', {message_id: pr2, chat_id: 463470649, reply_markup: {
  inline_keyboard: [],
}});

Thanks i made some modificactions to the code that @sidelux post because that was giving me a polling error when i pressed the keyboard button but now it works, but still thankyou so much for your help, i understand that the id's will be lost the moment that some one send another keyboard but it's ok and thanks to you @alexandercerutti for the idea of using promises

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Kasra-S picture Kasra-S  ·  3Comments

Rezania815 picture Rezania815  ·  3Comments

qunatumwhiskey picture qunatumwhiskey  ·  4Comments

sidelux picture sidelux  ·  3Comments

dimawebmaker picture dimawebmaker  ·  3Comments