Open JuoJuo opened 1 year ago
const { ScanStatus, WechatyBuilder, log } = require('wechaty'); const qrTerm = require('qrcode-terminal'); function onScan(qrcode, status) { if (status === ScanStatus.Waiting || status === ScanStatus.Timeout) { qrTerm.generate(qrcode, { small: true }); // show qrcode on console const qrcodeImageUrl = [ 'https://wechaty.js.org/qrcode/', encodeURIComponent(qrcode), ].join(''); log.info( 'StarterBot', 'onScan: %s(%s) - %s', ScanStatus[status], status, qrcodeImageUrl ); } else { log.info('StarterBot', 'onScan: %s(%s)', ScanStatus[status], status); } } function onLogin(user) { log.info('StarterBot', '%s login', user); } function onLogout(user) { log.info('StarterBot', '%s logout', user); } async function onMessage(msg) { log.info('StarterBot', msg.toString()); if (msg.text() === 'ding') { await msg.say('dong'); } } const bot = WechatyBuilder.build({ name: 'ding-dong-bot', puppet: 'wechaty-puppet-puppeteer', }); bot.on('scan', onScan); bot.on('login', onLogin); bot.on('logout', onLogout); bot.on('message', onMessage); bot .start() .then(() => log.info('StarterBot', 'Starter Bot Started.')) .catch(e => log.error('StarterBot', e));