Codementor Events

Send WhatsApp media/message using Python.

Published Aug 08, 2020Last updated Aug 12, 2020
Send WhatsApp media/message using Python.

You can use Twilio API to Send WhatsApp messages

Though there are many scripts available which are almost free but later on leads to getting blocked by Whatsapp.

We can use Twilio Library for sending and receiving whatsapp messages even for WhatsApp bussiness.

Install Twilio

pip install twilio

SEND MESSAGE USING PYTHON

# Download the helper library from https://www.twilio.com/docs/python/install
from twilio.rest import Client


# Your Account Sid and Auth Token from twilio.com/console
# DANGER! This is insecure. See http://twil.io/secure
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'
client = Client(account_sid, auth_token)

message = client.messages.create(
                              body='Hello there!',
                              from_='whatsapp:+14155238886',
                              to='whatsapp:+15005550006'
                          )

print(message.sid)

SEND MEDIA USING PYTHON AND TWILIO

# Download the helper library from https://www.twilio.com/docs/python/install
from twilio.rest import Client


# Your Account Sid and Auth Token from twilio.com/console
# DANGER! This is insecure. See http://twil.io/secure
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'
client = Client(account_sid, auth_token)

message = client.messages \
    .create(
         media_url=['https://images.unsplash.com/photo-1545093149-618ce3bcf49d?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=668&q=80'],
         from_='whatsapp:+14155238886',
         body="It's taco time!",
         to='whatsapp:+15017122661'
     )

print(message.sid)

where,
Account SID - Used to identify yourself in API requests
Auth Token - Used to authenticate REST API requests

Note: There are many unofficial APIs which leads to blocking of your WhatsApp account on security basis, so make sure whether the API you're using is affiliated by WhatsApp.

Reference: https://www.twilio.com/

Discover and read more posts from Pritish Deshpande
get started
post commentsBe the first to share your opinion
Show more replies