最近玩起了 Hubot,主要原因有:
1、想了解这货到底是何方神圣
2、思考在公司内部是否有使用的价值
3、能否通过其提升自己的工作效率
在这过程中,自己尝试写了几个脚本,其中最常用的就是自动翻译和端口获取了。由于代码很简单,也就不额外说明了。有兴趣的同学可以通过注释了解实现,有需要的同学直接copy吧。
ps:网上的例子几乎都是 coffeescript 实现的,不过我还是习惯 es6 格式 javascript,所以以下代码都是基于es6。
'use strict'
let request = require('request');
let fs = require('fs');
function isChina(s)
{
let patrn= /[\u4E00-\u9FA5]|[\uFE30-\uFFA0]/gi;
return (!patrn.exec(s))? false : true;
}
function translate(robot, res)
{
if (res.match.length < 2)
{
return;
}
let str = res.match[1];
let [from, to] = isChina(str) ? ['zh', 'en'] : ['en', 'zh'];
let url = `http:
console.log(str, isChina(str), isChinese(str));
request.get(url, function (err, response, body)
{
let data = JSON.parse(body);
if (data.trans_result.data)
{
res.send(JSON.stringify(data.trans_result.data));
}
else if (data.trans_result.dict_result)
{
res.send(JSON.stringify(data.trans_result.dict_result));
}
else
{
res.send(data);
}
});
}
module.exports = function (robot)
{
robot.hear(
/[fanyi|f] (.*)/i,
(res) => translate(robot, res)
);
}
'use strict'
let spawn = require('child_process').spawn;
function log(...res)
{
console.log(...res);
}
function getCommandOutput(cmd, arg1, arg2 = null, cb = null)
{
arg1 = arg1 || [];
arg2 = arg2 || {};
let output = "";
let handle = spawn(cmd, arg1, arg2);
handle.stdout.on('data', (data) =>
{
output = output + data.toString();
});
handle.stderr.on('data', (data) =>
{
console.log('【命令无法执行】:\n' + data);
});
handle.on('exit', (code, signal) =>
{
if (code == 0 && cb)
{
cb(output);
}
});
}
function parseNetStat(info)
{
let res = {success: false};
let idx = 0;
for (let data of info.split(' '))
{
if (data == '')
{
continue;
}
switch (idx++)
{
case 0 :
res.proto = data;
break;
case 1 :
res.local = data;
break;
case 2 :
res.foreign = data;
break;
case 3 :
res.state = data;
break;
case 4 :
res.pid = data;
break;
}
if (idx == 4)
{
res.success = true;
}
}
return res;
}
function getPortInfo(port, robot = null, res = null, cb = null)
{
getCommandOutput('netstat', ['-ano'], null, (output) =>
{
let lineList = output.split("\r\n");
let portStr = `:${port}`;
let showInfo = `【${port}】\t 端口未被占用`;
for (let info of lineList)
{
let data = parseNetStat(info);
if (data.success == false)
{
continue;
}
if (data.success == false ||
data.local.indexOf(portStr) == -1)
{
continue;
}
if (data.local.indexOf('::') != -1)
{
continue;
}
showInfo = `PID:${data.pid}\t${data.local} \t ${data.foreign}`;
pid2Name(data.pid, robot, res);
break
}
res ? res.send(showInfo) : console.log(showInfo);
})
}
function killTask(pid)
{
if (Number.parseInt(pid) == pid)
{
getCommandOutput('taskkill', ['/f', '/pid', pid]);
}
else
{
getCommandOutput('taskkill', ['/f', '/im', pid]);
}
}
function parseTaskList(info)
{
let res = {success: false};
let idx = 0;
for (let data of info.split(' '))
{
if (data == '')
{
continue;
}
switch (idx++)
{
case 0 :
res.name = data;
break;
case 1 :
res.pid = data;
break;
case 2 :
res.sessionName = data;
break;
case 3 :
res.sessionId = data;
break;
case 4 :
res.memory = data;
break;
}
if (idx == 4)
{
res.success = true;
}
}
return res;
}
function pid2Name(pid, robot = null, res = null, cb = null)
{
getCommandOutput('tasklist', [], null, (output) =>
{
let lineList = output.split("\r\n");
let showInfo = `【${pid}】\t该进程不存在`;
for (let info of lineList)
{
let data = parseTaskList(info);
if (data.success == false || data.pid != pid)
{
continue;
}
showInfo = `${data.name}【PID : ${data.pid}】`
break;
}
res ? res.send(showInfo) : console.log(showInfo);
});
}
module.exports = function (robot)
{
robot.hear(/p (.*)/i, (res) => getPortInfo(res.match[1], robot, res));
robot.hear(/sys p (.*)/i, (res) => getPortInfo(res.match[1], robot, res));
robot.hear(/sys k (.*)/i, (res) => killTask(res.match[1], robot, res));
robot.hear(/k (.*)/i, (res) => killTask(res.match[1], robot, res));
}