skip to Main Content

How do I make my Google App Script wait for an answer in telegram bot and run my if statement afterwards?

My idea of the code is:
When I type /survey, it will run the following code which will post a question, "how are you?". The script will wait for me to type any text, afterwards run my if statement when the answer is typed in.

function survey(data){
  var Q1 = {
    'chat_id': data.message.chat.id,
    'text': 'how are you?'
  }
  var method = 'sendMessage';
  var options = {
    'method' : 'post',
    'contentType': 'application/json',
    'payload' : JSON.stringify(Q1)
  }
  var response = UrlFetchApp.fetch('https://api.telegram.org/bot' + telegramToken + '/' + method, options);

var text = data.message.text;
if(text == ""){
   currentstep = '3'; //need help here
}

2

Answers


  1. You can use Promises or async/await, in this way your IF statement will wait for response from API and executed as soon you get response.

    async function survey(data){
      var Q1 = {
        'chat_id': data.message.chat.id,
        'text': 'how are you?'
      }
      var method = 'sendMessage';
      var options = {
        'method' : 'post',
        'contentType': 'application/json',
        'payload' : JSON.stringify(Q1)
      }
      var response = await UrlFetchApp.fetch('https://api.telegram.org/bot' + telegramToken + '/' + method, options);
    
    var text = data.message.text;
    if(text == ""){
       currentstep = '3'; //need help here
    }
    

    OR

    function survey(data){
      var Q1 = {
        'chat_id': data.message.chat.id,
        'text': 'how are you?'
      }
      var method = 'sendMessage';
      var options = {
        'method' : 'post',
        'contentType': 'application/json',
        'payload' : JSON.stringify(Q1)
      }
      UrlFetchApp.fetch('https://api.telegram.org/bot' + telegramToken + '/' + method, options).then((data)=>{
    var text = data.message.text;
    if(text == ""){
       currentstep = '3'; //need help here
    }
    })
    }
    
    Login or Signup to reply.
  2. Perhaps what can accommodate your needs is the reply_markup parameter with ForceReply, available in almost all send methods in the Telegram Bot API. As mentioned:

    ForceReply can be extremely useful if you want to create user-friendly
    step-by-step interfaces without having to sacrifice privacy mode

    You can see a sample JSON response from ForceReply here. Based on that structure, do the same thing by calling data.message.text to get the results of user input from ForceReply.

    if ( (( data || {} ).message || {}).text ){
      
      if ( data.message.text === "/start" ) {
      
        var dataForceReply = {
          method: "post",
          payload: {
            method: "sendMessage",
            chat_id: String( data.message.chat.id ),
            text: "how are you?",
            reply_markup: JSON.stringify({
              "force_reply": true
            })
          }
        };
        UrlFetchApp.fetch( telegramAPIURL + "/", dataForceReply );
    
      } else { //Assumed the user gives input
        
        var text = data.message.text;
        
        if ( text === "" ) {
          var currentstep = "3";
        } else {
          //Process according to your needs
        }
    
      }
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search