tutorial,

Getting Started with Wechaty - Live Coding Tutorial

Jiarui LI (李佳芮) Jiarui LI (李佳芮) Follow Jan 01, 2017 · 3 mins read
Getting Started with Wechaty - Live Coding Tutorial

Wechaty is super easy to use, especially when you are using Docker.

In this video, @lijiarui will show you how to getting started with Wechaty through a 10 minutes live coding tutorial, with Wechaty Docker Runtime. (Event Node.js Party #18, Beijing)

For visiters come from China who can not visit YouTube.com, this video is also hosted on YouKu.com & Tencent Video

Learn more about how to use Wechaty: https://github.com/wechaty/wechaty/wiki/GettingStarted

Code in the video:

import {Wechaty, Room} from 'wechaty'

const bot = Wechaty.instance()

bot
.on('scan', (url, code)=>{
    let loginUrl = url.replace('qrcode', 'l')
    require('qrcode-terminal').generate(loginUrl)
    console.log(url)
})

.on('login', user=>{
    console.log(`${user} login`)
})

.on('friend', async function (contact, request){
    if(request){
        await request.accept()
        console.log(`Contact: ${contact.name()} send request ${request.hello}`)
    }
})

.on('message', async function(m){
    const contact = m.from()
    const content = m.content()
    const room = m.room()

    if(room){
        console.log(`Room: ${room.topic()} Contact: ${contact.name()} Content: ${content}`)
    } else{
        console.log(`Contact: ${contact.name()} Content: ${content}`)
    }

    if(m.self()){
        return
    }

    if(/hello/.test(content)){
        m.say("hello how are you")
    }

    if(/room/.test(content)){
        let keyroom = await Room.find({topic: "test"})
        if(keyroom){
            await keyroom.add(contact)
            await keyroom.say("welcome!", contact)
        }
    }

    if(/out/.test(content)){
        let keyroom = await Room.find({topic: "test"})
        if(keyroom){
            await keyroom.say("Remove from the room", contact)
            await keyroom.del(contact)
        }
    }
})

.init()

docker command:

docker run -ti --volume="$(pwd)":/bot --rm zixia/wechaty mybot.ts

Click here to get the repo

Join Newsletter
Get the latest news right in your inbox. We never spam!
Written by Jiarui LI (李佳芮) Follow
Co-Creator of Wechaty; 句子互动创始人 & CEO,微软人工智能最具价值专家 (AI MVP)