Puppet-Whatsapp
has been published for more than a year, announcement here, and it was still in alpha stage with only login and text message supported. Recently Juzi.bot was planning to support whatsapp in the web application, so we started working on the project.
Primary Goals
Make puppet-whatsapp to be enterprise beta ready
To achieve this goal, Juzi.bot worked from below aspects:
- improve code structure
- improve stability
- improve functionality
Improve code structure
Code structure is also related to the application structure. Before we started, we looked into the code, and found that all the main code logic are written into a huge class called PuppetWhatsapp
. This is okay for an alpha stage puppet, but the scalability and flexibility will be limited by the structure. To make sure we have an enterprise beta ready puppet, we need to have a better foundation. Based on the experience working on other puppets, we extract below components from the huge class:
WhatsappManager
: in charge of everything that connects towhatsapp-web.js
- ‘mixins’: separate implementation of different entities’ into different mixins
Besides, we also added a new manager layer to the PuppetWhatsapp
. With a new PuppetManager
, we are able to make the PuppetWhatsapp
class with least logic, and most code are pretty short and easy to read.
Improve stability
Stability is really important for an enterprise ready puppet.
Login Session
The first step for stability project of a puppet is always keep the login session. Two advantage with an persistent login session:
- easier for develop phase, since restart application won’t require a new scan login
- easier to maintain stability, when we encounter an unknown state that causes application hangs or crashes, we could confidently restart the application and automatically re-login, recover the application with minor down time
So we spent a lot of time working on the login session. We’ve also found some issues in whatsapp-web.js
project. To make sure we could finish PuppetWhatsapp
within a reasonable time, we decide to fork the whatsapp-web.js
and fix the problem, then raise PRs to whatsapp-web.js
project.
Request Control
whatsapp-web.js
is using the puppeteer
to control the whatsapp web application, so the api calling rate and amount needs to be considered, if we are making requests too frequent, the puppeteer
might crash, if we are making too many requests, we might trigger the risk control mechanism of whatsapp. So request control is another key point to ensure stability.
To control the request sent to whatsapp application, there are two different ways, both are important:
- programmatically limit the request rate
- use cache to reduce the get requests to whatsapp
Request Manager
We created a new class called RequestManager
to handle all request related stuff, and within the RequestManager
, we added another component called RateManager
, with RateManager
, we are able to control the request rate separately for different apis, and also it provides a queue mechanism, so we can control the request rate better. For detailed code changes, please refer to this PR
Cache Manager
We created a new class called CacheManager
to manager different caches. We use flash-store
as the underlying data persistent library, which is actually an old friend of puppets.
With CacheManager
, requests to get same contact or room will be served by cache, from previous experience, this will significant reduce the get requests to IMs.
Improve functionality
The alpha version only supports login and text messages, which is too basic for an enterprise ready puppet. So we started to add more features to the Puppet-Whatsapp
.
Refer to new Wechaty 1.0 mixin design, we decide to also use mixins to implement the new functions. We’ve added 7 mixins
Some of the mixin has only un-implemented methods, but they are placeholders for future implementations.
Thanks To
With all works described above, we’ve glad to announce the Puppet-Whatsapp
1.0 are launched!
Thanks to developers contributing to the project
New PuppetWhatsapp Maintainers
To award the excellent contribution to Puppet-Whatsapp
, author of Wechaty, Huan, announced two new Puppet-Whatsapp
maintainers, they are SuperChang and NickWang. And a Github T-shirt, a bag of Github stickers and a Github brooch were given as the maintainer gifts. Additionally, to reward SuperChang’s continuous contribution to Wechaty, a special edition Github cup was rewarded.
Reference
Code base: https://github.com/wechaty/puppet-whatsapp Main Issue: https://github.com/wechaty/puppet-whatsapp/issues/263