{"_id":"56b20ef3bf040b0d00588be4","project":"568fce2a04440a1700e4cb47","user":"55116f88e2990b0d00fb0552","parentDoc":null,"version":{"_id":"568fce2b04440a1700e4cb4a","project":"568fce2a04440a1700e4cb47","__v":20,"createdAt":"2016-01-08T14:56:43.101Z","releaseDate":"2016-01-08T14:56:43.101Z","categories":["568fce2b04440a1700e4cb4b","568fd1b8b700ce0d002f4b1c","568fd23804440a1700e4cb5b","568fd2444719c119002ce5d8","568ff21204440a1700e4cbc1","5693732c8aa8040d009f2c28","5693738393445b0d00abdad0","5693740093445b0d00abdad1","56937445974aaa0d001ca699","5693b82173f48f0d0075c90d","5694c4cd1005590d0062cb25","569f854466a5640d00efa54c","56a264cdd15dd70d008d825b","56aa56bf318e6c1700a19ddb","56b0e6347ae4550d000627bd","56b200c0f48f270d00e0de6f","56b200c6f48f270d00e0de70","56b22a9665ddf50d0076ba40","56e92ef71996862200fd7f42","574d6577fb835c0e00ca316a"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.0.0","version":"1.0"},"__v":8,"category":{"_id":"56b200c6f48f270d00e0de70","pages":["56b22b962d96461700599228","56b3240705daaa2300363143","56b33a9978a12121009006a1","56b33ab2af176a0d00964ce1","56cae5309f4ae20b00644dca","56d412878001e30b00896dd2","56d6c28947f18a2d0088d620","56d87228a957fb1500765a63"],"project":"568fce2a04440a1700e4cb47","version":"568fce2b04440a1700e4cb4a","__v":8,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-02-03T13:29:42.367Z","from_sync":false,"order":2,"slug":"node","title":"Node.js Quickstarts"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-02-03T14:30:11.240Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":1,"body":"In the Send SMS Messages quickstart, we sent a message, but we don't know whether it's been delivered to the recipient yet; they may have their phone switched off, be in an area with no mobile coverage or the number may be out of service, so it'd be good to get some more information. To do this we'll use [Receipt Forwarding](doc:receipt-forwarding-overview).\n\nBuilding on the last sample, we're going to create a receipt handler that logs the status of each message to an in memory data store.\n\n1.Create receiptHandler.js which will receive and process receipts\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var express = require('express');\\nvar router = express.Router();\\nglobal.receipt = {};\\n\\n/* GET users listing. */\\nrouter.get('/receiptHandler', function(req, res, next) {\\n    var queryData = req.query;\\n    \\n    global.receipt[queryData.clientref] = {\\n        date: queryData.datetime,\\n        recipient: queryData.recipient,\\n        statusId: queryData.statusid,\\n        statusDescription: queryData.statusdescription,\\n        clientRef: queryData.clientref\\n    };\\n        \\n    res.writeHead(200, {\\\"Content-Type\\\": \\\"text/html\\\"});\\n    res.end();\\n});\\n\\nmodule.exports = router;\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nWe log the date the message status changed, numeric status id, the status description, the phone number that the receipt relates to and the clientRef that was specified with the message at send time.\nWe also return a 200 OK status to indicate to the sender that we've processed the receipt and don't need it to be sent again later\n\n2. We now need to enable the sending of receipts to our new handler by adding *deliverStatusUrl* to our SendMessage function from the [Send SMS Messages](doc:qs-send-sms-messages) quickstart. We'll also add a clientRef to help us match the receipt to the message that caused it to be sent:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"    var messageId = 0;\\n\\n    \\t\\t...\\n\\n            body: [{\\n                from: \\\"Example\\\",\\n                to: \\\"447700900000\\\",\\n                body: \\\"Hello, this is a test message\\\",\\n                deliveryStatusUrl: \\\"https://inbound.example.com/SmsReceipts\\\",\\n                clientRef: \\\"msg-\\\" + (++messageId).toString()\\n            }]\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Note\",\n  \"body\": \"The receipt handler endpoint specified in *deliveryStatusUrl* must be publicly accessible\"\n}\n[/block]\n3. Add a jade view called receiptViewer.jade which will allow us to view the receipts we've processed\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"\\nextends layout\\n\\nblock content\\n    table\\n        thead\\n            th ClientRef\\n            th Received At\\n            th Recipient\\n            th StatusId\\n            th Status Description\\n        tbody\\n            each val in global.receipt\\n                tr\\n                    td= val.clientRef\\n                    td= val.date\\n                    td= val.recipient\\n                    td= val.statusId\\n                    td= val.statusDescription\",\n      \"language\": \"text\",\n      \"name\": \"jade\"\n    }\n  ]\n}\n[/block]\n4. Finally, create receiptViewer.js which will plumb in the new view\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var express = require('express');\\nvar router = express.Router();\\n\\n/* GET home page. */\\nrouter.get('/receiptViewer', function(req, res, next) {\\n  res.render('receiptViewer');\\n});\\n\\nmodule.exports = router;\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"qs-node-receive-sms-receipts","type":"basic","title":"Receive SMS Receipts"}

Receive SMS Receipts


In the Send SMS Messages quickstart, we sent a message, but we don't know whether it's been delivered to the recipient yet; they may have their phone switched off, be in an area with no mobile coverage or the number may be out of service, so it'd be good to get some more information. To do this we'll use [Receipt Forwarding](doc:receipt-forwarding-overview). Building on the last sample, we're going to create a receipt handler that logs the status of each message to an in memory data store. 1.Create receiptHandler.js which will receive and process receipts [block:code] { "codes": [ { "code": "var express = require('express');\nvar router = express.Router();\nglobal.receipt = {};\n\n/* GET users listing. */\nrouter.get('/receiptHandler', function(req, res, next) {\n var queryData = req.query;\n \n global.receipt[queryData.clientref] = {\n date: queryData.datetime,\n recipient: queryData.recipient,\n statusId: queryData.statusid,\n statusDescription: queryData.statusdescription,\n clientRef: queryData.clientref\n };\n \n res.writeHead(200, {\"Content-Type\": \"text/html\"});\n res.end();\n});\n\nmodule.exports = router;", "language": "javascript" } ] } [/block] We log the date the message status changed, numeric status id, the status description, the phone number that the receipt relates to and the clientRef that was specified with the message at send time. We also return a 200 OK status to indicate to the sender that we've processed the receipt and don't need it to be sent again later 2. We now need to enable the sending of receipts to our new handler by adding *deliverStatusUrl* to our SendMessage function from the [Send SMS Messages](doc:qs-send-sms-messages) quickstart. We'll also add a clientRef to help us match the receipt to the message that caused it to be sent: [block:code] { "codes": [ { "code": " var messageId = 0;\n\n \t\t...\n\n body: [{\n from: \"Example\",\n to: \"447700900000\",\n body: \"Hello, this is a test message\",\n deliveryStatusUrl: \"https://inbound.example.com/SmsReceipts\",\n clientRef: \"msg-\" + (++messageId).toString()\n }]", "language": "javascript" } ] } [/block] [block:callout] { "type": "info", "title": "Note", "body": "The receipt handler endpoint specified in *deliveryStatusUrl* must be publicly accessible" } [/block] 3. Add a jade view called receiptViewer.jade which will allow us to view the receipts we've processed [block:code] { "codes": [ { "code": "\nextends layout\n\nblock content\n table\n thead\n th ClientRef\n th Received At\n th Recipient\n th StatusId\n th Status Description\n tbody\n each val in global.receipt\n tr\n td= val.clientRef\n td= val.date\n td= val.recipient\n td= val.statusId\n td= val.statusDescription", "language": "text", "name": "jade" } ] } [/block] 4. Finally, create receiptViewer.js which will plumb in the new view [block:code] { "codes": [ { "code": "var express = require('express');\nvar router = express.Router();\n\n/* GET home page. */\nrouter.get('/receiptViewer', function(req, res, next) {\n res.render('receiptViewer');\n});\n\nmodule.exports = router;", "language": "javascript" } ] } [/block]