提交 db422d1d 编写于 作者: Huan (李卓桓)'s avatar Huan (李卓桓)

Merge branch 'master' of github.com:wechaty/wechaty

......@@ -216,6 +216,7 @@ You can directly submit pull requests for documentation changes.
* [cherry-geqi](https://github.com/cherry-geqi)
* [lijiarui](https://github.com/lijiarui)
* [xinbenlv](https://github.com/xinbenlv)
* [Gcaufy](https://github.com/Gcaufy)
## Join us
......
/**
* Wechaty hot load dots demo
*
* DEV: docker run -ti -e --rm --volume="$(pwd)":/bot zixia/wechaty index.js
* PROD: docker run -ti -e NODE_ENV=production --rm --volume="$(pwd)":/bot zixia/wechaty index.js
*
* @author: Gcaufy
*
*/
const fs = require('fs');
const path = require('path');
const Wechaty = require('wechaty').default;
const isProd = process.env.NODE_ENV === 'production';
const bot = Wechaty.instance();
const EVENT_LIST = ['scan', 'logout', 'login', 'friend', 'room-join', 'room-leave', 'room-topic', 'message', 'heartbeat', 'error'];
// Load lisenter
const loadListener = (evt) => {
let fn;
try {
fn = require(`./listener/${evt}`);
console.log(`binded listener: ${evt}`);
} catch (e) {
fn = () => void 0;
if (e.toString().indexOf('Cannot find module') > -1) {
console.warn(`listener ${evt} is not defined.`);
} else {
console.error(e);
}
}
return fn;
}
// purge require cache
const purgeCache = (moduleName) => {
var mod = require.resolve(moduleName);
if (mod && ((mod = require.cache[mod]) !== undefined)) {
(function traverse(mod) {
mod.children.forEach(function (child) {
traverse(child);
});
delete require.cache[mod.id];
}(mod));
}
Object.keys(module.constructor._pathCache).forEach(function(cacheKey) {
if (cacheKey.indexOf(moduleName)>0) {
delete module.constructor._pathCache[cacheKey];
}
});
};
let eventHandler = {};
if (!isProd) { // start a watcher only if it's not production environment.
fs.watch('./listener', (e, filename) => {
let evt = filename.substring(0, filename.length - 3);
console.log(`${e}: ${filename}`);
if (EVENT_LIST.indexOf(evt) > -1) {
if (e === 'change') {
console.log(`${evt} listener reloaded.`);
purgeCache(`./listener/${evt}`);
// It may read an empty file, if not use setTimeout
setTimeout(() => {
bot.removeListener(evt, eventHandler[evt]);
//console.log('filecontent: ' + fs.readFileSync(`./listener/${evt}.js`));
eventHandler[evt] = loadListener(evt);
bot.on(evt, eventHandler[evt]);
}, 1000);
} else if (e === 'rename') {
console.log(`${evt} listener removed.`);
bot.removeListener(evt, eventHandler[evt]);
eventHandler[evt] = () => void 0;
bot.on(evt, eventHandler[evt]);
}
}
});
}
// Bind events
EVENT_LIST.forEach(evt => {
eventHandler[evt] = loadListener(evt);
bot.on(evt, eventHandler[evt]);
});
bot.init();
\ No newline at end of file
exports = module.exports = async function onFriend (contact, request) {
if(request){
let name = contact.name();
await request.accept();
console.log(`Contact: ${name} send request ${request.hello}`);
}
}
exports = module.exports = function onLoging (user) {
console.log(`${user} login`);
}
exports = module.exports = async function onMessage (message) {
const room = message.room();
const sender = message.from();
const content = message.content();
const topic = room ? '[' + room.topic() + ']' : '';
console.log(`${topic} <${sender.name()}> : ${message.toStringDigest()}`);
if (message.self() || room) {
console.log('message is sent from myself, or inside a room.');
return;
}
if (content === 'ding') {
message.say('thanks for ding me...');
} else {
sender.say('auto reply.');
return;
}
}
\ No newline at end of file
exports = module.exports = function onScan (url, code) {
let loginUrl = url.replace('qrcode', 'l');
require('qrcode-terminal').generate(loginUrl);
console.log(url);
}
\ No newline at end of file
......@@ -102,7 +102,7 @@
"node": ">= 6.9.0"
},
"dependencies": {
"@types/selenium-webdriver": "2.53.40",
"@types/selenium-webdriver": "3.0.0",
"@types/socket.io": "1.4.28",
"body-parser": "1.17.1",
"brolog": "0.3.10",
......@@ -118,18 +118,18 @@
"ws": "2.2.0"
},
"devDependencies": {
"@types/body-parser": "0.0.34",
"@types/body-parser": "1.16.0",
"@types/express": "4.0.35",
"@types/fluent-ffmpeg": "0.0.2",
"@types/mime": "0.0.29",
"@types/node": "7.0.5",
"@types/node": "7.0.7",
"@types/request": "0.0.41",
"@types/sinon": "1.16.35",
"@types/ws": "0.0.37",
"@types/ws": "0.0.39",
"apiai": "4.0.1",
"ava": "0.18.2",
"babel-eslint": "7.1.1",
"check-node-version": "1.1.2",
"check-node-version": "2.0.1",
"cookie-parser": "1.4.3",
"coveralls": "2.12.0",
"cross-env": "3.2.3",
......@@ -137,19 +137,19 @@
"eslint-plugin-ava": "4.2.0",
"finis": "0.0.2",
"fluent-ffmpeg": "2.1.0",
"nyc": "10.1.2",
"jsdoc-to-markdown": "^3.0.0",
"nyc": "10.1.0",
"qrcode-terminal": "0.11.0",
"request": "2.80.0",
"shx": "0.2.2",
"sinon": "2.0.0-pre.6",
"sloc": "0.2.0",
"ts-node": "2.1.0",
"tslint": "4.5.0",
"tslint": "4.5.1",
"tslint-jsdoc-rules": "^0.1.2",
"tuling123-client": "0.0.1",
"typescript": "2.2.1",
"yarn": "0.20.4"
"yarn": "0.21.3"
},
"files_comment__whitelist_npm_publish": "http://stackoverflow.com/a/8617868/1123955",
"files": [
......
......@@ -218,6 +218,7 @@ export {
/**
* to handle unhandled exceptions
*/
/*
process.on('unhandledRejection', (reason, promise) => {
log.error('Config', '###########################')
log.error('Config', 'unhandledRejection: %s %s', reason, promise)
......@@ -226,3 +227,4 @@ process.on('unhandledRejection', (reason, promise) => {
log.error('Config', 'unhandledRejection::catch(%s)', err.message || err)
})
})
*/
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册