0

I'm working on a project now within Twilio, using Twilio Functions, where I'm trying to set up SMS messaging so that if we receive an incoming keyword, we respond with a specific message, including a URL. The plan is to have multiple incoming keywords, with different responses so if someone sends an SMS to one of our numbers, depending on that key word, we respond with a basic message and a URL. I'm trying to figure out the best way to handle this within Twilio Functions.

I have this working for a single incoming keyword/response, as seen below.

if (incomingMessage.includes('testpark')) {
  twiml.message('StartMyParking:\n\nTo start your parking, please click this link: https://blahblah.com');
} else if (incomingMessage.includes('bye')) {
  twiml.message('Goodbye!');
} else {
  twiml.message('Please check your zone/code and try again.');
}

While that works, I want to add in more incoming words, along with responses, such as an incoming message of 'testpark2' and a response of 'StartMyParking:\n\nTo start your parking, please click this link: https://blahblah2.com'.

Then I would want to include another one with 'testpark3' and a response of 'StartMyParking:\n\nTo start your parking, please click this link: https://blahblah3.com' and so on, all within the same script.

Can someone help me understand how to achieve this?

Swimburger
  • 6,681
  • 6
  • 36
  • 63
cruzer
  • 1
  • 1

1 Answers1

2

There are a lot of ways to achieve your desired outcome, but here's the most straightforward to begin with.

Instead of creating an else if statement for every possible keyword, you could define the keyword/response pairs up front using a JavaScript Map. The keys of the Map will be your keywords, the values of the Map will be your responses:

const keywordResponseMap = new Map([
    ['testpark2', 'StartMyParking:\n\nTo start your parking, please click this link: https://blahblah2.com'],
    ['testpark3', 'StartMyParking:\n\nTo start your parking, please click this link: https://blahblah3.com'],
    ['testpark', 'StartMyParking:\n\nTo start your parking, please click this link: https://blahblah.com'],
]);

const keywords = Array.from(keywordResponseMap.keys());
let keyword;

if (incomingMessage.includes('bye')) {
    twiml.message('Goodbye!');
}
else if (keyword = keywords.find(k => incomingMessage.includes(k))) {
    const response = keywordResponseMap.get(keyword);
    twiml.message(response);
} else {
    twiml.message('Please check your zone/code and try again.');
}

Also note that I'm putting the bye case up front because it is more performant than looking for the keywords in the incomingMessage, thus you avoid unnecessarily doing that processing when a user says bye.

You can use find to search for any keyword that is in the incomingMessage, then you can use the keyword that you found to retrieve the response from the map.

If your response will always be the same except for the URL, you could further optimize this by only storing the URL in the map and using string interpolation like this:

const keywordUrlMap = new Map([
    ['testpark2', 'https://blahblah2.com'],
    ['testpark3', 'https://blahblah3.com'],
    ['testpark', 'https://blahblah.com'],
]);

const keywords = Array.from(keywordUrlMap.keys());
let keyword;

if (incomingMessage.includes('bye')) {
    twiml.message('Goodbye!');
}
else if (keyword = keywords.find(k => incomingMessage.includes(k))) {
    const url = keywordUrlMap.get(keyword);
    twiml.message(`StartMyParking:\n\nTo start your parking, please click this link: ${url}`);
} else {
    twiml.message('Please check your zone/code and try again.');
}

It is also important to note that I'm putting testpark last in the map because testpark matches to testpark2 and testpark3. If you'd put it first, it would always resolve to testpark even with a user submits testpark2 or similar values. Also, I'm using the Map type because it guarantees the order in which the keys are returned, which is again important for the previous point.

When you have a lot more keywords and responses, you may have to start looking at a solution to store them externally like a database, and query the database by keyword to resolve the response.

Good luck, we can't wait to see what you build!

Swimburger
  • 6,681
  • 6
  • 36
  • 63