One of my friend was having requirement to send WhatsApp from Lightning community portal. I have suggested to use Twilio API for sending message. This blog will provide complete detail about sending WhatsApp message from lightning Application, which i have suggested to my friend.
Steps for Twilio API integration in Salesforce:
- Signup in TwilioÂ
- Create Sandbox environment
- Create Apex for consuming API
- Create Lightning component
Signup in TwilioÂ
Signup in Twilio for integrating it with Salesforce application.
It will send one verification email to verify email. Verify email to continue. This will open below screen to verify phone number.
Verify mobile/phone number to process further. After verification and confirming some information , this popup will show. Confirm this also as it is for developers for testing Whatsapp messaging.
Create Sandbox environment
Developer can test WhatsApp messaging using Sandbox environment. Setting Sandbox environment is 4 steps process and it will start after above popup is confirmed.
First Step is to connect sandbox environment with your whatsapp number. So send message ‘join <connection code>’ to +14155238886. Connection code will be generated automatically and it will be visible on page like in below page with bold character. For my test environment it is difficult-now.
Once you send proper message, it will link your WhatsApp number to sandbox environment.
In next step, send any message form sandbox environment to your WhatsApp number. You can select any option to test like ‘Appointment Reminder’, ‘Order Notifications’ and ‘Verification Codes’.
Below screen show, how message will be delivered to your number.
After confirming that message is delivered to your WhatsApp number, you can process for next step. Next step is to send message from your WhatsApp number to verify two way communication.
After successful communication, it will show sandbox environment detail. For testing we can only use 3 highlighted message format.
Now our sandbox environment is setup. Let us proceed with using twilio API in apex.
Create Apex for consuming API
Integrate Twilio API to send WhatsApp message using Apex. Apex need your Twilio account SID and secret token. You can get these information here. If it is not available be default then click Show API Credentials in right side of the page.
Add Twilio API Url in Remote Site Setting.
Sending message will require three parameter.
- From : This is sender WhatsApp number. For sandbox environment this number will be  +14155238886.
- To   : This is receiver WhatsApp number. Use above WhatsApp number which you have configured in sandbox environment.
- Body : This is message which will be sent. For testing use format which is mentioned in sandbox page.
Create Lightning component
Create lightning component to send message using above apex class.
Provide receiver WhatsApp number and message in valid format. It will send WhatsApp message to provided number.
If you need to send WhatsApp message to other mobile number then it should be verified first. You can verify mobile no here.
Reference :Â https://www.twilio.com/docs/sms/whatsapp
Video:
31 comments
Hi Dhanik, in apex controller, name of method should be SendMessage i guess. can you please confirm
Thanks
Yes Sakshi. Thank you for informing. I have updated it.
how to receive message/incoming message from whatsapp
Hello Sumit,
Twilio has both functionality to receive and send SMS. Refer link
Thank You,
Dhanik
Hi Dhannik,
Your post is very nice but i need some help. i want show incoming message from user devive in saleforce component so how to receive message/incoming message from whatsapp
Hello Sumit,
Twilio has both functionality to receive and send SMS. Refer link
Thank You,
Dhanik
Hi Dhanik,
Please, Can you help me in Received messages storing in database of salesforce.
Thank you
Hello Basavaraj,
You can use any Custome Object to store messages. Still, if you need to discuss then ping me on Linkedin or telegram.
Thank You,
Dhanik
does this requires twilio for salesforce appexchange product installed to access twilio outbound api call or its public api. Thanks for the post.
Hello Kumar,
It is accessing twilio api. No need to install App-exchange product.
Thank You,
Dhanik
Hello,
I want to give it a shot, will it work with a developer org?
Yes, It will work for developer org.
Thank You,
Dhanik
Hi Dhanik ,
I am getting below error when I am trying to send SMS via apex using above code .
{“code”: 21211, “message”: “The ‘To’ number Wtatsapp:+91XXXXXXXXXX is not a valid phone number.”, “more_info”: “https://www.twilio.com/docs/errors/21211”, “status”: 400}
Can you confirm if I am missing anything?
A quick response will be highly appreciated.
Hello Manav,
You have to verify the mobile number where you are sending SMS. Let me know if this still not work. We can connect and resolve your issue.
Thank You,
Dhanik
Thanks Dhanik for guiding me, its working fine now
Hello Dhanik,
I am trying to get all messages from Twilio to salesforce. So i used ‘GET’ method for it, but in response Twilio only send the latest single message. Can you please help me out?
Hello Harshal,
Please check https://www.twilio.com/docs/sms/api/message-resource#read-multiple-message-resources for getting all messages.
Thank you,
Dhanik
Hi Dhanik,
How to send multimedia messages
Hello Ifthikar,
Checkout this page from Twilio for MMS support.
Thank You,
Dhanik
[…] class is using WhatsAppMessage service class. Refer our existing post for this […]
I am getting an error
18:57:04:772 USER_DEBUG [29]|DEBUG|{“code”: 63007, “message”: “Twilio could not find a Channel with the specified From address”, “more_info”: “https://www.twilio.com/docs/errors/63007”, “status”: 400}
18:57:04:773 USER_DEBUG [35]|DEBUG|Twilio errorTwilio could not find a Channel with the specified From address
And Can you Please tell me In Status Callback URL Which URL I Should used
Hey Akash,
Please check this post
Thank You,
Dhanik
Hi ,
Hi Dhanik ,
I am getting below error and also verified my number as well.
{“code”: 63007, “message”: “Twilio could not find a Channel with the specified From address”, “more_info”: “https://www.twilio.com/docs/errors/63007”, “status”: 400}
Can you please help me out? Thanks!
Hello Pramod,
Please check this blog https://support.twilio.com/hc/en-us/articles/360009017394-Troubleshooting-the-Twilio-Sandbox-for-WhatsApp#:~:text=Error%3A%20Twilio%20could%20not%20find,message%20is%20not%20formatted%20correctly.
Thank You,
Dhanik
Hi Dhanik,
can you please share the get message code and aura modification for this
Hello Kannan,
You can include this LWC in your aura as well. Any specific reason, only to create in Aura component?
Thank You,
Dhanik
Hi Dhanik,
To connect sandbox with my number, it should show join . In my case, it’s only showing join not sandbox name to send a message. I tried sending join but there is no sandbox name, do you have any idea why my sandbox name not coming.
Thanks
Hello Ankit,
Try to create a new user and connect. I hope, it will resolve. Otherwise, we can connect to resolve your issue.
Thank You,
Dhanik
[…] File in S3 using Named Credential OCR App using Salesforce Einstein OCR API Integrate Salesforce with WhatsApp using Twilio API rest and soap apirest api vs soap […]
Please help me to send a WhatsApp message to other mobile numbers or how to add mobile numbers in Twilio.
Hello Samir,
What issue you are facing. Please let me know so that i can help you. You can also refer video which is covering your issue as well.
Thank You,
Dhanik