Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
Twilio API for WhatsApp
Twilio API for WhatsApp is now available in early beta access, which allows developers to build prototypes in a sandbox environment. Twilio enables you to use the WhatsApp API immediately using a shared phone number, without waiting for a dedicated number to be approved by WhatsApp. To enable WhatsApp API in production, You need to request access in the limited availability program. Lets go ahead and send your first message using WhatsApp API.
First you need to signup for a Twilio account or login with your existing account and goto the Twilio whatsapp sandbox console and follow the on-screen instructions and opt-in to receive WhatsApp messages to enable the sandbox.
Twilio sandbox allows you to test WhatsApp by sending and receiving messages from a shared Twilio number to opted-in users.
Send WhatsApp message using Python
Get your Account SID and Auth Token from Twilio Console to authenticate your REST APIÂ calls.
Setup your python development environment
Though there is a official python sdk for twilio available but I am using requests library in this tutorial because i love it.
Send Messages / Notifications using WhatsApp Message Templates
To send outbound messages using WhatsApp, you must only use a pre-approved template and you can create custom templates when you enable WhatsApp on your Twilio number.
Currently, there are 3 pre-approved templates available.
- Appointment reminder template
- Order notification template
- Verification code template
Send Appointment reminders
Appointment reminder template is useful for sending appointment reminders. This template looks like this below,
Your appointment is coming up on {{August 20th}} at {{6:00PM}}
Code
import requests
TWILIO_SID = âACXXXXXXXXXXXXXXâTWILIO_AUTHTOKEN = â62f1efa7e0e9471cfdbfXXXXXXXXXâTWILIO_MESSAGE_ENDPOINT = âhttps://api.twilio.com/2010-04-01/Accounts/{TWILIO_SID}/Messages.json".format(TWILIO_SID=TWILIO_SID)
TWILIO_NUMBER = âwhatsapp:+14155238886â
def send_whatsapp_message(to, message):
message_data = { "To": to, "From": TWILIO_NUMBER, "Body": message, } response = requests.post(TWILIO_MESSAGE_ENDPOINT, data=message_data, auth=(TWILIO_SID, TWILIO_AUTHTOKEN)) response_json = response.json() return response_json
to_number = âwhatsapp:+919566XXXXXXâ appointment_msg = âââYour appointment is coming up on August 20th at 6:00PMâââ
msg = send_whatsapp_message(to_number, appointment_msg)
print(msg[âsidâ]) # SM5xxxafa561e34b1e84c9d22351ae08a0print(msg[âstatusâ]) # queued
Send Order notifications
Order notifications template is useful for sending order details. This template looks like this below,
Your Yummy Cupcakes Company order of {{1 dozen frosted cupcakes}} has shipped and should be delivered on {{July 10, 2019}}. Details : http://www.yummycupcakes.com/
Code
to_number = "whatsapp:+919566XXXXXX"
order_notif_msg = """Your Yummy Cupcakes Company order of 1 dozen frosted cupcakes has shipped and should be delivered on July 10, 2019. Details : http://www.yummycupcakes.com/"""
msg = send_whatsapp_message(to_number, order_notif_msg)print(msg['sid'])
Verification codes
Verification code template is useful for sending one time passwords and mobile number verification codes. This template looks like this below,
Your Twilio code is {{1238432}}
Code
to_number = "whatsapp:+919566XXXXXX" otp_msg = """Your Twilio code is 1238432"""
msg = send_whatsapp_message(to_number, otp_msg)print(msg['sid'])
Webhooks for WhatsApp messages
You can setup webhooks to receive inbound WhatsApp messages to build a conversational agent experience called chatbots.
You can configure webhooks for two events. One is sent when a new message comes in to your twilio whatsapp number and other is to check the delivery status updates of the messages sent using the Twilio API. For more info: https://www.twilio.com/docs/sms/whatsapp/api
Originally posted on idiotinside.com
How to send WhatsApp message using Python and Twilio API? was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.
Disclaimer
The views and opinions expressed in this article are solely those of the authors and do not reflect the views of Bitcoin Insider. Every investment and trading move involves risk - this is especially true for cryptocurrencies given their volatility. We strongly advise our readers to conduct their own research when making a decision.