This content originally appeared on Twilio Blog and was authored by Miguel Grinberg
The WhatsApp Business API from Twilio is a powerful, yet easy to use service that allows you to communicate with your users on the popular messaging app.
In this article, you’ll learn how to send an image to a user through WhatsApp using a cURL, which would make this functionality accessible from shell scripts.
Prerequisites
To follow this tutorial you need the following items:
- A Twilio account. If you are new to Twilio click here to create a free account now and receive $10 credit when you upgrade to a paid account. You can review the features and limitations of a free Twilio account.
- A copy of cURL installed on your computer. If you are using a Unix or Mac OS computer you likely have it already installed. For Windows, you can download an installer.
- A smartphone with an active WhatsApp account, to test the project.
The Twilio WhatsApp sandbox
Twilio provides a WhatsApp sandbox, where you can easily develop and test your application. Once your application is complete you can request production access for your Twilio phone number, which requires approval by WhatsApp.
In this section you are going to connect your smartphone to the sandbox. From your Twilio Console, select Messaging, then select Try it Out on the sidebar. Open the WhatsApp section. The WhatsApp sandbox page will show you the sandbox number assigned to your account, and a join code.
To enable the WhatsApp sandbox for your smartphone, send a WhatsApp message with the given code to the number assigned to your account. The code is going to begin with the word "join", followed by a randomly generated two-word phrase.
Shortly after you send the message you will receive a reply from Twilio indicating that your mobile number is connected to the sandbox and can start sending and receiving messages.
If you intend to test your application with additional smartphones, then you must repeat the sandbox registration process with each of them.
Authenticate against Twilio services
Before you can communicate with the Twilio API you need to safely store some important credentials that will be used to authenticate.
You can obtain the TWILIO_ACCOUNT_SID
and TWILIO_AUTH_TOKEN
credentials that apply to your Twilio account from the Twilio Console:
Once you have located the Account SID and Auth Token, you can store them in environment variables by typing the following commands into your terminal:
export TWILIO_ACCOUNT_SID=<YOUR_TWILIO_ACCOUNT_SID>
export TWILIO_AUTH_TOKEN=<YOUR_TWILIO_AUTH_TOKEN>
If you are following this tutorial on a Windows computer, replace export
with set
in the commands above.
You will also need the phone numbers of the sender and the recipient of the message. Since you are using the WhatsApp sandbox for this tutorial, the sender’s phone number is the number assigned to your sandbox. Store the phone numbers in two environment variables:
export FROM_NUMBER=<TWILIO_SANDBOX_PHONE_NUMBER>
export TO_NUMBER=<YOUR_PHONE_NUMBER>
Make sure that the phone numbers are given in E.164 format, which includes the plus sign prefix and the country code. Also remember to replace export
with set
if you are following the tutorial on a Windows computer.
Send a picture message on WhatsApp with cURL
The last step before you are ready to send the message is to decide what image you will be sending. The image needs to be hosted on the Internet, such that a URL for it can be given to Twilio. If you can’t think of an image that you’d like to use, you are welcome to use this one. Set the URL to your image in a fifth environment variable:
export MEDIA_URL=https://raw.githubusercontent.com/dianephan/flask_upload_photos/main/UPLOADS/DRAW_THE_OWL_MEME.png
Feel free to replace the above URL with your own.
With the five environment variables set, you are finally ready to send a picture message on WhatsApp. If you are following this tutorial on a Unix or MacOS computer, here is how to do it:
curl -X POST -d "Body=Check out this funny picture" --data-urlencode "From=whatsapp:$FROM_NUMBER" --data-urlencode "To=whatsapp:$TO_NUMBER" -d "MediaUrl=$MEDIA_URL" "https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages" -u "$TWILIO_ACCOUNT_SID:$TWILIO_AUTH_TOKEN"
For Windows, the syntax to insert variables in the command is slightly different, so use this version instead:
curl -X POST -d "Body=Check out this funny picture" --data-urlencode "From=whatsapp:%FROM_NUMBER%" --data-urlencode "To=whatsapp:%TO_NUMBER%" -d "MediaUrl=%MEDIA_URL%" "https://api.twilio.com/2010-04-01/Accounts/%TWILIO_ACCOUNT_SID%/Messages" -u "%TWILIO_ACCOUNT_SID%:%TWILIO_AUTH_TOKEN%"
In just a moment, you will see a ping from WhatsApp on your phone, sent directly from your computer!
Next steps
Congratulations on writing a one liner to send a quick image to your WhatsApp enabled device! This was so wickedly quick, can you even believe it was possible?
There's so much more you can do with Twilio. Here are some other neat WhatsApp media projects that you can check out:
- Build an image recognition app on WhatsApp using Twilio WhatsApp API, Clarifai API, Python, and Flask
- Learn Data Visualization with WhatsApp, Google Maps, Python, Twilio, and Clarifai
- Send SVG Images by SMS and WhatsApp with Python and CairoSVG
Let me know about the projects you're building with WhatsApp!
Miguel Grinberg is a Python Developer for Technical Content at Twilio. Reach out to him at mgrinberg [at] twilio [dot] com if you have a cool Python project you’d like to share on this blog!
This content originally appeared on Twilio Blog and was authored by Miguel Grinberg
Miguel Grinberg | Sciencx (2021-04-21T12:50:57+00:00) How to Send a Picture on WhatsApp from the Command Line Using Twilio and cURL. Retrieved from https://www.scien.cx/2021/04/21/how-to-send-a-picture-on-whatsapp-from-the-command-line-using-twilio-and-curl/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.