{"__v":7,"_id":"56b21502bf040b0d00588bea","category":{"__v":8,"_id":"56b200c6f48f270d00e0de70","pages":["56b22b962d96461700599228","56b3240705daaa2300363143","56b33a9978a12121009006a1","56b33ab2af176a0d00964ce1","56cae5309f4ae20b00644dca","56d412878001e30b00896dd2","56d6c28947f18a2d0088d620","56d87228a957fb1500765a63"],"project":"568fce2a04440a1700e4cb47","version":"568fce2b04440a1700e4cb4a","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-02-03T13:29:42.367Z","from_sync":false,"order":2,"slug":"node","title":"Node.js 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-02-03T14:56:02.880Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":3,"body":"Now let's build on the Send SMS Messages Quickstart and add checking for dead numbers before sending the SMS. We'll use the (ValidateNumber)[doc:rest-pnv-validatenumber] method on the Phone Number Validation API to do this.\n\nFirst, we'll make a call to Validate Number\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"        request({\\n            method: 'POST',\\n            url: 'https://services.dynmark.com/webapi/phonenumbervalidation/validatenumber',\\n            auth: {\\n                'user': ':::at:::Username@',\\n                'pass': '@Password@'\\n            },\\n            timeout: 130000,\\n            json: true,\\n            body: {\\n                Number: \\\"447700900000\\\",\\n                IsInternational: true\\n            }\\n        })\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nNow we need to do something with the response from the call. Let's output to the caller\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"    .then(function(body) {\\n        switch (body.NumberStatus)\\n        {\\n            case 2:\\n                res.write(\\\"<div>You phone is on, we're sending you a message.</div>\\\"); break;\\n            case 3:\\n                res.write(\\\"<div>You phone is off, please switch it on and try again in a few minutes.</div>\\\"); break;\\n            case 4:\\n                res.write(\\\"<div>You phone number appears to be dead.</div>\\\"); break;\\n            case 5:\\n                res.write(\\\"<div>You phone number isn't on a mobile network. Have you entered a landline number.</div>\\\"); break;\\n            case 6:\\n                res.write(\\\"<div>It looks like you entered something that isn't a phone number.</div>\\\"); break;\\n        }\\n    })\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nAnd add some error handling\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"    .catch(function (err) {\\n        if (err.statusCode == 401)\\n        {\\n            res.write(\\\"<div>Invalid credentials</div>\\\")\\n        }\\n        else if (err.statusCode == 400)\\n        {\\n            res.write(\\\"<div>Bad request format</div>\\\")\\n        }\\n        else\\n        {\\n            res.write(\\\"<div>Non success response</div>\\\")\\n        }\\n        res.end();\\n    })\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nNext we need to insert our call Send Message when we've found a phone number that's switched on:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \" .then(function(body) {\\n   \\n   ...\\n   \\n        if (body.NumberStatus == 2) {\\n            request({\\n                method: 'POST',\\n                url: 'https://services.dynmark.com/webapi/message/send',\\n                auth: {\\n                    'user': '@Username@',\\n                    'pass': '@Password@'\\n                },\\n\\t\\t            timeout: 130000,\\n                json: true,\\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            })\\n            .then(function(body) {\\n                if (body[0].successful)\\n                {\\n                    res.write(\\\"<div>We sent you a message.</div>\\\")\\n                }\\n                else\\n                {\\n                    res.write(\\\"<div>Sorry, we couldn't sent you a message.</div>\\\");\\n                    if (body[0].validationFailures.some(r => r.failureCode == \\\"ToInvalid\\\"))\\n                    {\\n                        res.write(\\\"<div>Your phone number doesn't look like a valid number.</div>\\\");\\n                    }\\n                }\\n                res.end();\\n            })\\n            .catch(function (err) {\\n                if (err.statusCode == 401)\\n                {\\n                    res.write(\\\"<div>Invalid credentials</div>\\\")\\n                }\\n                else if (err.statusCode == 400)\\n                {\\n                    res.write(\\\"<div>Bad request format</div>\\\")\\n                }\\n                else\\n                {\\n                    res.write(\\\"<div>Non success response</div>\\\")\\n                }\\n                res.end();\\n            })\\n        }\\n        else {\\n            res.end();\\n        }\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"qs-node-validate-phone-numbers","type":"basic","title":"Validate Phone Numbers"}

Validate Phone Numbers


Now let's build on the Send SMS Messages Quickstart and add checking for dead numbers before sending the SMS. We'll use the (ValidateNumber)[doc:rest-pnv-validatenumber] method on the Phone Number Validation API to do this. First, we'll make a call to Validate Number [block:code] { "codes": [ { "code": " request({\n method: 'POST',\n url: 'https://services.dynmark.com/webapi/phonenumbervalidation/validatenumber',\n auth: {\n 'user': '@Username@',\n 'pass': '@Password@'\n },\n timeout: 130000,\n json: true,\n body: {\n Number: \"447700900000\",\n IsInternational: true\n }\n })", "language": "javascript" } ] } [/block] Now we need to do something with the response from the call. Let's output to the caller [block:code] { "codes": [ { "code": " .then(function(body) {\n switch (body.NumberStatus)\n {\n case 2:\n res.write(\"<div>You phone is on, we're sending you a message.</div>\"); break;\n case 3:\n res.write(\"<div>You phone is off, please switch it on and try again in a few minutes.</div>\"); break;\n case 4:\n res.write(\"<div>You phone number appears to be dead.</div>\"); break;\n case 5:\n res.write(\"<div>You phone number isn't on a mobile network. Have you entered a landline number.</div>\"); break;\n case 6:\n res.write(\"<div>It looks like you entered something that isn't a phone number.</div>\"); break;\n }\n })", "language": "javascript" } ] } [/block] And add some error handling [block:code] { "codes": [ { "code": " .catch(function (err) {\n if (err.statusCode == 401)\n {\n res.write(\"<div>Invalid credentials</div>\")\n }\n else if (err.statusCode == 400)\n {\n res.write(\"<div>Bad request format</div>\")\n }\n else\n {\n res.write(\"<div>Non success response</div>\")\n }\n res.end();\n })", "language": "javascript" } ] } [/block] Next we need to insert our call Send Message when we've found a phone number that's switched on: [block:code] { "codes": [ { "code": " .then(function(body) {\n \n ...\n \n if (body.NumberStatus == 2) {\n request({\n method: 'POST',\n url: 'https://services.dynmark.com/webapi/message/send',\n auth: {\n 'user': '@Username@',\n 'pass': '@Password@'\n },\n\t\t timeout: 130000,\n json: true,\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 })\n .then(function(body) {\n if (body[0].successful)\n {\n res.write(\"<div>We sent you a message.</div>\")\n }\n else\n {\n res.write(\"<div>Sorry, we couldn't sent you a message.</div>\");\n if (body[0].validationFailures.some(r => r.failureCode == \"ToInvalid\"))\n {\n res.write(\"<div>Your phone number doesn't look like a valid number.</div>\");\n }\n }\n res.end();\n })\n .catch(function (err) {\n if (err.statusCode == 401)\n {\n res.write(\"<div>Invalid credentials</div>\")\n }\n else if (err.statusCode == 400)\n {\n res.write(\"<div>Bad request format</div>\")\n }\n else\n {\n res.write(\"<div>Non success response</div>\")\n }\n res.end();\n })\n }\n else {\n res.end();\n }", "language": "javascript" } ] } [/block]