diff --git a/examples/ding-dong-bot.js b/examples/ding-dong-bot.js new file mode 100644 index 0000000000000000000000000000000000000000..256e5e951dfdbe1f530b304c477f5a272b46e69b --- /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')) +