/** * * Wechaty - Wechat for Bot * * Connecting ChatBots * https://github.com/wechaty/wechaty * */ /* tslint:disable:variable-name */ const QrcodeTerminal = require('qrcode-terminal') const finis = require('finis') import { Wechaty , Config , log } from '../' 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 = Wechaty.instance({ profile: Config.DEFAULT_PROFILE }) bot .on('login' , user => { log.info('Bot', `${user.name()} logined`) bot.say('Wechaty login') }) .on('logout' , user => log.info('Bot', `${user.name()} logouted`)) .on('error' , e => log.info('Bot', 'error: %s', e)) .on('scan', (url, code) => { if (!/201|200/.test(String(code))) { let loginUrl = url.replace(/\/qrcode\//, '/l/') QrcodeTerminal.generate(loginUrl) } console.log(`${url}\n[${code}] Scan QR Code in above url to login: `) }) .on('message', m => { try { const room = m.room() console.log((room ? '[' + room.topic() + ']' : '') + '<' + m.from().name() + '>' + ':' + m.toStringDigest() ) if (/^(ding|ping|bing)$/i.test(m.content()) && !m.self()) { m.say('dong') log.info('Bot', 'REPLY: dong') } } catch (e) { log.error('Bot', 'on(message) exception: %s' , e) } }) bot.init() .catch(e => { log.error('Bot', 'init() fail: %s', e) bot.quit() process.exit(-1) }) finis((code, reason) => { const exitMsg = `Wechaty exit ${code} because of ${reason} ` console.log(exitMsg) bot.say(exitMsg) })