diff --git a/Dockerfile b/Dockerfile index 948b471e9..67eb134b0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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. diff --git a/advanced-examples/express-webhook-example.md b/advanced-examples/express-webhook-example.md new file mode 100644 index 000000000..7de1990fa --- /dev/null +++ b/advanced-examples/express-webhook-example.md @@ -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. \ No newline at end of file diff --git a/examples/express.js b/examples/express.js deleted file mode 100644 index 68d02475f..000000000 --- a/examples/express.js +++ /dev/null @@ -1,36 +0,0 @@ -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}`); -}); diff --git a/package.json b/package.json index 68f06bddf..23d6fea00 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,6 @@ "@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", @@ -45,8 +44,8 @@ "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",