{"__v":20,"_id":"56a2653b0aefae0d00b7fe46","category":{"__v":9,"_id":"56b200c0f48f270d00e0de6f","pages":["56b201173b6f690d00bdb68e","56b20ef3bf040b0d00588be4","56b214097ae4550d00062928","56b21502bf040b0d00588bea","56c1ffb1d07cc50d005e5f5e","56c74a05af5bc40d00d6bb5b","56d412b38001e30b00896dd5","56d85fc1c83c5f0b00d288a2","56d866f4a957fb1500765a40"],"project":"568fce2a04440a1700e4cb47","version":"568fce2b04440a1700e4cb4a","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-02-03T13:29:36.476Z","from_sync":false,"order":1,"slug":"c","title":"C# Quickstarts"},"parentDoc":null,"project":"568fce2a04440a1700e4cb47","user":"55116f88e2990b0d00fb0552","version":{"__v":20,"_id":"568fce2b04440a1700e4cb4a","project":"568fce2a04440a1700e4cb47","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"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-01-22T17:22:03.163Z","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.cshtml which will receive and process receipts\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \":::at:::functions {\\n    public class Receipt\\n    {\\n        public DateTime Date { get; set; }\\n        public string Recipient { get; set; }\\n        public int StatusId { get; set; }\\n        public string StatusDescription { get; set; }\\n        public string ClientRef { get; set; }\\n    }\\n\\n    public class ReceiptStore\\n    {\\n        public static List<Receipt> Receipts = new List<Receipt>();\\n    }\\n}\\n\\n@{\\n    // Do some basic parameter validation before processing\\n    if (!string.IsNullOrEmpty(Request[\\\"datetime\\\"]))\\n    {\\n        ReceiptStore.Receipts[Request[\\\"clientref\\\"]] =\\n            new Receipt\\n            {\\n                ClientRef = Request[\\\"clientref\\\"],\\n                Date = DateTime.Parse(Request[\\\"datetime\\\"]),\\n                Recipient = Request[\\\"recipient\\\"],\\n                StatusId = int.Parse(Request[\\\"statusid\\\"]),\\n                StatusDescription = Request[\\\"statusdescription\\\"]\\n            });\\n    }\\n}\\n\",\n      \"language\": \"csharp\"\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.\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\": \"    int messageId = 0;\\n\\n\\t\\tMessageResponse SendMessage(string phoneNumber)\\n    {\\n    \\t\\t...\\n\\n\\t\\t\\t\\tvar body = new[]\\n        {\\n            new\\n            {\\n                from = \\\"Example\\\",\\n                to = phoneNumber,\\n                body = \\\"Hello, this is a test message\\\",\\n                deliveryStatusUrl = \\\"https://inbound.example.com/SmsReceipts\\\",\\n                clientRef = string.Format(\\\"msg-{0}\\\", ++messageId)\\n            }\\n        };\",\n      \"language\": \"csharp\"\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.Finally, create ReceiptViewer.cshtml which will allow us to view the receipts we've processed\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<table>\\n    <tr>\\n        <th>ClientRef</th>\\n        <th>Received At</th>\\n        <th>Recipient</th>\\n        <th>StatusId</th>\\n        <th>Status Description</th>\\n    </tr>\\n    @foreach (var receipt in\\n              _Page_ReceiptHandler_cshtml.ReceiptStore.Receipts.Values)\\n    {\\n        <tr>\\n            <td>@receipt.ClientRef</td>\\n            <td>@receipt.Date</td>\\n            <td>@receipt.Recipient</td>\\n            <td>@receipt.StatusId</td>\\n            <td>@receipt.StatusDescription</td>\\n        </tr>\\n    }\\n</table>\\n\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"qs-cs-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.cshtml which will receive and process receipts [block:code] { "codes": [ { "code": "@functions {\n public class Receipt\n {\n public DateTime Date { get; set; }\n public string Recipient { get; set; }\n public int StatusId { get; set; }\n public string StatusDescription { get; set; }\n public string ClientRef { get; set; }\n }\n\n public class ReceiptStore\n {\n public static List<Receipt> Receipts = new List<Receipt>();\n }\n}\n\n@{\n // Do some basic parameter validation before processing\n if (!string.IsNullOrEmpty(Request[\"datetime\"]))\n {\n ReceiptStore.Receipts[Request[\"clientref\"]] =\n new Receipt\n {\n ClientRef = Request[\"clientref\"],\n Date = DateTime.Parse(Request[\"datetime\"]),\n Recipient = Request[\"recipient\"],\n StatusId = int.Parse(Request[\"statusid\"]),\n StatusDescription = Request[\"statusdescription\"]\n });\n }\n}\n", "language": "csharp" } ] } [/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. 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": " int messageId = 0;\n\n\t\tMessageResponse SendMessage(string phoneNumber)\n {\n \t\t...\n\n\t\t\t\tvar body = new[]\n {\n new\n {\n from = \"Example\",\n to = phoneNumber,\n body = \"Hello, this is a test message\",\n deliveryStatusUrl = \"https://inbound.example.com/SmsReceipts\",\n clientRef = string.Format(\"msg-{0}\", ++messageId)\n }\n };", "language": "csharp" } ] } [/block] [block:callout] { "type": "info", "title": "Note", "body": "The receipt handler endpoint specified in *deliveryStatusUrl* must be publicly accessible" } [/block] 3.Finally, create ReceiptViewer.cshtml which will allow us to view the receipts we've processed [block:code] { "codes": [ { "code": "<table>\n <tr>\n <th>ClientRef</th>\n <th>Received At</th>\n <th>Recipient</th>\n <th>StatusId</th>\n <th>Status Description</th>\n </tr>\n @foreach (var receipt in\n _Page_ReceiptHandler_cshtml.ReceiptStore.Receipts.Values)\n {\n <tr>\n <td>@receipt.ClientRef</td>\n <td>@receipt.Date</td>\n <td>@receipt.Recipient</td>\n <td>@receipt.StatusId</td>\n <td>@receipt.StatusDescription</td>\n </tr>\n }\n</table>\n", "language": "csharp" } ] } [/block]