Finally, there is a Java version of wechat robot.

The official account has not been updated for a long time. There are two main reasons. First, it takes more time to understand and learn the new business. Second, I recently spent almost all my spare time writing the Java version of wechat robot.

What is Wechaty

The description on the website reads:

  • A Conversational AI RPA SDK for Chatbot

It’s an open source SDK that allows you to quickly build chatbots. At the beginning, Wechaty was just a tool library based on wechat. Now, Wechaty has gradually developed to connect with the mainstream chat software in the world, including but not limited to: wechat, enterprise wechat, Dingding, Line, etc.

Programming languages have evolved from TypeScript to Java, Scala, Python, Go, and other multilingual libraries, while the community continues to grow.

Github address: github.com/wechaty/wec… It already has a 7.9K star.

Fell in love with Wechaty

In my previous job, my boss had a requirement that I would send a daily email to briefly describe my work progress after work every day. If you forget to send out daily papers, the next day I will be responsible for organizing the daily papers of the whole group. As an absent-minded person, forgetting to issue the daily paper is a common occurrence.

So we need a mechanism:

  • Remind me to send out the daily paper every day
  • The actions are as simple and automatic as possible.

At that time, I wondered if there could be a robot on wechat to remind me to send daily news regularly every day. As long as I replied to the robot, he could generate daily news according to the fixed template and send it to my boss. This will not be forgotten, and can be easily automated to complete the work.

Google actually found Wechaty. Try to write a daily robot to meet my needs. I went one step further and wrote a tool to remind my girlfriend to eat, but I wasn’t familiar with TypeScript. The written robot couldn’t stop, turned into an information bomber and was almost blocked. How could anyone forget to eat? Write a wechat robot to remind him

It was because of this article that I met Li Jiarui, the author of Wechaty. Now her company is worth a lot of zeros.

Since MY main working language is Java, I still don’t know much about TypeScript, so I put it aside.

Java version of the Wechaty

After a certain version of Wechaty, GRPC was supported as a transport protocol. At this time I think the multi-language development environment is more mature. So I started trying to write a Java version of Wechaty.

Java vs Kotlin

Wechaty was developed using TypeScripe. During the process of porting, Wechaty found that Java required too much template code to implement the TS version’s functions. It was then considered possible to build the Java-wechaty SDK using Kotlin.

Kotlin had the following features that felt appropriate for Wechaty development:

  • Java and Kotlin can interoperate without barriers
  • In Kotlin, functions are also first citizens and can be removed from the existence of classes, which is especially advantageous when porting TS code.
  • Null-pointer safety. When I was writing Java, I was tired of checking step by step. Kotlin solved the problem of null-pointer safety at the language level. Writing effectively reduces mental load.
  • Kotlin is pragmatic, more expressive language. The syntax is closer to TS and GO, and much more concise than Java.

event-driven

The TS version of Wechaty is based on Nodejs, a typical event-driven architecture. I naturally wanted to use the Vertx framework in the early stages of development. However, after a period of development, it turns out that Vertx is actually an event-driven network framework. The main solution is network related problems, java-wechaty is too heavy.

Therefore, the Vertx framework in the code was removed, and EventEmitter in Nodejs was used to implement the event-driven component of Kotlin version.

The overall architecture

  +--------------------------+ +--------------------------+
  |                          | |                          |
  |   Wechaty (TypeScript)   | |     Wechaty (Java)       |
  |                          | |                          |
  +--------------------------+ +--------------------------+

  +-------------------------------------------------------+
  |                 Wechaty Puppet Hostie                 |
  |                                                       |
  |                (wechaty-puppet-hostie)                |
  +-------------------------------------------------------+

+---------------------  @chatie/grpc  ----------------------+

  +-------------------------------------------------------+
  |                Wechaty Puppet Abstract                |
  |                                                       |
  |                   (wechaty-puppet)                    |
  +-------------------------------------------------------+

  +--------------------------+ +--------------------------+
  |      Pad Protocol        | |      Web Protocol        |
  |                          | |                          |
  | wechaty-puppet-padplus   | |(wechaty-puppet-puppeteer)|
  +--------------------------+ +--------------------------+
  +--------------------------+ +--------------------------+
  |    Windows Protocol      | |       Mac Protocol       |
  |                          | |                          |
  | (wechaty-puppet-windows) | | (wechaty-puppet-macpro)  |
  +--------------------------+ +--------------------------+
Copy the code

As you can see from this diagram, Wechaty’s structural design is much clearer. Puppet’s architecture isolates real communication protocols from specific IM software. At this point, different languages can be developed in multiple languages based on Puppet’s protocol.

Does it work

Thanks to Wechaty’s early API design, you can develop your own chatbot in just a few lines of code:

The Demo 1:

class Bot{
  public static void main(String args[]){
    Wechaty bot = Wechaty.instance()
      .onScan((qrcode, statusScanStatus, data) -> System.out.println(QrcodeUtils.getQr(qrcode)))
      .onLogin(user -> System.out.println("User logined :" + user))
      .onMessage(message -> System.out.println("Message:" + message))
      .start(true); }}Copy the code

This Demo 6 lines of code, on the realization of the robot scan code login, accept the message function. Java-wechaty also now supports pluggable plug-ins. With plug-ins, robots can be built much more easily.

The Demo 2:

class Bot{
  public static void main(String args[]){
    Wechaty bot = Wechaty.instance()
            .use(
                WechatyPlugins.ScanPlugin(),
                WechatyPlugins.DingDongPlugin(null)
            )
            .start(true); }}Copy the code

With more and more plug-ins in the original, maybe in the future, users just need to combine various plug-ins, can achieve their own needs, as low code development as possible.

What is it now?

Currently, Java-wechaty has been ported to TS version.

Achieved the basic chat, friends management, group management functions. The rest of the development will focus on polishing the API and improving stability. We also look forward to your participation in contributing code to Java-wechaty.

What do you get from Java-wechaty?

  1. Truly participate in open-source contributions.
  2. In the Maven central library, published its own Jar package.
  3. I met a variety of friends, including @Huan, an angel investor who has been writing apps for 25 years.
  4. While writing Java-wechaty, I constantly looked at the features of various programming languages from a practical perspective by referring to my peers’ TypeScript, Go, and Python code. Explore the original intention of the design of language features.

Looking forward to your joining us

Wechtay participated in the open Source Software Supply Chain Lighting Project – Summer 2020, a summer activity for college students organized by the Institute of Software sciences and the openEuler Community.

Details: github.com/wechaty/sum…

Wechaty provides students with many interesting questions, such as:

  1. Using AI technology, an AI bucket graph robot is developed
  2. Use Wechaty’s plugin technology to create a “Sentence of the Day” plugin for a robot that can flirt with women for you
  3. There’s also engineering, code porting, where students really get involved in open source projects

Development languages include TypeScript, Go, Java, Kotlin, Python and even Scala. There’s always one for you.

If you are reading this, please forward this article to other students who study computer science or are interested in programming.

Afterword.

Java-wechaty project address. Join us and you can also write a wechat robot in six lines of code.