Saturday, January 6, 2018

C# Azure Function sending SMS message

Overview

An example using an Azure Function written in C# (scripting) to send text message using Twilio. The full source code is on github.

One of the key issue was to reference the Twilio.Api.dll.

From the platform Feature Dialog of the Azure Function, in the Azure Portal
  • Open KUDU Powershell console 
  • Create a ref folder in the main source folder
  • Drag and drop the Twilio.Api.dll from your desktop to the top file system in the browser





To reference the DLL from the main source file use the following syntax.
#r "D:\home\site\wwwroot\RequestSmsSending\ref\Twilio.Api.dll"



Azure Function Run Method 


#load "SendSmsHelper.csx"
#load "AzureFunctionHelper.csx"

using System.Net;

public static async Task&tl;HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{    
    var ah = new AzureFunctionHelper(req, log);

    ah.Log(log, $"-- SendSMS --");

    string smsID    = null;
    var smsText     = await ah.GetParameter("smsText");
    var responseMsg = $"Invalid parameters";
    var to          = await ah.GetParameter("to", SendSmsHelper.FRED);
   
    if(smsText == null) { 

        ah.Log(log, responseMsg);
    }
    else {

        var finalSmsText = $"[{DateTime.Now}][{smsText}]";
        smsID            = SendSmsHelper.SendSMS(finalSmsText, to);
        responseMsg      = $"To:{to}, SmsID:{smsID}, SmsText:'{finalSmsText}'";
        ah.Log(log, responseMsg);
    }
    return await ah.GetResponse(smsID != null, responseMsg);
}


No comments:

Post a Comment