登录
注册
node.js 学习社区
微信API For Node.js

安静的回应

2014-12-13 01:34

写了个微信公众API:
Github:https://github.com/JeremyWei/weixin_api
NPM:https://npmjs.org/package/weixin-api

我们使用express来开发微信公众平台应用,首先安装express:

    npm install express -g

创建应用

    express myweixin

cd myweixin修改package.json,添加对weixin-api的依赖

{
  "name": "application-name",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "node app.js"
  },
  "dependencies": {
    "express": "3.1.1",
    "jade": "*",
    "xml2js" : "0.2.6",
    "weixin-api" : ">=0.1.3"
  }
}

然后执行

    npm install

实例

var express = require('express'),
    weixin = require('weixin-api'),
    app = express();

// 解析器
app.use(express.bodyParser());
//app.use(xmlBodyParser);

// 接入验证
app.get('/', function(req, res) {

    // 签名成功
    if (weixin.checkSignature(req)) {
        res.send(200, req.query.echostr);
    } else {
        res.send(200, 'fail');
    }
});

// config
weixin.token = '你的token';

// 监听文本消息
weixin.textMsg(function(msg) {
    console.log("textMsg received");
    console.log(JSON.stringify(msg));

    var resMsg = {};

    switch (msg.content) {
        case "文本" :
            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 : "",
                HQMusicUrl : "",
                funcFlag : 0
            };
            break;

        case "图文" :

            var articles = [];
            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
            }
    }

    weixin.sendMsg(resMsg);
});

// 监听图片消息
weixin.imageMsg(function(msg) {
    //
    console.log("imageMsg received");
    console.log(JSON.stringify(msg));
});

// 监听位置消息
weixin.locationMsg(function(msg) {
    //
    console.log("locationMsg received");
    console.log(JSON.stringify(msg));
});

// 监听链接消息
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));
});

// Start
app.post('/', function(req, res) {

    // loop
    weixin.loop(req, res);

});

app.listen(3000);

原文引自:http://weizhifeng.net/weixin-with-node.js.html

回复 · 1

  • hello!我是社交APP「她说」的HR。她说是由红杉资本投资,是一款高质量的陌生人社交产品,业内好评度很高。现在正处于高速发展阶段。想找一位优秀的Node.js研发工程师一起打造杰出的产品。岗位base在北京朝阳门银河SOHO。如感兴趣,可以发简历至邮箱:hr@intelcupid.com,或者加我微信1229100802哈~

    0

发表回复

你可以在回复中 @ 其他人