我的代码有问题,命令未正确触发。这是我的代码。
function handleTextMessage(message, chatId, messageId, firstName, lastName, userName) {
const receivedTextMessage = message.text.replace(botHandle, '').trim();
const isPendingArsip = PropertiesService.getScriptProperties().getProperty(`pendingArsip_${chatId}`);
const isPendingLahan = PropertiesService.getScriptProperties().getProperty(`pendingCekLahan_${chatId}`);
const isPendingCekNIK = PropertiesService.getScriptProperties().getProperty(`pendingCekNIK_${chatId}`);
const isPendingAjuan = PropertiesService.getScriptProperties().getProperty(`pendingAjuan_${chatId}`);
const isPendingKoordinat = PropertiesService.getScriptProperties().getProperty(`pendingKoordinat_${chatId}`);
if (!isPendingArsip && !isPendingLahan && !isPendingCekNIK && !isPendingAjuan && !isPendingKoordinat) {
handleInitialCommands(receivedTextMessage, chatId, messageId);
} else if (isPendingArsip && receivedTextMessage.startsWith("Nama Dokumen :")) {
handleFormArsip(receivedTextMessage, chatId, messageId, firstName, lastName, userName);
} else if (isPendingLahan && receivedTextMessage) {
handleFormLahan(receivedTextMessage, chatId, messageId, firstName, lastName, userName);
} else if (isPendingCekNIK && receivedTextMessage) {
handleFormNIK(receivedTextMessage, chatId, messageId, firstName, lastName, userName);
} else if (isPendingAjuan && receivedTextMessage.startsWith("NIK :")) {
handleFormData(receivedTextMessage, chatId, messageId, firstName, lastName, userName);
} else if (isPendingKoordinat && receivedTextMessage.startsWith("NIK :")) {
handleKoordinatFormData(receivedTextMessage, chatId, messageId);
} else if (isPendingArsip && receivedTextMessage.toLowerCase() === '/break') {
cancelArsip(chatId, messageId);
} else if (isPendingLahan && receivedTextMessage.toLowerCase() === '/break') {
cancelCekLahan(chatId, messageId);
} else if (isPendingCekNIK && receivedTextMessage.toLowerCase() === '/break') {
cancelCekNIK(chatId, messageId);
} else if (isPendingAjuan && receivedTextMessage.toLowerCase() === '/break') {
cancelAjuan(chatId, messageId);
} else if (isPendingKoordinat && receivedTextMessage.toLowerCase() === '/break') {
cancelKoordinat(chatId, messageId);
}
}
function handleInitialCommands(receivedTextMessage, chatId, messageId) {
if (receivedTextMessage.toLowerCase() === '/daftarsimluh') {
startCekNIK(chatId, messageId);
} else if (receivedTextMessage.toLowerCase() === '/requestarsip') {
startArsip(chatId, messageId);
} else if (receivedTextMessage.toLowerCase() === '/datalahan') {
startLahan(chatId, messageId);
} else if (receivedTextMessage.toLowerCase() === '/koordinatsawah') {
startKoordinatSawah(chatId, messageId);
} else if (receivedTextMessage.toLowerCase() === '/ceknik') {
startCekNIK(chatId, messageId);
} else if (receivedTextMessage.toLowerCase() === '/info') {
startInfoData(chatId, messageId);
} else if (receivedTextMessage.toLowerCase() === '/update') {
startHelpDeskUpdate(chatId, messageId);
} else if (receivedTextMessage.toLowerCase() === '/help') {
startHelpDeskCommand(chatId, messageId);
} else if (receivedTextMessage.toLowerCase() === '/reset') {
resetScriptProperties();
} else if (receivedTextMessage.toLowerCase() === '/start') {
sendTelegramMessage(chatId, messageId, "Halo! Status bot dalam keadaan aktif.");
} else {
sendTelegramMessage(chatId, messageId, `Halo! \n\nSilahkan gunakan perintah /help untuk melihat pilihan perintah yang dapat digunakan`);
}
}
所以,我的代码中有一个
handleCommand
函数。当我尝试使用 /requestarsip
和 /datalahan
命令时,该函数会为我触发最后一个 else 选项,但其他命令在我使用它们时工作正常。我已经尝试将完整代码复制到不同的应用程序脚本文件中并创建了一个新的机器人,并且所有命令都可以正常工作。我很好奇第一个文件和新文件之间有什么问题以及有什么区别。
将参数对象打印到执行日志,以确认预期值/对象已分配给函数参数。换句话说,添加
console.log(JSON.stringify(arguments));
。
另一件可能有用的事情是您创建一个可以帮助您测试和调试代码的函数。如果您这样做,请在函数的“{”后面添加上述语句。
/**
* This is to be executed using the debug button after setting an execution break point.
*/
function test_handleTextMessage(){
const message = "Sample Message",
chatId = "sampleChatId",
messageId = "sampleMessageId",
firstName = "Sample First Name",
lastName = "Sample Last Name",
userName = "sampleUserName";
handleTextMessage(message, chatId, messageId, firstName, lastName, userName)
}
function handleTextMessage(message, chatId, messageId, firstName, lastName, userName) {
console.logs(JSON.stringify(arguments));
// put here the handleTextMessage's block statements
}
参考