3.2. 訂閱號後臺自定義菜單
- 若是訂閱號後臺設置爲開發者模式,在訂閱號正式發佈以前調用一次便可
- 若是你的公衆號沒有認證是沒有這個接口權限的,咱們須要用微信公衆號平臺提供的測試號
- 代碼
// 自定義微信菜單
const express = require("express")
const request = require("request")
const app = express()
app.use("/menu", (req, res, next) => {
//1 獲取access_token
let appid = "wx80aec7a5cef167";
let appsecret = "3fc079b274881b813436a078d3810";
let tokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + appsecret;
request(tokenUrl, (err, response, body) => {
//body是字符串
let json = JSON.parse(body);
console.log(json.access_token);
let data = {
"button": [
{
"type": "click",
"name": "今日歌曲",
"key": "V1001_TODAY_MUSIC"
},
{
"type": "scancode_push",
"name": "掃碼",
"key": "rselfmenu_0_1"
},
{
"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"
}]
}
]
}
request({
method: "post",
url: "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=" + json.access_token,
body: JSON.stringify(data),
headers: {
"Content-Type": "application/json"
}
}, (err, response, body) => {
if(err){
res.send(err)
}else{
res.send(body)
}
})
})
})
// 啓動server
const server = app.listen(3002, function () {
const host = server.address().address;
const port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
免責說明
- 本博客中的文章摘自網上的衆多博客,僅做爲本身知識的補充和整理,並分享給其餘須要的coder,不會用於商用。
- 由於不少博客的地址看完沒有及時作保存,因此不少不會在這裏標明出處,很是感謝各位大牛的分享,也但願你們理解。