How to get started with Twilio whatsapp in Python?
If you want to build a WhatsApp chatbot quickly that handles message, replies and statuses automatically, then Twilio is the perfect choice.
In this article, let’s take a look at how to build a WhatsApp chatbot with the help of Twilio in Python.
I’ll break down the process step by step and at the end of this article, you’ll find a super helpful video about the same.
So, check it out if you want to see all the steps in a video.
Steps to Access Twilio Whatsapp API
- Create an account with Twilio.com. It’s absolutely FREE.
- After you are done, you’ll see your Account SID & Auth Token right in front of your dashboard.
- A trial account will give you enough Phone credits to test out SMS functionality as well.
- For WhatsApp messages though, there is absolutely NO charge whatsoever.
- But if you want to deploy this app with a real number and multiple message templates then you would have to upgrade the account.
- Minimum fees for upgrading is $20.
- Anyways, click on Messaging > Try it Out > Send a WhatsApp message.
- This will open up Twilio sandbox where you can immediately test sending out messages.
- You’ll also see a helpful message about sending messages from your device.
- You’ll have to send a code from your device to a Twilio generated number.
- When you send this code, Twilio will connect your device with that generated number for a 24-hour session.
- In this session, you can communicate back and forth with Twilio & your device.
- When your session times out, you can send the code again to start another 24-hour session.
- You can connect multiple devices at the same time with the generated number and code.
- Now, on the coding side…first, you’ll have to install Twilio library via pip install Twilio.
- Then, write the following code to get started with Twilio WhatsApp API.
- Replace the to=’whatsapp:+15005550006′, with your actual personal mobile number with your country pin, like to=’whatsapp:+9186026XXXXX’, would be mine from Indian pin followed by my 10 digit mobile number.
- When you run the code above, you should be able to see a successful response from Twilio WhatsApp api.
- In the above code, I am using my environment variables to fetch the Twilio account SID & Auth token, which is a very good practice when you are working in production environment.
- But for practice, you can simply do the following:
- Please do keep in mind to remove this implementation as soon as you are done with your tests/practice & implement environment variables approach to your app.
I hope the above steps will get you started with Twilio WhatsApp api. If you want to create a full-fledged WhatsApp chatbot then you can contact me via telegram. The link is in the author bio below.
Checkout the video below to see the steps in action & if you find them useful then please do subscribe to Code Jana YouTube channel.