WhatsApp integration!!
Most conversations will end up taking place on WhatsApp, be it a simple greeting, gossip, news, or networking. The very first thing that comes up in our mind is WhatsApp. Almost every industry leverages it or heading towards using it in growing its business and improves its reach to its customers.
How about ServiceNow to leverage WhatsApp?
This #know20 another interesting topic came up, Welcome to another article, ServiceNow integration with "WhatsApp".
Whenever do you want to perform integration what is the first thing that comes in your mind?
Yes, we need to know whether API is Open-source or Paid. If API is open-source then will check for its developer account, documentation, and sample message but what if API is paid then the first thing we ask Is there any tool or interface which can be leveraged to integrate?
does have APIs but those are business APIs, which requires a paid subscription.
Now, we know WhatsApp APIs are not open source then must require some interface or tool, using that we can integrate ServiceNow to access the WhatsApp APIs.
One of the interfaces is Twilio, it also provides the interface to integrate with WhatsApp APIs, and there are other features that it offers like make calls, send SMS, etc so on. Now we have to integrate ServiceNow with Twilio to enable the WhatsApp integration*.*
That's great !! but what next?
- Do I need to perform double integration?
- Whether Twilio APIs are paid or open-source.
- Does it have proper documentation for that?
- In ServiceNow, what are prerequisites to set up this integration?
- Is there any additional plugin or spoke required?
Those might be the questions that come in mind. Right?
Let's discuss the prerequisites for Twilio and ServiceNow one by one.
- Twilio prerequisites:-
- You need a Twilio developer account.
- Goto communication cloud selects Programmable SMS.
****
(a) Selecting Programmable SMS
- Select WhatsApp beta in Programmable SMS.
(b) Use code to connect WhatsApp
(C) Sending code to+14155238886
- Sandbox is now ready but some important things are required from Twilio, which are as follows:
- As we have to make POST requests, endpoints should be like 'https://api.twilio.com/2010-04-01/Accounts/<account id>/Messages.json'.
- Username: Account ID and Password: Auth Token.
- From(phone number) which Twilio provides.
(d) Endpoints, header, Username and To and From
So far, we have collected the prerequisites from Twilio, but before we jump to setting up it in ServiceNow, we need to understandits flow. How requests trigger from ServiceNow to Twilio and how Twilio triggers WhatsApp message to "To"(dedicated WhatsApp number).
(e) Request ServiceNow to WhatsApp
Instead of using the conventional outbound setup within ServiceNow, this time we will use Flow Designer with IntegrationHub, we simply need to define a flow to automate the process in ServiceNow along with action which is responsible for Rest Call to Twilio.
(f) Architectural view-flow and IntegartionHub
- Two main components at ServiceNow is Flow and IntegrationHub, by leveraging these two stacks we communicate from ServiceNow to WhatsApp.
- Flows: It enables business process analysts to automate approvals, tasks, etc without using code/script. There are 3 components to design flow Trigger, Action, and Flow-Logic.
- Trigger:- How do you want to initiate the flow. The trigger can be of schedule type or on the basis of record being created/updated.
- Action:- It allows automating the process, basically, these are the sequence of steps the flow follows. Action can be used to create the task, ask for approvals, or automate flow with 3rd party applications, etc and so on.
- Flow-Logic:- How flow leverages conditions to move forward. These can be conditional logic like If-Else, Do it in parallel, For-Each, etc.
- IntegrationHub: It enables the execution of third-party APIs as a part of a flow when a specific event occurs in
ServiceNow. It is next-gen. Integration and Orchestration solution which extends digital workflow to any external platform. - The integration that is built using hub is called Spokes.
- Spokes are easy to configure and enable you to quickly add powerful actions without the need to write a script.
- You can leverage XML parser, payload builder, PowerShell, and SSH commands without the need to write script/code.
(g) Functions within Action.
- Design Action in ServiceNow to get connected to Twilio, below are the steps:-
- Goto Designer in Flow-Designer and selects New and choose 'Action' tab.
(h) Action in flow-designer.
- Once you fill the name, category, and description, then Action designer window comes up.
(i) Action-Designer pane.
- Now you are in action-designer, let's define inputs, WhatsApp integration requires 3 inputs (i) From (Official WhatsApp no.) (ii) To (User's WhatsApp no.) and (iii) Body (Message body).
- In this scenario From =+14155238886, Twilio has its own format for defining mobile no. it should be like <+country code>.
- To also should be in the same format.<+country code>, eg. for India country code is +91 and mobile no. is 9XXX2113XX, then To = +919XXX2113XX.
- Bodycontains the message that you want to send to "To" from "From".
(j) Inputs in Action-Designer.
- Type for 'From' and 'To' should be Phone Number or Phone Number(E164).
- Apart from 'From', 'To' and 'Body', let's define another 4 input, which has to use as a prefix when you will make REST call to Twilio. Twilio accepts its own defined format and that should be in encoded format.
- In this step, you will use the Script step to parse the To and From mobile number to remove any special characters because Twilio API accepts the mobile number without special characters, eg. From +1(408) 555-0004 to +14085550004.
- Along with parsing, we will encode the prefix, so that it can be leveraged to make a REST call to Twilio with correct format.
(k) script-step to remove a special character.
As of now we remove the special character from numbers and encode the prefix. For the REST step to be executed correctly, we must need connection and credentials. Don't scratch your head on credential and connection.
- Connections encompass Endpoint URL, credentials, domain, and alias.
(l) Connection and Credential alias.
- Credential holds Authentication Type, WhatsApp integration uses Basic type so, for it, the username is <Account ID> and password is <Auth Token>.
Now, we have configured the connection and credentials, so let's move back to Action and define the REST step for this integration. Again in step panel, select the + and choose the REST in the action window.
In the REST step, fill connection details, request details, and r*equest conten*t, etc.
- Connection details encompass of connection, connection alias, and base URL.
(m) REST step in action designer.
Note:- Base URL will populate automatically if you choose connection as 'Use Connection Alias' and connection alias as specific to your integration.
- Request details contain build requests, resource paths, and so on.
- Request content encompasses request-type and inputs on the basis of request-type.WhatsApp requires "Form URL-encoded" as a request-type, and its inputs are From, To, and Body that we have defined in the above step.
(n) Request content in the REST step.
- Let's quickly recap what has been done so far, we defined inputs, the script has been written to remove special character from phone nos. and convert "whatsapp:+" to whatsapp%3A%2B which is uri-encoded format.
Now, we have configured most of the steps but What do you think thats it? Is integration completed?
No, Integration is incomplete without "Error - Handling" capability. Error handling makes integration robust as it helps to understand the error on basis of status code and error message,so that you can build fault-tolerant actions that can be recovered through Flow Logic or through analyst interventions. Include error handling to allow flows to fail gracefully and provide valuable troubleshooting details when errors are encountered.
- Create another script step that can be leveraged to handle the errors, and within script steps you can invoke resusable classess, what we call Script-Includes.
(o) Error-handling step.
All the steps is completed, the response that we receive from Twilio, it has to be available to flow and to do this we need to to define Action outputs as last step in this integration so that we can leverage the response to design conditional flow or can perform further configuration.
- Action Outputs:- It returns response or data from action to flow-designer, script-steps you define are only available to action itsself and to leverage it in flow-designer Action-Outputs are required.
(p) Action-outputs.
Custom spoke or action is completed,Now, test your action and validate the results.To validate the results flow-designer execution tab is best as it encompasses information about triggers,actions,outputs wether your step gets passed or failed.
- Test requires 3 inputs, what we have defined in initial step those are From, To and Body.
(q) Test Action.
WhatsApp integration with ServiceNow is done, it totally up to you or on to business requirement How do you want to leverage this custom spoke or custom action?
I have designed the whole application which is leveraging this custom spoke and on creation of incident I am sending details to Caller of the incident. If you want to leverage that application and get your hands dirty you can fork in your Git from https://github.com/aatrey882/ServiceNow-Twillio-Whatsapp.
As this is custom spoke you should have Git account and after then you can fork from above repository. First time you need to import it manually in your instance.
The application I created has following components :-
- WhasApp-Twilio Flow - Flow that is being used to automate the process.
- WhatsApp-Twillio - Action that helps to get connected to WhatsApp API.
Other customization are in incident form, there I have created section called WhatsApp Section. You dont need to create that section, once, you import the application it gets installed automatically.
(r) Form section on incident.
In application you will find two tables (i) WhatsApp Configuration and (ii) WhatsApp Transaction logs, before you start building over those just go through from their schema and learn about How they are connected to each other? and How those can be leveraged in other tables to enable this Integration?
Top takeaways :-
- Action-designer allows you to create custom spoke through which you can make it dynamic.
- Leverage flow is better way to automate the process without need to write code/script.
Cons of Twilio API :-
- To setup inbound you must have subscription for their upgraded version.
- No dedicated inbound for WhatsApp in twilio,either you have to use TwiMl or Conversation API.
- If you want to customize the WhatsApp then you should have approval form WhatsApp business.
I hope this integration helps you to know How to leverage WhatsApp in ServiceNow.I believe you will build over this application and design some cool offering.
Mark it helpful ifyou like this article and find it informative and also subscribe to it.
Happy Learning !! Stay Safe!!
Regards
Aashish Atrey
Labels:
https://www.servicenow.com/community/developer-articles/servicenow-whatsapp-integration/ta-p/2324535
