现在的情况是服务可以在远程运行起来,访问站点界面如下:
http://likewechat.leanapp.cn/
[cid:image001.jpg@01D22E25.B1B43290]
但是运行todo示例失败:
[cid:image002.jpg@01D22E25.B1B43290]
我的微信在提交验证的时候提示”token失败“, 微信提交界面如下:
[cid:image009.jpg@01D22E25.B1B43290]
主要的代码如下:
Serverjs:
'use strict';
var AV = require('leanengine');
AV.init({
appId: process.env.LEANCLOUD_APP_ID,
appKey: process.env.LEANCLOUD_APP_KEY,
masterKey: process.env.LEANCLOUD_APP_MASTER_KEY
});
// 如果不希望使用 masterKey 权限,可以将下面一行删除
AV.Cloud.useMasterKey();
var app = require('./app');
// 端口一定要从环境变量 LEANCLOUD_APP_PORT
中获取。
// LeanEngine 运行时会分配端口并赋值到该变量。
var PORT = parseInt(process.env.LEANCLOUD_APP_PORT || 3000);
app.listen(PORT, function () {
console.log('Node app is running, port:', PORT);
// 注册全局未捕获异常处理器
process.on('uncaughtException', function(err) {
console.error("Caught exception:", err.stack);
});
process.on('unhandledRejection', function(reason, p) {
console.error("Unhandled Rejection at: Promise ", p, " reason: ", reason.stack);
});
});
App.js
'use strict';
var express = require('express');
var timeout = require('connect-timeout');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var todos = require('./routes/todos');
var AV = require('leanengine');
var app = express();
var wechat = require('./routes/wechatBot');
// 设置模板引擎
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static('public'));
// 设置默认超时时间
app.use(timeout('15s'));
// 加载云函数定义
require('./cloud');
// 加载云引擎中间件
app.use(AV.express());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.get('/', function(req, res) {
res.render('index', { currentTime: new Date() });
});
// 可以将一类的路由单独保存在一个文件中
app.use('/wechat', wechat);
app.use(function(req, res, next) {
// 如果任何一个路由都没有返回响应,则抛出一个 404 异常给后续的异常处理器
if (!res.headersSent) {
var err = new Error('Not Found');
err.status = 404;
next(err);
}
});
// error handlers
app.use(function(err, req, res, next) { // jshint ignore:line
if (req.timedout && req.headers.upgrade === 'websocket') {
// 忽略 websocket 的超时
return;
}
var statusCode = err.status || 500;
if(statusCode === 500) {
console.error(err.stack || err);
}
if(req.timedout) {
console.error('请求超时: url=%s, timeout=%d, 请确认方法执行耗时很长,或没有正确的 response 回调。', req.originalUrl, err.timeout);
}
res.status(statusCode);
// 默认不输出异常详情
var error = {}
if (app.get('env') === 'development') {
// 如果是开发环境,则将异常堆栈输出到页面,方便开发调试
error = err;
}
res.render('error', {
message: err.message,
error: error
});
});
module.exports = app;
Router/wechatBot.js
var router = require('express').Router();
// 引用 wechat 库,详细请查看 https://github.com/node-webot/wechat
var wechat = require('wechat');
var config = {
token: 'weixin',
appid: 'wx4a851ef18c3323ed',
encodingAESKey: 'W4xcwC7OnfH79PqibY5xMiqp8kCDW6fENHYSlAth8YR'
};
var WechatAPI = require('wechat-api');
var api = new WechatAPI('wx4a851ef18c3323ed',
'7895bf59a734c61d4e3f1616a8c2f77d');
router.use('/', wechat(config.token).text(function(message, req, res, next) {
// message为文本内容
// FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
// CreateTime: '1359125035',
// MsgType: 'text',
// Content: 'http',
// MsgId: '5837397576500011341' }
var keyArray = ['你好', '约吗'];
var content = message.Content;
var keyIndex = keyArray.indexOf(content);
switch (keyIndex) {
case 0:
{
res.reply({
type: "text",
content: '您好,大家好才是真的好!'
});
}
break;
case 1:
{
res.reply({
type: "text",
content: '不约,不约,叔叔我们不约!'
});
}
break;
default:
res.reply({
type: "text",
content: '服务器挂掉了,你的要求暂时无法满足……'
});
break;
}
}).image(function(message, req, res, next) {
// message为图片内容
// { ToUserName: 'gh_d3e07d51b513',
// FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
// CreateTime: '1359124971',
// MsgType: 'image',
// PicUrl: 'http://mmsns.qpic.cn/mmsns/bfc815ygvIWcaaZlEXJV7NzhmA3Y2fc4eBOxLjpPI60Q1Q6ibYicwg/0',
// MediaId: 'media_id',
// MsgId: '5837397301622104395' }}).voice(function(message, req, res, next) {
// TODO
}).voice(function(message, req, res, next) {
// message为音频内容
// { ToUserName: 'gh_d3e07d51b513',
// FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
// CreateTime: '1359125022',
// MsgType: 'voice',
// MediaId: 'OMYnpghh8fRfzHL8obuboDN9rmLig4s0xdpoNT6a5BoFZWufbE6srbCKc_bxduzS',
// Format: 'amr',
// MsgId: '5837397520665436492' }
}).video(function(message, req, res, next) {
// message为视频内容
// { ToUserName: 'gh_d3e07d51b513',
// FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
// CreateTime: '1359125022',
// MsgType: 'video',
// MediaId: 'OMYnpghh8fRfzHL8obuboDN9rmLig4s0xdpoNT6a5BoFZWufbE6srbCKc_bxduzS',
// ThumbMediaId: 'media_id',
// MsgId: '5837397520665436492' }
// TODO
}).shortvideo(function(message, req, res, next) {
// message为短视频内容
// { ToUserName: 'gh_d3e07d51b513',
// FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
// CreateTime: '1359125022',
// MsgType: 'shortvideo',
// MediaId: 'OMYnpghh8fRfzHL8obuboDN9rmLig4s0xdpoNT6a5BoFZWufbE6srbCKc_bxduzS',
// ThumbMediaId: 'media_id',
// MsgId: '5837397520665436492' }
// TODO
}).location(function(message, req, res, next) {
// message为链接内容
// { ToUserName: 'gh_d3e07d51b513',
// FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
// CreateTime: '1359125022',
// MsgType: 'link',
// Title: '公众平台官网链接',
// Description: '公众平台官网链接',
// Url: 'http://1024.com/',
// MsgId: '5837397520665436492' }
// TODO
}).link(function(message, req, res, next) {
// message为链接内容
// { ToUserName: 'gh_d3e07d51b513',
// FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
// CreateTime: '1359125022',
// MsgType: 'link',
// Title: '公众平台官网链接',
// Description: '公众平台官网链接',
// Url: 'http://1024.com/',
// MsgId: '5837397520665436492' }
// TODO
}).event(function(message, req, res, next) {
// message为事件内容
// { ToUserName: 'gh_d3e07d51b513',
// FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
// CreateTime: '1359125022',
// MsgType: 'event',
// Event: 'LOCATION',
// Latitude: '23.137466',
// Longitude: '113.352425',
// Precision: '119.385040',
// MsgId: '5837397520665436492' }
// TODO
}).device_text(function(message, req, res, next) {
// message为设备文本消息内容
// { ToUserName: 'gh_d3e07d51b513',
// FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
// CreateTime: '1359125022',
// MsgType: 'device_text',
// DeviceType: 'gh_d3e07d51b513'
// DeviceID: 'dev1234abcd',
// Content: 'd2hvc3lvdXJkYWRkeQ==',
// SessionID: '9394',
// MsgId: '5837397520665436492',
// OpenID: 'oPKu7jgOibOA-De4u8J2RuNKpZRw' }
// TODO
}).device_event(function(message, req, res, next) {
// message为设备事件内容
// { ToUserName: 'gh_d3e07d51b513',
// FromUserName: 'oPKu7jgOibOA-De4u8J2RuNKpZRw',
// CreateTime: '1359125022',
// MsgType: 'device_event',
// Event: 'bind'
// DeviceType: 'gh_d3e07d51b513'
// DeviceID: 'dev1234abcd',
// OpType : 0, //Event为subscribe_status/unsubscribe_status时存在
// Content: 'd2hvc3lvdXJkYWRkeQ==', //Event不为subscribe_status/unsubscribe_status时存在
// SessionID: '9394',
// MsgId: '5837397520665436492',
// OpenID: 'oPKu7jgOibOA-De4u8J2RuNKpZRw' }
// TODO
}).middlewarify());
module.exports = router;
Package.js
{
"name": "LeanEngine_Weixin_Sample",
"version": "1.0.0",
"description": "A sample Weixin server app using LeanEngine",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"keywords": [
"node",
"LeanCloud",
"LeanEngine",
"express",
"weixin"
],
"license": "MIT",
"dependencies": {
"async": "^1.5.2",
"connect-timeout": "^1.7.0",
"body-parser": "1.12.3",
"cookie-parser": "^1.3.5",
"ejs": "2.3.1",
"express": "4.12.3",
"leanengine": "^1.2.1",
"request": "^2.69.0",
"strformat": "0.0.7",
"wechat": "^2.0.3",
"wechat-api": "^1.24.0"
}
}