r/GoogleAppsScript • • Sep 26 '20

Question Need to prevent GAS web app instances from colliding

I'm using Google Apps Script to create a web app for my Telegram bot. The web app is invoked by the bot via web hook.

My bot has an inline keyboard that you can see in action in GIF 1 below. Tapping the keyboard buttons you enter a code that appears on the screen.

My problem is when I tap the buttons a little faster, callback queries to the web app collide, which results in messed up entry. See GIF 2.

I've been racking my brain trying to figure out a way to prevent instances of the web app from collision, but I've had no luck so far.

I'm posting my code below. Please, help.

The way I see it is that every instance of the script needs more time to complete before the next one kicks in. I've tried using async/await and lockService to the best of my understanding. I've been advised to try and queue the queries, but sadly haven't been able to make it work.

function doPost(e){

var contents = JSON.parse(e.postData.contents);

var query_id = contents.callback_query.id;

var mes_id = contents.callback_query.message.message_id;

var userinput = contents.callback_query.data;

var message_now = contents.callback_query.message.text;

var inline_keyboard = contents.callback_query.message.reply_markup;

var message_upd = message_now + " " + userinput;

var keydata = {

method: "post",

payload: {

method: "editMessageText",

chat_id: String(chat_id),

message_id: mes_id,

text: message_upd ,

parse_mode: "HTML",

reply_markup: JSON.stringify(inline_keyboard)

}

}

UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/', keydata);

UrlFetchApp.fetch(url + "/answerCallbackQuery?callback_query_id=" + query_id);

}

GIF 1

GIF 2

6 Upvotes

8 comments sorted by

2

u/imthenachoman Sep 27 '20

Use LockServices. Make a lock and the next call will wait till the lock is cleared.

But, the better way would probably to handle it on the FE -- in your bot/app. It should track all the calls and handle collisions.

1

u/ipreachenglish Sep 27 '20

If I got you right, the FE in my case is Telegram, the app. I have no control over its code.

Speaking about LockServices, as I said, I tried, but to no avail. Honestly, I'm not sure I did it the right way. Here below is what it looked like. It did not help get rid of the issue at all.

I'd appreciate any thoughts on whether it's wrong, why it's wrong and how to better go about it.

var lock = LockService.getScriptLock();

function doPost(e){    
var contents = JSON.parse(e.postData.contents);    
var query_id = contents.callback_query.id;        
var mes_id = contents.callback_query.message.message_id;        
var userinput = contents.callback_query.data;        
var message_now = contents.callback_query.message.text;      
var inline_keyboard = contents.callback_query.message.reply_markup;         
var message_upd = message_now + " " + userinput;             

var keydata = {         
method: "post",         
payload: {           
method: "editMessageText",           
chat_id: String(chat_id),           
message_id: mes_id,           
text: message_upd ,           
parse_mode: "HTML",           
reply_markup: JSON.stringify(inline_keyboard)         
}       
}  

lock.waitLock(10000);

UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/', keydata);       UrlFetchApp.fetch(url + "/answerCallbackQuery?callback_query_id=" + query_id);

lock.releaseLock();

}

2

u/Pure__Anxiety Sep 27 '20

Low chance this does anything, but try putting

var lock = LockService.getScriptLock();    

inside the function?

1

u/ipreachenglish Sep 27 '20

Heard you. Put it inside the function right before lock.waitLock(10000);

So, now this part of code looks like:

var lock = LockService.getScriptLock();

lock.waitLock(10000);

UrlFetchApp.fetch('https://api.telegram.org/bot' + token + '/', keydata);

UrlFetchApp.fetch(url + "/answerCallbackQuery?callback_query_id=" + query_id);

lock.releaseLock();

Sadly, the behavior hasn't changed. Still getting the same collision.

1

u/imthenachoman Sep 27 '20

What is making the call to your GAS powered web-app?

1

u/ipreachenglish Sep 28 '20

My Telegram bot. This web-app of mine is my bot's code. The bot is a member in a group where users interact with messages containing inline keyboards. When a user taps an inline keyboard button, my bot receives an update, namely a callback query that is processed with the GAS code.

2

u/siquylee Sep 27 '20

Although Gas supports async and await/promise but everything is synchronous and it’s slow. I think it is not suitable for kind of this task. You might consider all text at once approach. Like you, I have been using Gas for Slack/Viber bot.

1

u/ipreachenglish Sep 27 '20

Yeah, all text at once is a way, definitely, but I was hoping for something else.