From 33349ae31a44401866fcfbb347b49cc45e727e12 Mon Sep 17 00:00:00 2001 From: Ariel Aguilar Date: Fri, 26 Feb 2016 11:58:06 -0800 Subject: [PATCH] Updated SendSMS --- .../Web/Scripts/Views/Device/AddDeviceCopy.js | 18 ++++++- .../Web/Views/Device/_AddDeviceCopy.cshtml | 14 +++-- DeviceAdministration/Web/Web.csproj | 8 +++ .../WebApiControllers/DeviceApiController.cs | 53 +++++++++++-------- DeviceAdministration/Web/packages.config | 4 +- .../EventProcessor.WebJob.csproj | 8 +++ .../EventProcessor.WebJob/packages.config | 2 + 7 files changed, 80 insertions(+), 27 deletions(-) diff --git a/DeviceAdministration/Web/Scripts/Views/Device/AddDeviceCopy.js b/DeviceAdministration/Web/Scripts/Views/Device/AddDeviceCopy.js index cde068b0..3eece226 100644 --- a/DeviceAdministration/Web/Scripts/Views/Device/AddDeviceCopy.js +++ b/DeviceAdministration/Web/Scripts/Views/Device/AddDeviceCopy.js @@ -7,6 +7,22 @@ $(".header_main__button_back").off("click"); $(".button_send_sms").on("click", function () { - $.post("https://trackiotathletes.azurewebsites.net/api/v1/devices/sendSMS", {}); + + var data = { + deviceId: $(".js-new_device_id .text_copy_container__input--add_device_copy_table").val().trim(), + domain: $(".js-new_device_provider .text_copy_container__input--add_device_copy_table").val().trim(), + deviceKey: $(".js-new_device_key .text_copy_container__input--add_device_copy_table").val().trim(), + phoneNumber: $(".js-send_sms_phone_number").val().trim() + } + + console.log(data); + + $.ajax({ + "dataType": "json", + "type": "POST", + "url": "/api/v1/devices/sendSMS", + "cache": false, + "data": data + }) }); }); diff --git a/DeviceAdministration/Web/Views/Device/_AddDeviceCopy.cshtml b/DeviceAdministration/Web/Views/Device/_AddDeviceCopy.cshtml index 846b747b..bd0fcfbb 100644 --- a/DeviceAdministration/Web/Views/Device/_AddDeviceCopy.cshtml +++ b/DeviceAdministration/Web/Views/Device/_AddDeviceCopy.cshtml @@ -20,7 +20,7 @@ else @Strings.DeviceIdColon - + @IoTHelpers.TextCopy("DeviceId", classname, @Model.DeviceId) @@ -28,7 +28,7 @@ else @Strings.AzureDevicesHostnameColon - + @IoTHelpers.TextCopy("AzureDevicesHostname", classname, @Model.HostName) @@ -36,7 +36,7 @@ else @Strings.DeviceKeyColon - + @IoTHelpers.TextCopy("PrimaryKey", classname, @Model.PrimaryKey) @@ -47,7 +47,13 @@ else - +
+ Send Device info SMS To: + + + + +
@if (!string.IsNullOrWhiteSpace(Model.InstructionsUrl)) {
diff --git a/DeviceAdministration/Web/Web.csproj b/DeviceAdministration/Web/Web.csproj index d5cf2b01..8285363d 100644 --- a/DeviceAdministration/Web/Web.csproj +++ b/DeviceAdministration/Web/Web.csproj @@ -166,6 +166,10 @@ ..\..\packages\Owin.1.0\lib\net40\Owin.dll + + ..\..\packages\RestSharp.105.2.2\lib\net451\RestSharp.dll + True + @@ -236,6 +240,10 @@ ..\..\packages\Microsoft.AspNet.Web.Optimization.1.1.3\lib\net40\System.Web.Optimization.dll + + ..\..\packages\Twilio.4.5.0\lib\3.5\Twilio.Api.dll + True + ..\..\packages\WebGrease.1.6.0\lib\WebGrease.dll diff --git a/DeviceAdministration/Web/WebApiControllers/DeviceApiController.cs b/DeviceAdministration/Web/WebApiControllers/DeviceApiController.cs index 0ce6321e..8917f567 100644 --- a/DeviceAdministration/Web/WebApiControllers/DeviceApiController.cs +++ b/DeviceAdministration/Web/WebApiControllers/DeviceApiController.cs @@ -10,8 +10,13 @@ using Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Infrastr using Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Infrastructure.Models; using Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.DataTables; using Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.Security; +using System.Security.Cryptography; using Newtonsoft.Json.Linq; using System.Net.Http.Headers; +using Twilio; +using System.Security.Cryptography; +using System.Text; +using System.IO; namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.WebApiControllers { @@ -28,7 +33,7 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web. // POST: api/v1/devices/sample/5 [HttpPost] [Route("sample/{count}")] - [WebApiRequirePermission(Permission.AddDevices)] + [WebApiRequirePermission(Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.Security.Permission.AddDevices)] public async Task GenerateSampleDevicesAsync(int count) { ValidatePositiveValue("count", count); @@ -43,7 +48,7 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web. // GET: api/v1/devices/5 [HttpGet] [Route("{id}")] - [WebApiRequirePermission(Permission.ViewDevices)] + [WebApiRequirePermission(Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.Security.Permission.ViewDevices)] public async Task GetDeviceAsync(string id) { ValidateArgumentNotNullOrWhitespace("id", id); @@ -63,7 +68,7 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web. // Example: api/v1/devices?filterColumn=DeviceID&filterType=StartsWithCaseSensitive&filterValue=000&filterColumn=FirmwareVersion&filterType=ContainsCaseInsensitive&filterValue=564 [HttpGet] [Route("")] - [WebApiRequirePermission(Permission.ViewDevices)] + [WebApiRequirePermission(Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.Security.Permission.ViewDevices)] public async Task GetDevicesAsync( [FromUri]string search = null, [FromUri]string sortColumn = null, @@ -109,7 +114,7 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web. // This endpoint is used by the jQuery DataTables grid to get data (and accepts an unusual data format based on that grid) [HttpPost] [Route("list")] - [WebApiRequirePermission(Permission.ViewDevices)] + [WebApiRequirePermission(Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.Security.Permission.ViewDevices)] public async Task GetDevices([FromBody]JObject requestData) { return await GetServiceResponseAsync(async () => @@ -148,25 +153,30 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web. // POST: api/v1/devices/sendSMS [HttpPost] [Route("sendSMS")] - [WebApiRequirePermission(Permission.ViewDevices)] - public async Task SendSMS([FromBody]JObject requestData) + [WebApiRequirePermission(Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.Security.Permission.ViewDevices)] + public HttpResponseMessage SendSMS([FromBody]JObject requestData) { - List> parameters = new List>; - parameters.Add(new KeyValuePair("To", "+17038638414")); - parameters.Add(new KeyValuePair("From", "+12064881483")); - parameters.Add(new KeyValuePair("Body", "This is a test from the application")); - HttpContent content = new FormUrlEncodedContent(parameters); - content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded"); + // Download the twilio-csharp library from twilio.com/docs/csharp/install - return await new HttpClient().PostAsync("https://AC2314c6031b641c3b877d84ee0d58a021:ffd9483b5d66b68d9dbac234fe771952@api.twilio.com/2010-04-01/Accounts/AC2314c6031b641c3b877d84ee0d58a021/Messages", content); - } + // Find your Account Sid and Auth Token at twilio.com/user/account + string AccountSid = "AC2314c6031b641c3b877d84ee0d58a021"; + string AuthToken = "ffd9483b5d66b68d9dbac234fe771952"; + var twilio = new TwilioRestClient(AccountSid, AuthToken); + + string messageBody = requestData["domain"].ToString() + ';' + requestData["deviceId"].ToString() + ';' + requestData["deviceKey"].ToString(); + + var message = twilio.SendMessage("+12064881483", requestData["phoneNumber"].ToString(), messageBody); + Console.WriteLine(message.Sid); + + return new HttpResponseMessage(System.Net.HttpStatusCode.OK); + } // DELETE: api/v1/devices/5 [HttpDelete] [Route("{id}")] - [WebApiRequirePermission(Permission.RemoveDevices)] + [WebApiRequirePermission(Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.Security.Permission.RemoveDevices)] public async Task RemoveDeviceAsync(string id) { ValidateArgumentNotNullOrWhitespace("id", id); @@ -181,7 +191,7 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web. // POST: api/v1/devices [HttpPost] [Route("")] - [WebApiRequirePermission(Permission.AddDevices)] + [WebApiRequirePermission(Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.Security.Permission.AddDevices)] public async Task AddDeviceAsync(dynamic device) { ValidateArgumentNotNull("device", device); @@ -195,7 +205,7 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web. //PUT: api/v1/devices [HttpPut] [Route("")] - [WebApiRequirePermission(Permission.EditDeviceMetadata)] + [WebApiRequirePermission(Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.Security.Permission.EditDeviceMetadata)] public async Task UpdateDeviceAsync(dynamic device) { ValidateArgumentNotNull("device", device); @@ -210,7 +220,7 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web. //GET: api/v1/devices/5/hub-keys [HttpGet] [Route("{id}/hub-keys")] - [WebApiRequirePermission(Permission.ViewDeviceSecurityKeys)] + [WebApiRequirePermission(Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.Security.Permission.ViewDeviceSecurityKeys)] public async Task GetDeviceKeysAsync(string id) { ValidateArgumentNotNullOrWhitespace("id", id); @@ -224,7 +234,7 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web. //PUT: api/v1/devices/5/enabledstatus [HttpPut] [Route("{deviceId}/enabledstatus")] - [WebApiRequirePermission(Permission.DisableEnableDevices)] + [WebApiRequirePermission(Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.Security.Permission.DisableEnableDevices)] public async Task UpdateDeviceEnabledStatus(string deviceId, [FromBody]JObject request) { bool isEnabled; @@ -260,7 +270,7 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web. // POST: api/v1/devices/5/commands/{commandName} [HttpPost] [Route("{deviceId}/commands/{commandName}")] - [WebApiRequirePermission(Permission.SendCommandToDevices)] + [WebApiRequirePermission(Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.Security.Permission.SendCommandToDevices)] public async Task SendCommand(string deviceId, string commandName, [FromBody]dynamic parameters) { ValidateArgumentNotNullOrWhitespace("deviceId", deviceId); @@ -279,7 +289,7 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web. // DELETE: api/v1/all-devices [HttpDelete] [Route("~/api/v1/all-devices")] - [WebApiRequirePermission(Permission.RemoveDevices)] + [WebApiRequirePermission(Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web.Security.Permission.RemoveDevices)] public async Task DeleteAllDevices() { return await GetServiceResponseAsync(async () => @@ -312,4 +322,5 @@ namespace Microsoft.Azure.Devices.Applications.RemoteMonitoring.DeviceAdmin.Web. #endif } + } diff --git a/DeviceAdministration/Web/packages.config b/DeviceAdministration/Web/packages.config index 3db2978b..2003fed0 100644 --- a/DeviceAdministration/Web/packages.config +++ b/DeviceAdministration/Web/packages.config @@ -8,7 +8,6 @@ - @@ -39,14 +38,17 @@ + + + diff --git a/EventProcessor/EventProcessor.WebJob/EventProcessor.WebJob.csproj b/EventProcessor/EventProcessor.WebJob/EventProcessor.WebJob.csproj index f6f0a8a1..92cf082e 100644 --- a/EventProcessor/EventProcessor.WebJob/EventProcessor.WebJob.csproj +++ b/EventProcessor/EventProcessor.WebJob/EventProcessor.WebJob.csproj @@ -102,6 +102,10 @@ $(SolutionDir)\packages\Owin.1.0\lib\net40\Owin.dll + + ..\..\packages\RestSharp.105.2.2\lib\net451\RestSharp.dll + True + @@ -135,6 +139,10 @@ + + ..\..\packages\Twilio.4.5.0\lib\3.5\Twilio.Api.dll + True + diff --git a/EventProcessor/EventProcessor.WebJob/packages.config b/EventProcessor/EventProcessor.WebJob/packages.config index fa75a5e9..ebe95c46 100644 --- a/EventProcessor/EventProcessor.WebJob/packages.config +++ b/EventProcessor/EventProcessor.WebJob/packages.config @@ -21,7 +21,9 @@ + +