This Twilio Functions service implements the Frontline callbacks and Conversations callbacks to faciliate inbound and outbound SMS and WhatsApp support for Twilio Frontline based on Contact ownership in Salesforce.
The Functions implement the following Frontline and Conversations callbacks:
- crm.protected.js - Implements the Frontline CRM Callback to:
- Pull a Customer List per user from Contact records from Salesforce based on the Salesforce Contact Owner matching the Frontline user's identity
- Pull a Customer's details by Customer ID (Salesforce Contact Record ID)
- Execute a SOSL search against the Salesforce Contact list by Contact Owner
- outgoing-conversation.protected.js - Implements the Frontline Outgoing Conversations Callback to return the outbound proxy addresses for SMS and WhatsApp channels
- inbound-routing.protected.js - Implements the Frontline Inbound Routing Callback to add the Frontline worker to the Conversation
- templates.protected.js - Implements the Frontline Templates Callback to return WhatsApp-approved templates for outbound WhatsApp conversations
- inbound-conversation.protected.js - Implements the Frontline Custom Routing Callback:
- Handles onConversationAdd webhook from Conversations to set the conversation friendly name to the Contact name from Salesforce
- Handles onParticipantAdded webhook from Conversations to update the customer participant's
customer_id
anddisplay_name
The following screenshots show the Frontline worker screens for SMS as well as Frontline worker WhatsApp template selection and customer receipt of the WhatsApp template:
To deploy these Frontline Functions, you will need:
- An active Twilio account with Frontline and approved WhatsApp Senders and Templates
- SSO configured for your Frontline instance. This ensures that the Salesforce and Frontline accounts can be linked securely using a common SSO identity provider.
- A new, unique Sync service SID within the Frontline Twilio account to cache Salesforce access tokens
- npm version 5.0.0 or later installed (type
npm -v
in your terminal to check) - Node.js version 12 or later installed (type
node -v
in your terminal to check) - Twilio CLI along with the Serverless Plugin. Run the following commands to install them:
# Install the Twilio CLI npm install twilio-cli -g # Install the Serverless Plugin twilio plugins:install @twilio-labs/plugin-serverless
- Salesforce Developer or Sandbox Org. with System Administrator access
Create a Salesforce Connected App under Salesforce Setup > App Manager > New Connected App with at least Access and manage your data (api)
and Perform requests on your behalf at any time (refresh_token, offline_access)
OAuth scopes. The Callback URL can be https://login.salesforce.com/services/oauth2/success
and is only used during the initial setup.
Select "Use digital signatures" and follow Salesforce's instructions to Create a Private Key and Self-Signed Digital Certificate
- Upload the resulting
server.crt
file to Salesforce by selecting "Choose File" next to "Use digital signatures" - Place your private key
server.key
in a cloned version of this respository at the pathassets/server.private.key
- Authorize your Salesforce Org. Using the JWT Bearer Flow
- Clone this repo:
git clone https://github.com/randyjohnston/function-frontline-salesforce
- Install the dependencies
# Install the dependencies of the Twilio Functions
npm install
- Copy the
.env.example
file
cp .env.example .env
- Edit
.env
:
ACCOUNT_SID=Found at https://www.twilio.com/console
AUTH_TOKEN=Found at https://www.twilio.com/console
SF_CONSUMER_KEY=Your Salesforce Connected App OAuth Client ID
SF_USERNAME=The username to make JWT bearer token Salesforce API calls for inbound callbacks lacking user context to the webhook request
SFDC_INSTANCE_URL=Your Salesforce instance URL, e.g. https://abc-dev-ed.my.salesforce.com
WHATSAPP_NUMBER=WhatsApp number for outbound conversations proxy address
SMS_NUMBER=SMS number for outbound conversations proxy address
DEFAULT_WORKER=The default Frontline worker for inbound conversation routing if no matching Contact Owner is found in Salesforce
SYNC_SERVICE_SID=The Sync Service SID used to store Salesforce access tokens
In order to develop locally, you can use the twilio-run
CLI by running (from the root directory):
# Start Twilio Functions
twilio serverless:start
This will automatically start up the Twilio Serverless local development server. Your app will run on http://localhost:3000
.
When you are ready, deploy your Twilio Functions:
twilio serverless:deploy