A Python API for Whatsapp Web

by Efraim Rodrigues on November 24, 2017

Tagged as: python, whatsapp, javascript.

Documentation Status

Available on GitHub

This article explains the basis of WhaPy a API for Whatsapp Web that allows chatbots to be created. Please refer to the documentation for detailed information on how to use it.

Intro

Whatsapp has been claimed to be one of the most used conversation application in the western world. It seems whatsapp has not made plans for implementing an API (Application Programming Interface) in a near future. An API would allow developers to implement chatbots to provide automated services on it. Although whatsapp doesn’t seem willing to implement an API, whatsapp web seemed to make it possible for an unofficial API.

WhatsApp Web

Whatsapp Web is the whatsapp version for browsers, but it requires the mobile application to be online simultaneously. This version uses ReacJs which is a JavaScript library for building applications’ interfaces.

ReacJs defines a virtual DOM in order to perform updates in the DOM more efficiently. Here’s an explanation of React’s perks. Whatsapp Web’s architecture defines React on top of the architecture to make requests to lower layers. Then, the frontend Store is defined to provided a interface for this.

Frontend Store

The frontend Store defines a set of objects and functions to work with the architecture’s lower layers. Here’s a list of objects defined on the frontend Store:

  • AllStarredMsgs
  • Blocklist
  • Call
  • Chat
  • ChatPreference
  • Cmd
  • Conn
  • Contact
  • ConversionTuple
  • EmojiVariant
  • GroupMetadata
  • Location
  • Msg
  • MsgInfo
  • Mute
  • Presence
  • ProfilePicThumb
  • RecentEmoji
  • ServerProps
  • StarredGifMsgs
  • Status
  • StatusV3
  • StatusV3Privacy
  • Stream
  • Wap

Most of these objects are comprehensible by their names, and their functions are self explanatory. List of objects are usually store in the models attribute. For instance, using JavaScript, Store.Chat.models gives access to the list of chats and Store.Chat.models[0].sendMessage(“Hello!”) will send a message to the first chat. These objects could be seen as whatsapp classes’ managing beans. Store.Chat is the most used object in WhaPy and it’s used for listening to events, sending messages and managing chats.

WhaPy

WhaPy provides two events. They are on_message and on_ready. on_message is fired each time a new message is detected and on_ready is fired when all chats are loaded in the frontend store. Besides these two events, WhaPy also offers interfaces for managing chats and messages.

on_message

This event watches chats via JavaScript (this will be addressed in the next topics). Once the JavaScript code detects an unread message in a chat, the routine will invoke a method for handling unread messages.

on_ready

Because the whatsapp web page is not loaded completely at once, this event is fired each time the chats objects are present in the page.

For handling a whatsapp web session WhaPy uses the selenium library for managing the page via JavaScript. Selenium allows WhaPy to open a web browser and control it with JavaScript code. Perhaps the most beautiful thing about selenium is that it allows objects to go from JavaScript to Python.