Send Images and Files via WhatsApp Using Ruby
Time to read: 2 minutes
With the Twilio API for WhatsApp we can send messages to WhatsApp numbers. Those messages can be plain text or include files like images, audio and even PDFs up to 5MB. Let's see how to do so using Ruby.
Things you'll need
If you want to code along with this post, you'll need a few things:
- A Twilio account (sign up for a free account now)
- Ruby and Bundler installed
- The WhatsApp Sandbox Channel installed (learn how to activate your WhatsApp sandbox)
Got all that? Let's get coding then!
Create a new directory for your project and use Bundler to initialise a new Gemfile
:
Open up the new Gemfile
and add the twilio-ruby
gem:
Install the gem by running bundle install
on the command line.
Sending your first WhatsApp message
Create a file in your project directory called app.rb
and open it up. We're going to require the twilio-ruby
gem and initialise an API client with our Account Sid and Auth Token (which you can find on your Twilio console). We will use the API client to send a message from the WhatsApp Sandbox number to our own WhatsApp account.
Make sure to replace all the placeholders with your own details. WhatsApp numbers take the format "whatsapp:E.164-phone-number". An E.164 formatted phone number starts with the country code followed by the rest of the number with no other punctuation, for example "+15551231234".
Run the code with:
You should receive your message in the WhatsApp app.
Sending media messages
We can do better than just a text message. This time let's send an image. The Twilio API for WhatsApp supports sending JPG, JPEG or PNG images.
To send a media message, we need to add one more thing to the call to create a message, a media_url
. The media_url
needs to point to an image file that is available online and the content type header should match the extension.
Add the media_url
to your existing code. You can use the URL below or pick your own image.
Run the code again and you will receive a text message and the image alongside.
What's next?
Now you've sent WhatsApp messages with text and images using Ruby. Next, you could try sending an audio file (in the MP3, OGG, and AMR formats) or a PDF file, as long as they are under 5MB in size.
Want to get a bit more involved with WhatsApp messages and Ruby? Check out how to combine WhatsApp, Spotify and Rails to create a collaborative playlist or how to take images from WhatsApp and use AWS Rekognition to check for celebrities in your pictures.
I'd love to see what you can build sending and receiving media over WhatsApp with the Twilio API. Hit me up in the comments, over email at philnash@twilio.com or on Twitter at @philnash with your projects or questions.
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.