From 4cd633588f154675f47146e31e00a7430a026f04 Mon Sep 17 00:00:00 2001 From: Huan LI Date: Tue, 3 Jul 2018 01:36:12 +0800 Subject: [PATCH] make it simple --- examples/basic-bot.js | 41 ++++++++++++++++++++++++++++++++++++ examples/simplest-bot/bot.js | 26 ----------------------- examples/simplest-bot/bot.ts | 27 ------------------------ examples/six-lines-bot.js | 6 ------ package.json | 3 +-- 5 files changed, 42 insertions(+), 61 deletions(-) create mode 100644 examples/basic-bot.js delete mode 100644 examples/simplest-bot/bot.js delete mode 100644 examples/simplest-bot/bot.ts delete mode 100644 examples/six-lines-bot.js diff --git a/examples/basic-bot.js b/examples/basic-bot.js new file mode 100644 index 0000000..67420d7 --- /dev/null +++ b/examples/basic-bot.js @@ -0,0 +1,41 @@ +const qrTerm = require('qrcode-terminal') + +const { + Wechaty, + Room +} = require('wechaty') + +const bot = new Wechaty() + +bot.on('scan', onScan) +bot.on('login', onLogin) +bot.on('logout', onLogout) +bot.on('message', onMessage) + +bot.start() +.catch(console.error) + +function onScan (qrcode, status) { + qrTerm.generate(qrcode, { small: true }) // show qrcode on console + + const qrcodeImageUrl = [ + 'https://api.qrserver.com/v1/create-qr-code/?data=', + encodeURIComponent(qrcode), + '&size=220x220&margin=20', + ].join('') + + console.log(qrcodeImageUrl) +} + +function onLogin (user) { + console.log(`${user} login`) +} + +function onLogout(user) { + console.log(`${user} logout`) +} + + +async function onMessage (msg) { + console.log(msg.toString()) +} diff --git a/examples/simplest-bot/bot.js b/examples/simplest-bot/bot.js deleted file mode 100644 index 15dad99..0000000 --- a/examples/simplest-bot/bot.js +++ /dev/null @@ -1,26 +0,0 @@ -const { Wechaty, Room } = require('wechaty') -const qrTerm = require('qrcode-terminal') - -const bot = new Wechaty() - -bot.on('scan', (qrcode, status) => { - qrTerm.generate(qrcode, { small: true }) // show qrcode on console - - const qrcodeImageUrl = [ - 'https://api.qrserver.com/v1/create-qr-code/?data=', - encodeURIComponent(qrcode), - '&size=220x220&margin=20', - ].join('') - - console.log(qrcodeImageUrl) -}) - -bot.on('login', user => { - console.log(`${user} login`) -}) - -bot.on('message', async function (msg) { - console.log(msg.toString()) -}) - -bot.start() \ No newline at end of file diff --git a/examples/simplest-bot/bot.ts b/examples/simplest-bot/bot.ts deleted file mode 100644 index 2171347..0000000 --- a/examples/simplest-bot/bot.ts +++ /dev/null @@ -1,27 +0,0 @@ -import { Wechaty, Room } from 'wechaty' - -const qrTerm = require('qrcode-terminal') - -const bot = new Wechaty() - -bot.on('scan', (qrcode, status) => { - qrTerm.generate(qrcode, { small: true }) // show qrcode on console - - const qrcodeImageUrl = [ - 'https://api.qrserver.com/v1/create-qr-code/?data=', - encodeURIComponent(qrcode), - '&size=220x220&margin=20', - ].join('') - - console.log(qrcodeImageUrl) -}) - -bot.on('login', user => { - console.log(`${user} login`) -}) - -bot.on('message', async function (msg) { - console.log(msg.toString()) -}) - -bot.start() \ No newline at end of file diff --git a/examples/six-lines-bot.js b/examples/six-lines-bot.js deleted file mode 100644 index c394483..0000000 --- a/examples/six-lines-bot.js +++ /dev/null @@ -1,6 +0,0 @@ -const { Wechaty } = require('wechaty') // import { Wechaty } from 'wechaty' -Wechaty.instance() // Global Instance -.on('scan', (qrcode, status) => console.log(`Scan QR Code to login: ${status}\n${qrcode}`)) -.on('login', user => console.log(`User ${user} logined`)) -.on('message', message => console.log(`Message: ${message}`)) -.start() \ No newline at end of file diff --git a/package.json b/package.json index b94c7a1..dcfae17 100644 --- a/package.json +++ b/package.json @@ -4,8 +4,7 @@ "description": "Wechaty is an easy to use Wechat Chat Bot SDK. This repo contains the code about the video tutorial.", "main": "index.js", "scripts": { - "start": "ts-node examples/simplest-bot/bot.ts", - "six-lines-bot": "node examples/six-lines-bot.js", + "start": "ts-node examples/basic-bot.js", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": {