Skip to content

Instantly share code, notes, and snippets.

@yanxi123-com
Last active January 1, 2016 12:49
Show Gist options
  • Select an option

  • Save yanxi123-com/8147529 to your computer and use it in GitHub Desktop.

Select an option

Save yanxi123-com/8147529 to your computer and use it in GitHub Desktop.
nodejs 更新微信公共账号菜单
/*
* qiri.com <yanxi@yanxi.com>
* 微信开发文档:http://mp.weixin.qq.com/wiki/index.php?title=%E9%A6%96%E9%A1%B5
*/
var request = require('request');
var async = require('async');
var config = {
appId : '',
appSecret : ''
};
var menu = {
"button" : [ {
"type" : "view",
"name" : "启日科技",
"url" : "http://www.qiri.com/"
}, {
"type" : "click",
"name" : "歌手简介",
"key" : "V1001_TODAY_SINGER"
}, {
"name" : "菜单",
"sub_button" : [ {
"type" : "view",
"name" : "搜索",
"url" : "http://www.soso.com/"
}, {
"type" : "view",
"name" : "视频",
"url" : "http://v.qq.com/"
}, {
"type" : "click",
"name" : "赞一下我们",
"key" : "V1001_GOOD"
} ]
} ]
};
async.auto({
accessToken : function(callback) {
var url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='
+ config.appId + '&secret=' + config.appSecret;
request.get(url, function(err, res, body) {
var result = JSON.parse(body);
var accessToken = result.access_token;
callback(err, accessToken);
});
},
createMenu : ['accessToken', function(callback, results){
var accessToken = results.accessToken;
console.log('accessToken=' + accessToken);
var url = 'https://api.weixin.qq.com/cgi-bin/menu/create?access_token='
+ accessToken;
request({
url : url,
method : 'POST',
body : JSON.stringify(menu)
}, function(err, res, body) {
callback(err, body);
});
}],
}, function(err, results) {
if(err) {
return console.log(err);
}
console.log(results.createMenu);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment