How to Integrate Microsoft Dynamics 365 Business Central with WhatsApp Using Twilio
Business Central, Twilio e WhatsApp

How to Integrate Microsoft Dynamics 365 Business Central with WhatsApp Using Twilio

This guide explains how to achieve this integration using Twilio's WhatsApp API.


Step 1: Register and Set Up Twilio

  1. Visit Twilio and create an account.
  2. Access your Twilio Console and locate your Account SID and Auth Token under the "Project Info" section.
  3. In the Twilio Console, navigate to the "Messaging" section and set up a WhatsApp-enabled phone number.


Step 2: Create Business Central Table for Settings

Define a table to store your Twilio credentials and WhatsApp settings.

table 50100 "WhatsApp Setup"
{
    DataClassification = ToBeClassified;
    fields
    {
        field(1; "Account SID"; Text[50]) { }
        field(2; "Auth Token"; Text[50]) { }
        field(3; "From Number"; Text[20]) { }
        field(4; "To Number"; Text[20]) { }
        field(5; "Message Body"; Text[250]) { }
    }
    keys
    {
        key(PK; "Account SID") { Clustered = true; }
    }
}
        


Step 3: Create Integration Page

Design a page for configuring WhatsApp settings and sending test messages.

page 50100 "WhatsApp Integration Setup"
{
    PageType = Card;
    ApplicationArea = All;
    SourceTable = "WhatsApp Setup";

    layout
    {
        area(content)
        {
            group("API Credentials")
            {
                field("Account SID"; "Account SID") { ApplicationArea = All; }
                field("Auth Token"; "Auth Token") { ApplicationArea = All; }
            }
            group("WhatsApp Numbers")
            {
                field("From Number"; "From Number") { ApplicationArea = All; }
                field("To Number"; "To Number") { ApplicationArea = All; }
            }
            group("Message Settings")
            {
                field("Message Body"; "Message Body") { ApplicationArea = All; }
            }
        }
    }

    actions
    {
        area(processing)
        {
            action("Save Credentials")
            {
                ApplicationArea = All;
                trigger OnAction()
                begin
                    // Logic to save credentials
                    Message('Credentials saved successfully.');
                end;
            }
            action("Send Test Message")
            {
                ApplicationArea = All;
                trigger OnAction()
                var
                    HttpClient: HttpClient;
                    HttpRequestMessage: HttpRequestMessage;
                    HttpResponseMessage: HttpResponseMessage;
                    Content: HttpContent;
                    JsonObject: JsonObject;
                begin
                    "WhatsApp Setup".Get();

                    // Prepare request
                    HttpRequestMessage.SetRequestUri('https://api.twilio.com/2010-04-01/Accounts/' + "Account SID" + '/Messages.json');
                    HttpRequestMessage.Method := 'POST';
                    HttpRequestMessage.GetHeaders().Add('Authorization', 'Basic ' + Convert.ToBase64String("Account SID" + ':' + "Auth Token"));
                    JsonObject.Add('From', 'whatsapp:' + "From Number");
                    JsonObject.Add('To', 'whatsapp:' + "To Number");
                    JsonObject.Add('Body', "Message Body");
                    Content.WriteFrom(JsonObject.ToString());
                    Content.GetHeaders().Add('Content-Type', 'application/json');
                    HttpRequestMessage.Content := Content;

                    // Send request
                    if HttpClient.Send(HttpRequestMessage, HttpResponseMessage) then begin
                        HttpResponseMessage.Content().ReadAs(JsonObject);
                        if HttpResponseMessage.IsSuccessStatusCode() then
                            Message('Message sent successfully.')
                        else
                            Message('Error: ' + JsonObject.GetValue('message').AsText());
                    end else
                        Message('Failed to send request.');
                end;
            }
        }
    }
}

        


Step 4: Deploy and Test

  • Publish and install the extension in your Business Central environment.
  • Open the "WhatsApp Integration Setup" page and enter your Twilio credentials and phone numbers.
  • Use the "Send Test Message" button to verify the integration.

Good Fun!!!!

For detailed guidance and additional features, refer to the Twilio documentation and Business Central development resources.

Georgian Radu

Empowering Digital Transformation?Dynamics 365?Change Management?Program Management

8 个月

Matteo Fiocco what is the business application compared to the integration with Teams?

回复
Fernando Soto

Dynamics NAV/ BC Senior Consultant en Daira Professional Services

8 个月

I try and don't works.

Integrate Microsoft Dynamics 365 Business Central with WhatsApp using Twilio for seamless communication.

要查看或添加评论,请登录

Matteo Fiocco的更多文章

社区洞察

其他会员也浏览了