使用NodeJS本地开发微信公众号示例
来源:互联网 发布:软件打斗视频 编辑:程序博客网 时间:2024/04/29 15:25
1.windows上安装NodeJS
2.新建项目目录,如nodejs_wechat_bot
,进入目录后安装express
和weixin-api
模块,具体方法是:
npm install express npm install weixin-api
3.新建文件index.js
,写入如下内容
var weixin = require('weixin-api'); var express = require('express'); var app = express(); // 接入验证app.get('/', function(req, res) { // 签名成功 if (weixin.checkSignature(req)) { res.status(200).send(req.query.echostr); } else { res.status(200).send('fail'); }});// config 根据自己的实际配置填写weixin.token = 'qbtest';// 监听文本消息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 : "音乐url", HQMusicUrl : "高质量音乐url", 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));});// Startapp.post('/', function(req, res) { // loop weixin.loop(req, res);});app.listen(3000);
4.执行node index.js
启动node server。注意到weixin.token
被赋值了qbtest
,后面会用到。
5.服务器启动,参见“2.微信本地调试服务器篇”
6.进入微信公众平台,填写配置信息,以测试账号为例,注意到Token填写了上文提到的qbtest
7.打开微信公众号,发消息试试
8.进入审查报文
,查看已接收和自动回复的报文
示例中用到的所有代码和模块下载>>,下载后直接node index.js
即可使用。
0 0
- 使用NodeJS本地开发微信公众号示例
- NodeJs开发微信公众号
- Nodejs 开发微信公众号
- nodejs微信公众号开发
- nodejs开发微信公众号支付
- nodejs开发微信公众号
- 微信开发公众号本地调试
- ngrok微信公众号本地开发
- nodejs + BAE 搭建微信公众号开发平台
- NodeJS微信公众平台开发
- 微信公众号开发--使用crontab定时删除没用的本地素材文件
- java微信公众号开发本地web调试
- 微信公众号开发443端口,本地服务器,小记
- 微信公众号本地开发调试工具
- 微信公众号开发-签名验证本地服务器方法
- ngrok配置微信公众号本地开发环境
- 微信公众号开发本地测试方法
- 微信公众号开发 [02] 本地测试环境搭建
- mysql分库分表方案demo
- Angular之基础知识
- HDU 2669 扩展欧几里得最小非负解
- c语言数组总结
- java中利用properties制作配置文件
- 使用NodeJS本地开发微信公众号示例
- MySQL安装无响应
- SSL/TLS协议运行机制的概述
- Leetcode - Integer to Roman
- DOM对象,控制HTML元素
- uboot环境变量与常用命令
- BLE开发的各种坑(转载)
- Spring学习历程---管理bean的生命周期
- android openCV检测图像的基本特征,包括Canny边缘检测、Harris角点检测、霍夫直线检测-基于Android studio