Hey everyone, I'm having trouble with my Telegram bot in Google Apps Script. Some commands aren't working right.
Here's what's happening:
- Most commands work fine
- But /requestarsip and /datalahan always trigger the default else message
- I tried making a new bot with the same code and it worked perfectly there
My handleInitialCommands function looks like this:
function handleInitialCommands(msg, chatId, msgId) {
switch(msg.toLowerCase()) {
case '/getarchive':
beginArchiveProcess(chatId, msgId);
break;
case '/fielddata':
startFieldInfo(chatId, msgId);
break;
// other cases...
default:
sendBotMessage(chatId, msgId, "Hi! Use /assist to see available commands.");
}
}
Any ideas why those two commands might not be recognized? I'm stumped!
I’ve run into similar issues before, and it can be frustrating. One thing that helped me was adding a debug line to log the exact message received. Something like:
console.log(‘Received message:’, msg);
right before your switch statement. This way, you can see exactly what’s coming in, including any hidden characters or formatting.
Another trick I found useful was to use a regular expression instead of direct comparison. It’s a bit more flexible:
if (/^/requestarsip$/i.test(msg)) {
// handle requestarsip command
} else if (/^/datalahan$/i.test(msg)) {
// handle datalahan command
}
This approach ignores case and catches commands even if there’s some weird whitespace. Might be worth a shot if the logging doesn’t reveal anything obvious.
Have you double-checked the exact spelling and casing of ‘/requestarsip’ and ‘/datalahan’ in your switch statement? It’s possible there’s a minor typo causing these commands to fall through to the default case. Also, ensure there’s no leading or trailing whitespace in the incoming ‘msg’ variable. You could try adding a console.log(msg) before the switch statement to see exactly what’s being received. Another potential issue could be character encoding - make sure your script is saved with UTF-8 encoding to handle non-ASCII characters correctly. Lastly, verify that your webhook is properly set up and receiving all updates from Telegram, as sometimes partial updates can cause inconsistent behavior.
hey mate, have u tried logging the msg before the switch? might show whats goin on. also check if theres any weird spaces or stuff in those commands. sometimes telegram does funky things with text. maybe try trimming the msg before the switch too? just a thought!