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