This is a second article showing how easy you can build value by correct compositions of two elements. This time we will try to send SMS from Azure Functions. Like in previous time – when we were sending emails – we have dedicated provider for this type of service. It is Twilio.
Twilio Configuration
In case of Twilio, we are not able to create this service from Azure Portal. We need to make the whole configuration through Twilio page and then in Azure Portal, we will consume this service.
Of course first thing that you need to do is account creation. You need to provide your data and use your mobile phone number as confirmation. After that you will be redirected to the page when you need to define project name:
After doing that you will be redirected to dashboard page. On that page you have first most crucial information – Account SID and Auth Token. Those values will be needed in the configuration of our function. Please remember the place or copy those values.
Your next step is to configure number that will be needed to be used for sending SMSes. You need to click Get Started and on the next screen click Get a number and then confirm by clicking Choose this Number:
We are ready to switch to Azure Portal. You should have saved the following information from Twilio:
- Account SID,
- Auth Token,
- Phone number.
We need to provide them during function configuration.
SMS sending from Azure Functions
Function created as a script
We will use a similar approach as in case of emails. As a base, we will use Http Trigger function. You should go to Integrate, click New Output and select Twilio SMS:
Then after clicking Select you need to provide configuration of Twilio SMS service:
Please be careful in case of Auth Token setting and Account SID setting parameters. In these cases, you need to provide identifier name from App Setting where those values will be stored. I believe that rest of the values are clear.
After clicking save configuration will be kept in function.json file:
To add a new key in App Setting you need to open Platform features, then Application settings and add two keys. Please remember – name should be the same as you provided in function Integrate section.
Writing code will be a bit harder because Documentation section is empty. The approach is the same – we will use SMS message as output parameter:
#r "Twilio.Api" using System; using System.Net; using Twilio; public static HttpResponseMessage Run(HttpRequestMessage req, out SMSMessage messagec) { // parse query parameter string messageText = req.GetQueryNameValuePairs() .FirstOrDefault(q => string.Compare(q.Key, "message", true) == 0) .Value; message = new SMSMessage(); message.Body = messageText; return req.CreateResponse(HttpStatusCode.Accepted); }
As you can see the function is very easy. We need only to create a SMSMessage object and that is all.
Of course you can adjust parameters of SMS as you wish by extending this code.
Function created as code file
We also have the second option – we can use attributes to configure Twilio integration. The steps are pretty the same. Only the code looks a bit different:
[FunctionName("SendNotificationSMS")] public static void Run( [QueueTrigger("tosendnotification", Connection = "registration2storage_STORAGE")] Customer customer, [TwilioSms( To = "+48123456789", From = "+4812345678", Body = "New customer {Name} {Surname}!")] out SMSMessage message) { message = new SMSMessage(); }
Please note that in this case we can consume values that come in object from queue directly in Twilio attribute.
Dzień dobry,
Zastanawia nas jaki model telefonu Yealink działałby z Twilio ?
Wydaje mi się, że każdy wspierający protokuł SIP. Tylko proszę zauważyć, że Twilio służszy do rozszerzenia obecnego rozwiązania / zbudowanie jego bardziej inteligentej / automatycznej wersji niż do dostarczania usług VOIP. Proszę spojrzeć na stronę: https://www.twilio.com/voice/sip-registration