preface

Recently took a GDG community said about wechaty live, for implementing wechaty personal WeChat conversational interaction is quite interested in, after watching the live broadcast is practical and is one of the primer, do simple interesting easy to fit in the so-called six lines of code build based on personal WeChat dialogue human-computer interaction interface application, really liked.

Wechaty

Wechaty is an open source chat bot SDK that supports personal wechat accounts. It’s a Node.js application built in Typescript. Support a variety of wechat access solutions, including web, iPad, iOS, Windows, Darwin(OSX/Mac) and Docker platforms.

Wechaty helps developers implement an IM platform that allows users to talk or interact with an connected robot without feeling it. Developers scan code on the IM platform to access, freely select the bottom layer and design the application layer, and finally realize intelligent dialogue.

Demo

  • Step 1: Clone the wechaty-get-started warehouse to the local directory
git clone https://github.com/wechaty/wechaty-getting-started.git
cd wechaty-getting-started
Copy the code
  • Step 2: Install dependency packages
npm install
Copy the code
  • Step3: enter commands on the command line
WECHATY_LOG=verbose WECHATY_PUPPET=wechaty-puppet-wechat npm start
Copy the code

Wechaty robot based on the Web version of wechat can be started.

  • Step4

After starting the command, the interface will generate a TWO-DIMENSIONAL code, and then use their own micro signal scan code login, confirm after login their micro signal change as a robot. Then you can test it. Open the chat dialog box at any time. When the other party sends a message with “ding” in the keyword, your wechat signal will automatically reply “Dong”.

The above simple steps can make a personal wechat account into a wechat chatbot.

The pitfall is when you install dependencies, some resources can’t be downloaded, and then you can switch networks.

Terminal interface

In the terminal, we can see the chat content of the wechat robot, haha, so don’t scan the QR code of strangers.

Core code analysis

1. package.json
  "scripts": { 
    "start:wechat:web": "cross-env WECHATY_LOG=verbose WECHATY_PUPPET=wechaty-puppet-wechat npm start"
  },
Copy the code

“Start :wechat:web” means to start wechaty using the Web protocol. The core code is

WECHATY_LOG=verbose WECHATY_PUPPET=wechaty-puppet-wechat npm start
Copy the code

WECHATY_PUPPET specifies that the current robot logs in to the wechaty-puppet-wechat web protocol. Execute this code directly to start the service.

2. ding-dong-bot.ts
async function onMessage (msg: Message) {
  log.info('StarterBot', msg.toString())

  if (msg.text() === 'ding') {
    await msg.say('dong')
  }
}
Copy the code

MSG. Text () executes MSG. Say (‘dong’) and the robot automatically replies ‘dong’ if it receives ‘ding’.

Industry outlook

Wechaty supports not only wechat chatbot, but also WeCom(Enterprise wechat), Official, WhatsApp, DingTalk(DingTalk), Lark(Flying book), Gitter and 5G RCS(5G Chatbot). In the future, there will be great opportunities in conversational human-computer interaction interfaces, because natural language communication will become the trend of human-computer interaction after a certain degree of artificial intelligence. As an outstanding open source project, Wechaty will help more enterprises achieve commercial success.

The resources

Wechaty -[GDG community says] Video playback address

Wechaty official document

wechaty – github