Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Love Code
Added: Jun 8, 2018 9:47 PM
Views: 11
Tags: no tags
Send delivery confirmation for message delivery status w Twilio
  1. // Twilio Credentials
  2. const accountSid = 'AC4da58e5f25ab72893b4696420b80d660';
  3. const authToken = 'your_auth_token';
  4.  
  5. // require the Twilio module and create a REST client
  6. const client = require('twilio')(accountSid, authToken);
  7.  
  8. const http = require('http');
  9. const express = require('express');
  10.  
  11. const app = express();
  12.  
  13. app.get('/confirm', (req, res) => {
  14.   const uniqueId = req.query.id;
  15.  
  16.   // Lookup constiable `uniqueId` in a database to find messageSid
  17.   const messageSid = 'SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
  18.  
  19.   // Send Feedback to Twilio
  20.   client.accounts(accountSid)
  21.     .messages(messageSid)
  22.     .feedback
  23.     .create({
  24.       outcome: 'confirmed',
  25.     })
  26.     .then(() => {
  27.       // Handle remaining request normally
  28.       res.send('Thank you!');
  29.       res.end();
  30.     })
  31.     .catch((err) => {
  32.       res.status(500);
  33.       res.send(err.toString());
  34.     })
  35.     .done();
  36. });
  37.  
  38. http.createServer(app).listen(1337, () => {
  39.   console.log('Express server listening on port 1337');
  40. });