From e34248abcc607ab45691cd4f20869c03cdd3c340 Mon Sep 17 00:00:00 2001 From: "Zhuohuan LI (CARPE DIEM)" Date: Mon, 9 May 2016 20:39:05 +0800 Subject: [PATCH] example: ding dong auto-replyer --- examples/ding-dong-bot.js | 57 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 examples/ding-dong-bot.js diff --git a/examples/ding-dong-bot.js b/examples/ding-dong-bot.js new file mode 100644 index 00000000..256e5e95 --- /dev/null +++ b/examples/ding-dong-bot.js @@ -0,0 +1,57 @@ +const Wechaty = require('../wechaty') +const log = require('npmlog') +//log.level = 'verbose' + +const welcome = ` +| __ __ _ _ +| \\ \\ / /__ ___| |__ __ _| |_ _ _ +| \\ \\ /\\ / / _ \\/ __| '_ \\ / _\` | __| | | | +| \\ V V / __/ (__| | | | (_| | |_| |_| | +| \\_/\\_/ \\___|\\___|_| |_|\\__,_|\\__|\\__, | +| |___/ + +=============== Powered by Wechaty =============== +-------- https://github.com/zixia/wechaty -------- + +I'm a bot, my super power is talk in Wechat. + +If you send me a 'ding', I will reply you a 'dong'! +__________________________________________________ + +Hope you like it, and you are very welcome to +upgrade me for more super powers! + +Please wait... I'm trying to login in... + +` + +console.log(welcome) + +const bot = new Wechaty() +bot.init() +.then(login) + +bot.on('message', (m) => { + log.info('Bot', 'recv message: %s', m) + if (/^ding$/.test(m.get('content'))) { + const r = new Wechaty.Message() + r.set('to', m.get('from')) + r.set('content', 'dong') + bot.send(r) + .then(() => { log.info('Bot', 'auto replied') }) + } +}) + +function login() { + log.info('Bot', 'Welcome to Wechaty') + + bot.puppet + .getLoginQrImgUrl() + .then(url => + console.log(`\n\nAction needed. Scan the belowing QRCode to login:\n\n${url}\n\nTip: You can copy/paste it to a web browser.\n`) + ).catch(e => log.error('Bot', 'promise rejected')) +} + +bot.on('login' , () => npm.info('Bot', 'logined')) +bot.on('logout' , () => npm.info('Bot', 'logouted')) + -- GitLab