Build a WhatsApp bot in 30 minutes 🕐
A few months ago, I'd started making chatbots on Telegram - I'd seen APIs for WhatsApp but they were unoffical and there was a chance for getting your number blocked 📱 ❌
This is a tutorial to help you make a something like this, your own chatbots on WhatsApp - these bots are immediately available to 2 billion users, and there are so many things possible 🎓
I can't wait to see what you make! Now, let's get started 🏃♂️
🔑 Accounts and Keys
First, Sign up for Twilio - it's free and you won't need a credit card 💳
Once you're done verifying your phone number, select Procuts > Programmable SMS and then continue to name your project.
Feel free to skip steps for adding teammates - you won't need that for now.
You must now take note of some authentication keys you'll need for building the WhatsApp bot 👇
The final step - setup your WhatsApp Sandbox here - choose any number, and join your sandbox following instructions on the page.
Aaaaaand you're done with credential setup! Don't worry, that was the toughest part of this tutorial 😛
🚀 Getting Started
So that we don't spend too much time on setup, I've created an environment (with repl.it!) you can use within your browser. Head over here, and wait for a couple of seconds to fork it.
Next, open up
server.js and put in your Account SID and Auth Token, on lines
You can see, this environment already has dependencies installed, and an
express server set up. We still need to give Twilio a URL to send incoming messages to, though 🔗
Let's go back to the WhatsApp Sandbox, and put in a webhook URL for incoming messages.
This URL must be what you see on the preview panel of your repl.it project +
We can now finally read messages that are sent to the bot. Add a simple
console.log() in your webhook handler 👇
When you send a message to your bot, you should be able to see something like this in your repl console 👨💻
Building an echo bot would look something like this, using
twiml to write a message 👇
But, since we're actually trying to build a useful bot - let's use informative APIs!
🌐 Fetching Information
DuckDuckGo has an amazing, free instant answer API. It takes in a query and returns back a summary from WikiPedia and more.
Pretty straight forward, right? 😄
🛠️ Putting it all together
To make our actual bot, all we need to do is get the query from our request - which we can get as
req.body.Body - and use
twmil to send across the data we collected in
You now have a fully functionaing WhatsApp bot! Send anything you want to know about your bot 🤖 and you should see it respond super fast 💬 ⚡
Adding welcome messages and a little formatting is quite simple, look at the final repl to see how I did it 👨💻
🔗 Sharing the bot
For others to use this bot, they'll need to join your sandbox first - and send a message just like you did earlier 👉
You can create links with this text too - For example this link lets you join my bot 👇
14155238886 is my bot's number, while
ultramarine-tapir is the sandbox phrase.
⚡ What's next?
Now that you know how to build a bot on WhatsApp, try sending notifications to yourself, and building more useful tools! Twilio has loads of other mediums to message through too!
All code for my WikiBot is on Github!
I'm a 15 year old maker 👨💻 For more cool things to make and to stay update with my progress, sign up for my newsletter 📧
On twilio it's showing this error
Error - 11200
HTTP retrieval failure
and on repl.it terminal it doesnot shows any info about incoming message.