SUPPORT.TWILIO.COM END OF LIFE NOTICE: This site,, is scheduled to go End of Life on February 27, 2024. All Twilio Support content has been migrated to, where you can continue to find helpful Support articles, API docs, and Twilio blog content, and escalate your issues to our Support team. We encourage you to update your bookmarks and begin using the new site today for all your Twilio Support needs.

Building an SMS keyword response application

SMS keyword applications are designed to send customized SMS replies based on the ‘Body’ of the incoming text message.

When an SMS message is received at your Twilio phone number, your application will fetch the ‘Body’ request parameter of the message. Your application will then match the text of your incoming message with the keywords you have listed and send a reply.


The following is a PHP code example of a SMS Keyword Response Application. When an animal name messaged to a Twilio phone number, the definition of the animal with a photo is replied in response.

// Code for Twilio Support Document:
// Get the PHP helper library from
require __DIR__ . '/vendor/autoload.php'; // Loads the library. This may vary depending on how you installed the library.
use Twilio\Rest\Client;

** Your Account Sid and Auth Token from
*/ $sid = "YourTwilioAccountSID"; $token = "YourTwilioAuthToken"; $client = new Client($sid, $token);
** Array of response messages, to represent the function of a database.
*/ $responseMessages = array( 'monkey' => array('body' => 'Monkey. A small to medium-sized primate that typically has a long tail, most kinds of which live in trees in tropical countries.', 'media' => ''), 'dog' => array('body' => 'Dog. A domesticated carnivorous mammal that typically has a long snout, an acute sense of smell, and a barking, howling, or whining voice.', 'media' => ''), 'pigeon' => array('body' => 'Pigeon. A stout seed- or fruit-eating bird with a small head, short legs, and a cooing voice, typically having gray and white plumage.', 'media' => ''), 'owl' => array('body' => 'Owl. A nocturnal bird of prey with large forward-facing eyes surrounded by facial disks, a hooked beak, and typically a loud call.', 'media' => '') );
** Default response message when receiving a message without key words.
*/ $defaultMessage = "Reply with one of the following keywords: monkey, dog, pigeon, owl.";
** Read the contents of the incoming message fields.
*/ $body = $_REQUEST['Body']; $to = $_REQUEST['From']; $from = $_REQUEST['To'];
** Remove formatting from $body until it is just lowercase
** characters without punctuation or spaces.
*/ $result = preg_replace("/[^A-Za-z0-9]/u", " ", $body); $result = trim($result); $result = strtolower($result); $sendDefault = true; // Default message is sent unless key word is found in following loop.
** Choose the correct message response and set default to false.
*/ foreach ($responseMessages as $animal => $messages) { if ($animal == $result) { $body = $messages['body']; $media = $messages['media']; $sendDefault = false; } }
// Send the correct response message. if ($sendDefault != false) { $client->messages->create( $to, array( 'from' => $from, 'body' => $defaultMessage, ) ); } else { $client->messages->create( $to, array( 'from' => $from, 'body' => $body, 'mediaUrl' => $media, ) ); }
Have more questions? Submit a request
Powered by Zendesk