Code with passion
Apr 21, 2019 4 min read

Whatsapp Hackernews Bot


Wait, What??

Yea you heard it right, this time we’re gonna try and build a chatbot for WhatsApp. The challenge is WhatsApp doesn’t provide any integrations API of that kind. So we’re looking for a workaround to build chatbot for API without affecting their user guidelines, we are not gonna use the techniques to build a spam bot. Rule of thump : Only send transactional messages. This is purely research.

Okay, So… What’s the Plan?

The plan is to use DialogFlow to handle the NLP part of the project and Write a Node.js Loopback API server to give back response after executing any custom actions if required.

Sounds Cool, Where to begin?

First of all, we need a way to send and receive the message in between WhatsApp and DialogFlow to initiate our bridge and complete the project. I searched all over the web and found some interesting ways of doing it. We’re gonna try out a library called yowsup2 at first. To use yowsup2 we need a mobile number.

I already had a mobile connection with whatsapp business app account for test purpose, we can use whatsapp business app to send autoreply and greetings etc, So am gonna use the same number for the chatbot demo.

To install yowsup2 run (you should have python installed already): pip install yowsup2 Next step is to register my business app number with yowsup2 client using yowsup2-cli: run:

yowsup-cli registration -r sms --phone 9197XXXXXXX7 --cc 91 --mcc 404 --mnc 95 --env android

Here, argument type

  • -r is for registartion type “sms” or “voice”
  • --phone : your full phone number with country code but without preceeding without preceeding ‘+’ or ‘00’.
  • -cc : country code without preceeding ‘+’ or ‘00’.
  • --mcc and --mncA you can get from this wikipedia page After this you’ll get a json response from whatsapp server and a otp in your mobile phone.

then run:

yowsup-cli registration -R <otp> --phone 9197XXXXXXX7 --cc 91

here, argument types are :

  • -R : recieved otp
  • --phoneA and --cc : same as in the previous command.

You’ll get another json response from whatsapp server. This response contains our whatsapp login details to connect with whatsapp and send/recieve message. The response will be in the following format:

pw: b'wSrxxxxxSDXXXXx='
kind: b'free'
cost: b'0.99'
login: b'917XXXXXXXX7'
expiration: 4444444444.0
type: b'existing'
currency: b'USD'
price_expiration: 1517996977
price: b'$0.99'
status: b'ok'

In this response “login” and “pwd” is all we need to care about for now. In that pwd is the base_64 encoded string and login is your number to be used with chatbot.

ALRIGHT, When can I send a bot Message ;) ??

So desperate to see the bot in action? lets use the demo apps by yowsup-cli

yowsup-cli -l 917xxxxxxxx7:wSxxxxXXXxxxxxg= -e

arguments :

  • -l phone-number:password

This will start an echo WhatsApp bot in your terminal, it will reply back the same message sent to it. Try testing it by sending a message via WhatsApp.

COOL! Let’s build one ourself .

Okay, we can easily take the clone of the echo bot and modify it to build our own bot. But we should define a goal for the bot.

To keep things simple and useful, I decided to build a bot for hacker news because I love that site and its simplicity.

goal 1: If anyone send ‘hn’ to my bot, the bot will send back the news of the day from hackernews.

So what are the things we need for that? obviously, we need a APIA�or content scrapper for HackerNews to do this and for my luck, they already haveA�API support.

The API for hacker news is built upon angolia search API :

The following api gives back the content of i.e the trending technical news from all over the WWW. we’ll call this api when user sends a message with ‘hn’ as content and then get all response and send back as WhatsApp message.

You can refer the code in

I’ll explain the implementation in a seperate post, to see the bot in action send a whatsapp message to +917736472617

Next step is to use Dialogflow in between the bot and API to enable NLP instead of hardcoded message menus.