From 390d8497d1e83453253cecd7794e82becb4c9130 Mon Sep 17 00:00:00 2001 From: Shubham Tiwari Date: Mon, 29 Jan 2024 19:37:55 +0530 Subject: [PATCH] chore: removing express dependency --- README.md | 41 ++++++++++++++++++++++++++++++++++++++++- examples/express.js | 36 ------------------------------------ package.json | 1 - 3 files changed, 40 insertions(+), 38 deletions(-) delete mode 100644 examples/express.js diff --git a/README.md b/README.md index 3177afe3e..2e9a85303 100644 --- a/README.md +++ b/README.md @@ -250,7 +250,46 @@ To use a custom HTTP client with this helper library, please see the [advanced e ### Use webhook validation -See [example](examples/express.js) for a code sample for incoming Twilio request validation. +Following code sample can be referred for incoming Twilio request validation. +```js +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}`); +}); + +``` ## Docker image 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 c9a86554f..3173a8c57 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,6 @@ "@types/url-parse": "^1.4.8", "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",