Back to Blog
Language How-Tos

How to Build an SMS Auto-Reply Bot in Twilio Using Node.js

Build a keyword-triggered SMS auto-reply bot in Node.js using Express and TwiML MessagingResponse to reply to inbound texts.

DA
Danial A
Senior Twilio Consultant, Telphi Consulting
June 22, 2026
8 min read
Twilio
Node.js
JavaScript
How-To
Code
How to Build an SMS Auto-Reply Bot in Twilio Using Node.js

This tutorial builds a keyword-driven SMS auto-reply bot using Express.js and Twilio TwiML in Node.js that responds to inbound messages based on the words they contain. It targets developers who need to automate responses to common questions such as store hours, pricing, or FAQ answers without a human agent in the loop. You will build an Express webhook with a configurable keyword-to-response map and a fallback message for texts that match no known keyword.

Prerequisites and Setup

Install the packages with npm install twilio express dotenv and configure your Twilio number's SMS webhook to POST to your Express route. The bot uses a plain JavaScript object to map lowercase keyword strings to response text, keeping the routing logic separate from the message content so you can update responses without changing application code. Expose your server with npx ngrok http 3000 during development so Twilio can reach your endpoint. The keyword matching uses String.prototype.includes() on the normalised body, which handles cases where the keyword appears in a longer phrase.

npm install twilio express dotenv

# Twilio Console: SMS webhook
# https://xxxx.ngrok.io/bot (POST)

The Complete Code

The bot normalises each inbound body to lowercase and strips leading and trailing whitespace before checking it against each keyword using includes(). Iterating the responses object with Object.entries() and breaking on the first match ensures only one response is sent per message even if multiple keywords appear. The MessagingResponse helper builds valid TwiML without string concatenation so there is no risk of malformed XML. A fallback message at the end of the check handles any inbound text that does not match a known keyword, ensuring every message gets a reply.

require('dotenv').config();
const express = require('express');
const twilio = require('twilio');

const app = express();
app.use(express.urlencoded({ extended: false }));

const RESPONSES = {
  hours:    'We are open Mon-Fri 9am-6pm ET and Saturday 10am-4pm ET.',
  price:    'Our plans start at $29/month. Reply PLANS for a full breakdown.',
  plans:    'Starter $29/mo, Professional $79/mo, Enterprise custom. Reply TALK for a demo.',
  support:  'For support email support@telphi.com or reply TALK to speak with us.',
  talk:     'A sales rep will call you within 1 business hour. Reply with your name.',
  location: 'We serve clients nationwide. Our HQ is in New York, NY.',
};

const FALLBACK =
  'Thanks for your message! Reply HOURS, PRICE, PLANS, SUPPORT, or TALK ' +
  'to get instant answers, or a team member will follow up shortly.';

app.post('/bot', (req, res) => {
  const body = (req.body.Body || '').toLowerCase().trim();
  const twiml = new twilio.twiml.MessagingResponse();

  let matched = false;
  for (const [keyword, reply] of Object.entries(RESPONSES)) {
    if (body.includes(keyword)) {
      twiml.message(reply);
      matched = true;
      break;
    }
  }

  if (!matched) twiml.message(FALLBACK);

  res.type('text/xml');
  res.send(twiml.toString());
});

app.listen(3000, () => console.log('Bot listening on port 3000'));

Testing and Debugging

Send each keyword from a test phone and confirm the bot returns the matching response for each one. Test the fallback by sending a message with no matching keywords such as "hi" and verify the fallback text is returned. Check the Express terminal for each inbound POST to see the normalised body and confirm the matching logic is working as expected. If the bot fails to respond at all, verify ngrok is running, the webhook URL in the Twilio Console is correct, and the Express server is returning a 200 with valid TwiML XML in the response body.

Taking It Further

Load keyword-response pairs from a database or JSON configuration file so non-technical team members can update bot responses through an admin panel without touching Node.js code. Add fuzzy matching using a library like fuse.js to handle misspellings and near-matches for common keywords. Track keyword match frequency in a database to identify which topics receive the most inbound questions and use that data to expand your keyword list. Integrate an LLM API as a fallback for messages that match no keyword so the bot can answer arbitrary questions using your product documentation as context instead of returning a generic fallback message.

Contact Telphi Consulting to build a production SMS bot with NLP integration, conversation state tracking, and CRM synchronisation.

Share this article:
0 views

Ready to Transform Your Business Communications?

Get a free consultation with our VoIP experts and discover how we can help you save costs, improve efficiency, and scale your business.

Comments (0)

Join the discussion and share your thoughts (AI-moderated for quality)

Protected by AI moderation

Be the first to comment

No comments yet. Share your thoughts below.