In this blog post I am going to dive into the use case of sending a WhatsApp message from Business Central utilizing Twilio framework.
Most of my readers are familiar with “as a Service” acronyms like IaaS, PaaS, SaaS, and Microsoft’s Azure platform supports each of these concepts. Below are a few examples:

I was introduced recently to the concept of CPaaS – Communication Platform as a Service and its multifaceted applications in the modern world.
Microsoft Azure has its own implementation of CPaaS via Azure Communication Services. See more about it on the Microsoft Learn platform.
I would like to share my experience with a Twilio product in this blog post.
The company focuses on building platforms for communication channels and applications:

Their SMS and WhatsApp messaging platform caught my attention.
This is the exercise I worked through: a sales order in Business Central gets posted. This triggers a POST request to an Azure Function which, in turn, leverages Twilio’s messaging API to send WhatsApp messages via Twilio platform.
Business Central – Azure Function POST request
In Business Central, subscribe to an event during posting of a sales order. The SMS/WhatsApp could also be sent at various times during document lifecycle.
codeunit 50100 "Sales Mgt.SVIR"
{
[EventSubscriber(ObjectType::Codeunit, Codeunit::"Sales-Post", 'OnAfterFinalizePostingOnBeforeCommit', '', false, false)]
local procedure CallTwilioAzureFunc(var EverythingInvoiced: Boolean; WhseReceive: Boolean; WhseShip: Boolean;
CommitIsSuppressed: Boolean;
PreviewMode: Boolean;
var GenJnlPostLine: Codeunit "Gen. Jnl.-Post Line";
var ReturnReceiptHeader: Record "Return Receipt Header";
var SalesCrMemoHeader: Record "Sales Cr.Memo Header";
var SalesHeader: Record "Sales Header";
var SalesInvoiceHeader: Record "Sales Invoice Header";
var SalesShipmentHeader: Record "Sales Shipment Header")
var
_cust: Record Customer;
_phone: Text;
_message: Text;
_jsonBody: Text;
_httpContent: HttpContent;
_httpResponse: HttpResponseMessage;
_httpHeader: HttpHeaders;
_httpClient: HttpClient;
_url: Label 'https://twillioengine.azurewebsites.net/api/TwilioFunc?code=6iN3A3VgOeFD9EOm9MKaI37seFBctvwuw_Ge7rCaY4XbAzFuLvI2oA==';
begin
if PreviewMode then
exit;
if SalesHeader.IsTemporary then
exit;
if _cust.Get(SalesHeader."Sell-to Customer No.") then begin
if _cust."Phone No." <> '' then begin
// build json for POST request with customer cell phone number and the message to send
_phone := '1' + _cust."Phone No.";
_message := SalesHeader."No." + ' was just processed.';
_jsonBody := ' {"PhoneTo":"' + _phone + '","message":"' + _message + '"}';
// build POST http request
_httpContent.WriteFrom(_jsonBody);
_httpContent.GetHeaders(_httpHeader);
_httpHeader.Remove('Content-Type');
_httpHeader.Add('Content-Type', 'application/json');
_httpClient.Post(_url, _httpContent, _httpResponse);
if not _httpResponse.IsSuccessStatusCode then begin
Error(StrSubstNo('Error - Received %1', Format(_httpResponse.HttpStatusCode)));
end;
end;
end;
end;
}
Set up Twilio sandbox
Before creating the Azure Function set up your free trial Twilio account.
There is a link on the company’s website for signing up for a free trial.

If you decide to promote the project to production you can upgrade and the monthly bill will consist of charges for:
- number of phone numbers rented from Twilio and
- number of messages sent and received.
After trial, there is 1$ per month per phone number and 0.007 $ per message.
You would need to set up a:
- Messaging Service for SMS
- a Twilio phone number for WhatsApp messages
This is how Twilio UI looks like for setting app Messaging Service:

And this is the screen where you get assigned a Twilio number for WhatsApp messages:

From Twilio platform let’s attempt to send a SMS:

You need to choose:
- the destination for your test SMS
- Twilio messaging service
- And the test SMS
On the right side, Twilio platform displays sample source code in several programming languages.
Below is the the C# source code for sending a SMS via Twilio platform.
This sample code can go into an Azure function:
using System;
using System.Collections.Generic;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;
class Example
{
static void Main(string[] args)
{
var accountSid = "ACe00a276056a4a696ff14691a3bb40f3b";
var authToken = "[AuthToken]";
TwilioClient.Init(accountSid, authToken);
var messageOptions = new CreateMessageOptions(
new PhoneNumber("+16199307581"));
messageOptions.MessagingServiceSid = "MGb06079125586edf64f3c13f51843d07e";
messageOptions.Body = "Testing for blog";
var message = MessageResource.Create(messageOptions);
Console.WriteLine(message.Body);
}
}
Similarly, to get the source code for sending a WhatsApp message, go to “Send a WhasApp message” blade.

And after connecting to Twilio assigned sandbox you get this on your phone:

Click on “Send One Way” message:

There are 3 supported use cases: Appointment Reminders, Order Notification, Verification Codes. For my example I will choose Order Notification:

And below is the C# sample code for sending the WhatsApp message:
using System;
using System.Collections.Generic;
using Twilio;
using Twilio.Rest.Api.V2010.Account;
using Twilio.Types;
class Example
{
static void Main(string[] args)
{
var accountSid = "ACe00a276056a4a696ff14691a3bb40f3b";
var authToken = "[AuthToken]";
TwilioClient.Init(accountSid, authToken);
var messageOptions = new CreateMessageOptions(
new PhoneNumber("whatsapp:+16199307581"));
messageOptions.From = new PhoneNumber("whatsapp:+14155238886");
messageOptions.Body = "Your Yummy Cupcakes Company order of 1 dozen frosted cupcakes has shipped and should be delivered on July 10, 2019. Details: http://www.yummycupcakes.com/";
var message = MessageResource.Create(messageOptions);
Console.WriteLine(message.Body);
}
}
A handshake has to take place between a target phone number and the Twilio sandbox.
For the receiving phone to join Twilio sandbox, send from the receiver a message “join fall-wood” to Twilio sandbox.
This could also be triggered with an Azure Function POST request from Business Central that sends the WhatsApp message to the Twilio number.
Writing the Azure function
When it comes to creating the Azure Function, one could:
- develop inside the Azure Platform:
- login into Azure Platform
- Create new function app and new trigger function
- There is a Test/Debug functionality as well as a Monitor

- Develop in VS Code
- My choice for development environment was Visual Studio:
- create a new Azure Function project
- create a new trigger function

Lastly, publish the function to Azure:

Once deployed, click on “Get Function URL” and paste it in your AL code:

Publish extension:

Test Scenario
To conclude, we can test it by posting a sales order whose customer has a phone number that joined the Twilio sandbox. After all, the message appears in WhatsApp:

Consequently, with transition to cloud, Business Central can now integrate with new and emerging communication platforms like Twilio.
Happy development!
2 Responses