Latest news about Bitcoin and all cryptocurrencies. Your daily crypto news habit.
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:
mkdir whatsapp-messagescd whatsapp-messagesbundle init
Open up the new Gemfile and add the twilio-ruby gem:
# frozen_string_literal: truesource "https://rubygems.org"gem "twilio-ruby"
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.
# app.rbrequire 'twilio-ruby'client = Twilio::REST::Client.new('YOUR_ACCOUNT_SID', 'YOUR_AUTH_TOKEN')message = client.messages.create( to: 'whatsapp:YOUR_WHATSAPP_NUMBER', from: 'whatsapp:WHATSAPP_SANDBOX_NUMBER', body: 'Ahoy from Twilio!')puts "Message sent.\nMessage SID: #{message.sid}."
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:
bundle exec ruby app.rb
You should receive your message in the WhatsApp app.
Note: there are some differences between WhatsApp and regular SMS via the Twilio API. You can only send free-form messages if you have an active session established with a user. Sessions are created when users respond to a template message or the user initiates the conversation by sending a message to your WhatsApp number. A session lasts 24 hours from the last message the user sent to you. If you didnât receive a message, try sending a message to the WhatsApp sandbox first and then running the script again.
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.
# app.rbrequire 'twilio-ruby'client = Twilio::REST::Client.new('YOUR_ACCOUNT_SID', 'YOUR_AUTH_TOKEN')message = client.messages.create( to: 'whatsapp:+447719532208', from: 'whatsapp:+14155238886', body: 'Ahoy from Twilio!', media_url: 'https://tinyurl.com/ahoy-whatsapp')puts "Message sent. Message SID: #{message.sid}."
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.
Originally published at https://www.twilio.com.
Send Images and Files via WhatsApp Using Ruby 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.