I’m having trouble with my Telegram bot in Google Apps Script. Most commands work fine, but /requestarsip
and /datalahan
don’t trigger the right functions. Instead, they end up hitting the default else block.
Here’s a simplified version of my code:
function processCommands(message, chatId, msgId) {
switch(message.toLowerCase()) {
case '/getarchive':
beginArchiveProcess(chatId, msgId);
break;
case '/fielddata':
initFieldDataRetrieval(chatId, msgId);
break;
case '/help':
showCommandList(chatId, msgId);
break;
default:
sendDefaultMessage(chatId, msgId);
}
}
I’ve tried replicating the bot in a new script file, where every command functions properly. I’m wondering what might be causing this issue in my original file and how to troubleshoot it.
Have you considered the possibility of whitespace or invisible characters in the incoming messages? It’s worth implementing a more robust command parsing system. Try using a regular expression to match commands, like this:
function processCommands(message, chatId, msgId) {
const command = message.trim().toLowerCase().split(/\s+/)[0];
switch(command) {
case '/getarchive':
case '/requestarsip':
beginArchiveProcess(chatId, msgId);
break;
case '/fielddata':
case '/datalahan':
initFieldDataRetrieval(chatId, msgId);
break;
// ... other cases ...
}
}
This approach allows for multiple command variations and is more forgiving of extra spaces. Also, double-check your webhook setup to ensure it’s correctly forwarding all commands to your script.
hey man, have u tried debugging the message variable? maybe theres some weird formatting goin on. also, check if ur bot is set up to handle commands with its username like /requestarsip@yourbot. sometimes that trips things up. good luck!
I’ve encountered similar issues with Telegram bots in Google Apps Script before. From your description, it sounds like there might be some hidden characters or encoding problems in the message itself. I would recommend logging the raw content to check for any unexpected characters and applying the trim() function to the message before processing it further. In addition, verify that your bot is correctly configured to handle commands that might include its username, such as /command@yourbotname. It might be useful to compare your working script with the problematic one to spot subtle differences in how the messages are handled. Also, consider testing the bot in both private and group chats, as group settings can sometimes interfere with command recognition.