How to send WhatsApp message using Python and Twilio API?

Written by sureshdsk | Published 2018/08/15
Tech Story Tags: messaging | whatsapp | python | python-and-twilio-api | twilio-api

TLDRvia the TL;DR App

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.

  1. Appointment reminder template
  2. Order notification template
  3. 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


Written by sureshdsk | Software Architect | Pythonista 🔥
Published by HackerNoon on 2018/08/15