SMS (Twilio)
Prerequisites
Section titled “Prerequisites”- Twilio account and a phone number with SMS capability.
- User sends SMS to your Twilio number.
- Twilio hits your webhook with
From
andBody
. - Your server posts to GeniStudio
/api/v1/message
. - You reply via Twilio Messages API.
Twilio Webhook (Express example)
Section titled “Twilio Webhook (Express example)”import express from 'express';import fetch from 'node-fetch';import twilio from 'twilio';
const app = express();app.use(express.urlencoded({ extended: false }));
app.post('/twilio/sms', async (req, res) => { const from = req.body.From; const body = req.body.Body;
const r = await fetch('https://message.geneline-x.net/api/v1/message', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-API-Key': process.env.GENISTUDIO_API_KEY!, }, body: JSON.stringify({ chatbotId: process.env.GENISTUDIO_CHATBOT_ID!, email: `${from}@sms`, message: body, }), }); const aiText = await r.text();
const MessagingResponse = twilio.twiml.MessagingResponse; const twiml = new MessagingResponse(); twiml.message(aiText.slice(0, 1600)); res.type('text/xml').send(twiml.toString());});
app.listen(3000);
Configure your Twilio number’s Messaging webhook to POST https://yourdomain.com/twilio/sms
.