Any idea why my doPost function isn't able to read my start(data) function

Viewed 147

I am trying to send questions to my telegram bot from google app script, but right now it seems like my code is not executing my start function when I type /start to my telegram bot. Please advise.

function start(data) {
    var reply = {
    'text': "Please type /survey to start using it's service.",
    'parse_mode': 'Markdown',
    'disable_web_page_preview': true
        };
  var method = 'sendMessage.';
  var options = {
    'method' : 'post',
    'contentType': 'application/json',
    'payload' : JSON.stringify(reply)
  };
  var response = UrlFetchApp.fetch('https://api.telegram.org/bot' + telegramToken + '/' + method, 
  options);
}

function doPost(e){
    var data = JSON.parse(e.postData.contents);
    var text = data.message.text;
  
    if(text == '/start'){
        start(data);
    } else if (text == '/survey' || text == '/survey') {
        survey(data);
    } else {
        invalid(data);
    }
}
1 Answers

I have not much experience with google-apps-script, but you need to add chat_id.

In your case, You can get chat id from data.message.chat.id

this is a sample url:

https://api.telegram.org/bot<TOKEN>/sendMessage?chat_id=<CHAT_ID>&text=Please type /survey to start using it's service.&parse_mode=Markdown&disable_web_page_preview=true
Related