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);
}


2 comments:

  1. SMS API C# offers simple and efficient method for connecting corporate applications to the global SMS networks without having to deal with telecom contracts, routing and complex messaging protocols.

    ReplyDelete
  2. Thanks for sharing this blog. We have now started providing bulk SMS API C# India that is very powerful and easy to integrate into your own software/application/website. As it saves your lot of valuable time in logging our interface again and again.

    ReplyDelete