Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ ENV NODE_PATH /usr/local/lib/node_modules

COPY src ./src
COPY spec ./spec
COPY examples ./examples
COPY index.* package.json babel.config.js tsconfig.json ./

RUN npm install --unsafe-perm true # Needed to run prepublish as root.
Expand Down
112 changes: 112 additions & 0 deletions advanced-examples/express-webhook-example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# Express Webhook Example

This example demonstrates how to use Twilio webhooks with Express.js.

## Setup

First, install Express as a dependency in your project:

```bash
npm install express
```

You may also need body-parser for parsing request bodies:

```bash
npm install body-parser
```

## Code

Here's the complete example:

```javascript
const twilio = require("twilio");
const bodyParser = require("body-parser");
const MessagingResponse = require("twilio").twiml.MessagingResponse;

const authToken = process.env.TWILIO_AUTH_TOKEN;

const express = require("express");
const app = express();
const port = 3000;

app.use(
bodyParser.json({
verify: (req, res, buf) => {
req.rawBody = buf;
},
})
);

app.get("/", (req, res) => {
res.send("Hello World!");
});

app.post("/message", twilio.webhook(authToken), (req, res) => {
// Twilio Messaging URL - receives incoming messages from Twilio
const response = new MessagingResponse();

response.message(`Your text to me was ${req.body.Body}.
Webhooks are neat :)`);

res.set("Content-Type", "text/xml");
res.send(response.toString());
});

app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`);
});
```

## Alternative: Without Express

If you prefer not to use Express, you can validate Twilio requests manually:

```javascript
const twilio = require("twilio");
const http = require("http");
const url = require("url");
const querystring = require("querystring");

const authToken = process.env.TWILIO_AUTH_TOKEN;

const server = http.createServer((req, res) => {
if (req.method === 'POST' && req.url === '/message') {
let body = '';
req.on('data', chunk => {
body += chunk.toString();
});

req.on('end', () => {
const signature = req.headers['x-twilio-signature'];
const requestUrl = `https://${req.headers.host}${req.url}`;
const params = querystring.parse(body);

const isValidRequest = twilio.validateRequest(authToken, signature, requestUrl, params);

if (isValidRequest) {
const response = new twilio.twiml.MessagingResponse();
response.message(`Your text to me was ${params.Body}. Webhooks are neat :)`);

res.writeHead(200, {'Content-Type': 'text/xml'});
res.end(response.toString());
} else {
res.writeHead(403);
res.end('Forbidden');
}
});
} else {
res.writeHead(404);
res.end('Not Found');
}
});

server.listen(3000, () => {
console.log('Server listening on port 3000');
});
```

## Notes

The Express example was moved to the advanced-examples directory to reduce the package size. Express is not required for using the Twilio SDK - it's just one way to handle webhook requests.
36 changes: 0 additions & 36 deletions examples/express.js

This file was deleted.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,15 @@
"@types/qs": "^6.9.7",
"babel-plugin-replace-ts-export-assignment": "^0.0.2",
"eslint": "^8.31.0",
"express": "^4.17.1",
"jest": "^29.5.5",
"jshint": "^2.11.0",
"mock-fs": "^5.2.0",
"nock": "^13.2.9",
"node-mocks-http": "^1.8.1",
"prettier": "^2.7.1",
"ts-jest": "^29.1.1",
"typescript": "5.0.4",
"typedoc": "^0.23.21"
"typedoc": "^0.23.21",
"typescript": "5.0.4"
},
"scripts": {
"test": "npm run test:javascript && npm run test:typescript",
Expand Down