2024年1月27日 19:58 by wst
公众号开发微信自带的菜单不够灵活,项目中有时要自定义菜单。具体代码如下:
import requests
import json
from wanhuqianjia.settings import MENU_URL
from .utils import get_token, remove_token
MENU = {
"button":[
{
"type":"view",
"name":"抖音学习",
"key":"V1001_DOUYIN",
"url": "https://h5.xiaoeknow.com"
},
{
"type":"click",
"name":"签约合作",
"key":"V1002_SIGN",
"url": "https://union.bytedance.com/open/"
},
{
"type":"view",
"name":"打卡更新",
"key":"V1003_CONTACT",
"url": "https://h5.xiaoeknow.com/p/course/ecourse/"
}
]
}
def create_menu(menu, menu_url):
"创建自定义菜单"
token = get_token()
url = menu_url + "?access_token=" + token
data = json.dumps(menu,ensure_ascii=False).encode('utf-8')
res = requests.post(url, data=data)
res_data = res.json()
if "errcode" in res_data and res_data['errcode']==40001:
remove_token()
res = requests.post(url, data=data)
print("res:", res.json())
def test_menu():
"创建菜单"
create_menu(MENU, MENU_URL)
if __name__ == "__main__":
test_menu()
这里列出的是主要代码,如果问题,欢迎留言!