var express = require('express');
var router = express.Router();
var util = require('util')
var weixin = require('weixin-api');
var colors = require('colors')
var net = require('net');
var HOST = '127.0.0.1';
var PORT = 5656;
var fs=require('fs')
router.get('/', function(req, res, next) {
//console.log( util.inspect( res).red.underline )
if (weixin.checkSignature(req)) {
console.log( util.inspect( req.query).red.underline )
res.status(200).send(req.query.echostr);
//weixin.loop(req, res);
} else {
res.status(200).send('fail');
}
});
weixin.token = 'qwertyuiopasdfghjkl';
weixin.textMsg(function(msg) {
console.log("textMsg received".red);
console.log(JSON.stringify(msg));
var resMsg = {};
switch (msg.content) {
case "開機" :
var fd=fs.openSync('/tmp/pipeopen' , 'w')
fs.writeSync(fd , 'start')
fs.closeSync(fd)
//
resMsg = {
fromUserName : msg.toUserName,
toUserName : msg.fromUserName,
msgType : "text",
content : "機器開啓成功",
funcFlag : 0
};
break;
case "關機" :
var fd=fs.openSync('/tmp/pipeopen' , 'w')
fs.writeSync(fd , 'close')
fs.closeSync(fd)
resMsg = {
fromUserName : msg.toUserName,
toUserName : msg.fromUserName,
msgType : "text",
content : "機器關機成功",
funcFlag : 0
};
break;
case "播放音樂" :
resMsg = {
fromUserName : msg.toUserName,
toUserName : msg.fromUserName,
msgType : "music",
title : "音樂標題",
description : "音樂描述",
musicUrl : "http://music.baidu.com/mv/60819306/?type=mv",
HQMusicUrl : "高質量音樂url",
funcFlag : 0
};
break;
case "圖片" :
var articles = [];
articles[0] = {
title : "美女圖片",
description : "美女圖片",
picUrl : "http://huliankfb.com:81/resource/7.jpg",
url : "http://pic.yesky.com/75/52568575.shtml#baidu"
};
/* articles[0] = {
title : "PHP依賴管理工具Composer入門",
description : "PHP依賴管理工具Composer入門",
picUrl : "http://weizhifeng.net/images/tech/composer.png",
url : "http://weizhifeng.net/manage-php-dependency-with-composer.html"
};
articles[1] = {
title : "八月西湖",
description : "八月西湖",
picUrl : "http://weizhifeng.net/images/poem/bayuexihu.jpg",
url : "http://weizhifeng.net/bayuexihu.html"
};
articles[2] = {
title : "「翻譯」Redis協議",
description : "「翻譯」Redis協議",
picUrl : "http://weizhifeng.net/images/tech/redis.png",
url : "http://weizhifeng.net/redis-protocol.html"
};*/
// 返回圖文消息
resMsg = {
fromUserName : msg.toUserName,
toUserName : msg.fromUserName,
msgType : "news",
articles : articles,
funcFlag : 0
}
break;
default:
resMsg = {
fromUserName : msg.toUserName,
toUserName : msg.fromUserName,
msgType : "text",
content : "抱歉,不能識別的指令",
funcFlag : 0
};
}
weixin.sendMsg(resMsg);
});
// 監聽圖片消息
weixin.imageMsg(function(msg) {
console.log("imageMsg received");
console.log(JSON.stringify(msg));
resMsg = {
fromUserName : msg.toUserName,
toUserName : msg.fromUserName,
msgType : "text",
content : msg.picUrl,
funcFlag : 0
};
weixin.sendMsg(resMsg);
//weixin.sendMsg(resMsg);
});
// 監聽位置消息
weixin.locationMsg(function(msg) {
resMsg = {
fromUserName : msg.toUserName,
toUserName : msg.fromUserName,
msgType : "text",
content : msg.label,
funcFlag : 0
};
weixin.sendMsg(resMsg);
});
// 監聽鏈接消息
weixin.urlMsg(function(msg) {
console.log("urlMsg received");
console.log(JSON.stringify(msg));
});
// 監聽事件消息
weixin.eventMsg(function(msg) {
console.log("eventMsg received");
console.log(JSON.stringify(msg));
});
router.post('/', function(req, res) {
// loop
weixin.loop(req, res);
});
module.exports = router;
基於Node.js的微服務後臺
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.