/sf-twilio-sms-function

Send SMS via Twilio using Salesforce Function

Primary LanguageApexCreative Commons Zero v1.0 UniversalCC0-1.0

Send SMS via Twilio using Salesforce Function

About

This project is an integration between a Salesforce Org, Salesforce Functions and Twilio.

The goal of the integration is to Send SMS from Salesforce to given phone number using twilio.

Installation

Prerequisites

Salesforce Resources

  1. Sign up for a Salesforce Functions trial org.
  2. Enable Dev Hub in your org.
  3. Install the Salesforce CLI.
  4. Authorize your Dev Hub in the Salesforce CLI.
  5. Get Started | Salesforce Functions

Twilio Resources

  1. Sign up for your free Twilio trial.
  2. Once you finish signup, you should see your Console Dashboard. This is your home for finding your Twilio credentials. Get your Account SID and Auth Token from this page. You will need those values for the Integration.
  3. Verify your personal phone number: When you signed up for your trial account, you verified your personal phone number. You can see your list of verified phone numbers on the Verified Caller IDs page. Here you must verify any non-Twilio phone numbers you wish to send SMS messages or MMS messages, or place phone calls to while in trial mode.
  4. Get your first Twilio phone number: After signing up for your trial account, navigate to the Phone Numbers page in your console. Click Buy a Number to purchase your first Twilio number.
  5. The Twilio Node Helper Library

Deploy and configure the Salesforce Function

You execute the function by either deploying to a compute environement or run locally.

Deploy to compute environment

Follow these steps to deploy your function to a compute environment:

  1. Log in to Salesforce Functions (you may have to repeat this command later as this will eventually time out)

    sf login functions
  2. Create a compute environment:

    sf env create compute -o sf-twilio-function -a stenv
  3. Deploy the Salesforce Function:

    cd functions/sftwiliosms
    sf deploy functions -o sf-twilio-function
  4. Configure the Salesforce Function with the following command (see environment variables reference):

    sf env var set TWILIO_ACCOUNT_SID=XXXXXXXXXX -e stenv
    sf env var set TWILIO_AUTH_TOKEN=XXXXXXXXXX -e stenv
    sf env var set FROM_NUMBER=XXXXXXXXXX -e stenv
  5. Open Developer Console and execute below code using Execute Anonymous Window to send SMS:

    // Replace [VERIFIED_NUMBER] with non-Twilio phone number you wish to send SMS to.
    SFTwilioFunctionDemo.sendSMS('[VERIFIED_NUMBER]', 'This sms was sent from twili using Salesforce Function.');

Run locally

Follow these steps to test your function locally:

  1. Create a .env file in the functions/sftwiliosms directory. Use the following template and make sure to replace values accordingly:

    TWILIO_ACCOUNT_SID=XXXXXXXXXX
    TWILIO_AUTH_TOKEN=XXXXXXXXXX
    FROM_NUMBER=XXXXXXXXXX
  2. Prepare the JSON payload. You can pass the payload inline or create a payload.json file in your function directory with a verified number and SMS body as:

    {
      "toNumber": "[VERIFIED_NUMBER]",
      "smsBody": "This SMS was sent via Salesforce Function."
    }
  3. Run these commands to start the function locally:

    cd functions/sftwiliosms
    sf run function start
  4. Navigate to your project root directory and invoke the function as:

    sf run function -l http://localhost:8080 -p '@functions/sftwiliosms/payload.json'

Environment variables reference

Variable Name Description
TWILIO_ACCOUNT_SID Your twilio account's Account SID.
TWILIO_AUTH_TOKEN Your twilio account's Auth Token .
FROM_NUMBER Your Twilio phone number

Troubleshooting

  1. While creating the compute environment if you see below error, Check your sfdx-project.json at the root level. The name of the project must not have -. Please change it to underscore and it should work.

    Creating compute environment for org ID 00DXXXXXXXXXXXXXXX... failed
    Error: Request failed with status code 422
    
  2. Monitor Salesforce Function's logs by running:

    sf env log tail -e stenv
  3. Monitor Salesforce logs by running:

    sfdx force:apex:log:tail -c

Best Practice

Release the Compute Environments: If you no longer need a compute environment, or you wish to disconnect a connected org, use the sf env delete command with the compute environment alias to remove the compute environment and let someone else use these resources for practice :) :

sf env delete -e stenv