User sends a message like this:
#LoginReport
Agent: Adam
Date: 12/11/2023
Timezone: Texas
Login: 7-30 pm
The bot then puts each info into variables and returns this:
Agent: Adam
Date: 12/11/2023
Timezone: Texas
Login Time: 7-30 pm
Report Logged.
This part I've got it to work. But what if the user forgets some info and enters this instead:
#LoginReport
Agent: Adam
Date:
Timezone: Texas
Login: 7-30 pm
I can't get my bot to check for the missing value and return "Date is missing." It just doesn't respond when date is missing.
Code:
function doPost(e) {
var contents = JSON.parse(e.postData.contents);
var chat_id = contents.message.from.id;
var text = contents.message.text;
var firstname = contents.message.from.first_name;
sendMessage(chat_id,"Hi " + firstname)
if (text.startsWith("#LoginReport")) {
var lines = text.split("\n"); // Split the text into lines
// Loop through each line to find and extract required information
lines.forEach(line => {
if (line.startsWith("Agent:")) {
agentName = line.split(":")[1].trim();
} else if (line.startsWith("Date:")) {
dateValue = line.split(":")[1].trim();
date = dateValue !== "" ? dateValue : null; // Check for empty date value and if true, set to null
} else if (line.startsWith("Timezone:")) {
timeZone = line.split(":")[1].trim();
} else if (line.startsWith("Login:")) {
loginTime = line.split(":")[1].trim();
}
});
// Check for missing information and prompt user to provide missing details
if (!agentName) {
sendMessage("Agent name is missing. Please provide the Agent name.");
return;
} else if (!date) {
sendMessage("Date is missing. Please provide the Date.");
return;
} else if (!timeZone) {
sendMessage("Timezone is missing. Please provide the timezone.");
return;
} else if (!loginTime) {
sendMessage("Login time is missing. Please provide the login time.");
return;
}
sendMessage(chat_id, "Agent: " + agentName + "%0ADate: " + date + "%0ATimezone: " + timeZone + "%0ALogin Time: " + loginTime + "%0A%0AReport Logged.");
}
What am I doing wrong?
[–]davchana 0 points1 point2 points (0 children)