Skip to main content

World's Shortest Chatbot

Powered by Wechaty TypeScript

World's Shortest Chatbot is the very first example showcasing how easy it is to get started with Wechaty in minimum 6 lines of code.

Requirements

  1. Node.js v16+
  2. Wechaty v0.40+

Getting started

You should have Node.js installed on your system. If you do not have Node.js installed (or have a version below 12), then you need to install the latest version of Node.js by following the links below:

Node.js installation docs

Installation guide for Node.js on other platforms can be found here.

Usage

import { Wechaty } from 'wechaty'

async function main () {
const bot = new Wechaty()
bot
.on('scan', (qrcode, status) => console.log(`Scan QR Code to login: ${status}\nhttps://wechaty.js.org/qrcode/${encodeURIComponent(qrcode)}`))
.on('login', user => console.log(`User ${user} logged in`))
.on('message', message => console.log(`Message: ${message}`))
await bot.start()
}

main()
.catch(console.error)

For building a bot with Wechaty, you have to follow the steps below:

  1. Import wechaty.
  2. Create a function main and initialize a bot by providing it a name.
  3. Assign proper functions to call when an event is triggered.
    • When scan is triggered, it generates QR code.
    • login will display {user} logged in if the user has logged in.
    • message will display message on console.
  4. Finally, start the bot with bot.start.

Reference

Wechaty Getting Started Github Repository