python實(shí)現(xiàn)微信自定義菜單管理,具體代碼如下:
# -*- coding: utf-8 -*-
'''
Created on 2013年10月28日
@author: 壞壞的憂(yōu)傷
'''
import urllib.request
import json
class MenuManager:
accessUrl = ""
delMenuUrl = "="
createUrl = "="
getMenuUri="="
def getAccessToken(self):
f = urllib.request.urlopen(self.accessUrl)
accessT = f.read().decode("utf-8")
jsonT = json.loads(accessT)
return jsonT["access_token"]
def delMenu(self, accessToken):
html = urllib.request.urlopen(self.delMenuUrl + accessToken)
result = json.loads(html.read().decode("utf-8"))
return result["errcode"]
def createMenu(self, accessToken):
menu = '''{
"button":[
{
"type":"click",
"name":"今日歌曲",
"key":"V1001_TODAY_MUSIC"
},
{
"type":"view",
"name":"歌手簡(jiǎn)介",
"url":""
},
{
"name":"菜單",
"sub_button":[
{"type":"click","name":"hello word","key":"V1001_HELLO_WORLD"},{"type":"click","name":"贊一下我們","key":"V1001_GOOD"}]}]}'''
html = urllib.request.urlopen(self.createUrl + accessToken, menu.encode("utf-8"))
result = json.loads(html.read().decode("utf-8"))
return result["errcode"]
def getMenu(self):
html = urllib.request.urlopen(self.getMenuUri + accessToken)
print(html.read().decode("utf-8"))
if __name__ == "__main__":
wx = MenuManager()
accessToken = wx.getAccessToken()
#print(wx.delMenu(accessToken)) #刪除菜單
#print(wx.createMenu(accessToken)) #創(chuàng)建菜單
wx.getMenu()
更多信息請(qǐng)查看IT技術(shù)專(zhuān)欄