/** * Wechaty - https://github.com/chatie/wechaty * * @copyright 2016-2017 Huan LI * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ import * as path from 'path' /* tslint:disable:variable-name */ const QrcodeTerminal = require('qrcode-terminal') const finis = require('finis') /** * Change `import { ... } from '../'` * to `import { ... } from 'wechaty'` * when you are runing with Docker or NPM instead of Git Source. */ import { config, Wechaty, log, MediaMessage, } from '../index' const BOT_QR_CODE_IMAGE_FILE = path.join( __dirname, '../docs/images/bot-qr-code.png', ) const welcome = ` | __ __ _ _ | \\ \\ / /__ ___| |__ __ _| |_ _ _ | \\ \\ /\\ / / _ \\/ __| '_ \\ / _\` | __| | | | | \\ V V / __/ (__| | | | (_| | |_| |_| | | \\_/\\_/ \\___|\\___|_| |_|\\__,_|\\__|\\__, | | |___/ =============== Powered by Wechaty =============== -------- https://github.com/chatie/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.DEFAULT_PROFILE }) bot .on('logout' , user => log.info('Bot', `${user.name()} logouted`)) .on('login' , user => { log.info('Bot', `${user.name()} logined`) bot.say('Wechaty login').catch(console.error) }) .on('scan', (url, code) => { if (!/201|200/.test(String(code))) { const loginUrl = url.replace(/\/qrcode\//, '/l/') QrcodeTerminal.generate(loginUrl) } console.log(`${url}\n[${code}] Scan QR Code in above url to login: `) }) .on('message', async m => { try { const room = m.room() console.log((room ? '[' + room.topic() + ']' : '') + '<' + m.from().name() + '>' + ':' + m.toStringDigest(), ) if (/^(ding|ping|bing|code)$/i.test(m.content()) && !m.self()) { m.say('dong') log.info('Bot', 'REPLY: dong') const joinWechaty = `Join Wechaty Developers' Community\n\n` + `Wechaty is used in many ChatBot projects by hundreds of developers.\n\n` + `If you want to talk with other developers, just scan the following QR Code in WeChat with secret code: wechaty,\n\n` + `you can join our Wechaty Developers' Home at once` await m.say(joinWechaty) await m.say(new MediaMessage(BOT_QR_CODE_IMAGE_FILE)) await m.say('Scan now, because other Wechaty developers want to talk with you too!\n\n(secret code: wechaty)') log.info('Bot', 'REPLY: Image') } } catch (e) { log.error('Bot', 'on(message) exception: %s' , e) } }) bot.start() .catch(e => { log.error('Bot', 'init() fail: %s', e) bot.stop() process.exit(-1) }) bot.on('error', async e => { log.error('Bot', 'error: %s', e) if (bot.loginout()) { await bot.say('Wechaty error: ' + e.message).catch(console.error) } await bot.stop() }) finis((code, signal) => { const exitMsg = `Wechaty exit ${code} because of ${signal} ` console.log(exitMsg) if (bot.loginout()) { bot.say(exitMsg).catch(console.error) } })