Home > Back-end >  Finding telegram message_id while using google app script
Finding telegram message_id while using google app script

Time:12-19

I am trying to obtain the message ID of the latest message I have using the bot coded on google app script, but I seem to have trouble getting it.

Here is my sendMessage function:

function sendMessage(chat_id, message) {
  const data = {
    method:"post",
    payload:{
      method:"sendMessage",
      chat_id: String(chat_id),
      text: message,
      parse_mode: "HTML"
    }
  };

  UrlFetchApp.fetch(teleUrl   "/", data);
}

If my understanding of asynchronous functions is right, have tried using an asynchronous function to wait for a reply, but when I try running the function on google app script, I get returned an empty object. However, based on Telegram's API documentation, I am supposed to get back a "message" object.

async function test() {
  const message = await sendMessage(chat_id, "HI");
  return message;
}

function greeting() {
  const message = test();
  Logger.log(message);
//result is {}, an empty object
}

CodePudding user response:

The endpoint is synchronous. Also, sendMessage is not returning any value.

function sendMessage(chat_id, message) {
  const data = {
    method:"post",
    payload:{
      method:"sendMessage",
      chat_id: String(chat_id),
      text: message,
      parse_mode: "HTML"
    }
  };
  const response = UrlFetchApp.fetch(teleUrl   "/", data);
  const content = response.getContentText();
  const message = JSON.parse(content);
  return message;
}

function greeting() {
  const message = sendMessage();
  Logger.log(message);
  const {message_id} = message;
  Logger.log(message_id);
}
  • Related