自定义菜单能够帮助公众号丰富界面,让用户更好更快地理解公众号的功能。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/45769.html
文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/45769.html
微信公众号自定义菜单的官方要求文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/45769.html
- 自定义菜单最多包括3个一级菜单,每个一级菜单最多包含5个二级菜单。
- 一级菜单最多4个汉字,二级菜单最多8个汉字。
自定义菜单接口可实现多种类型按钮:文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/45769.html
- click:点击推事件;
- view:跳转URL;
- scancode_push:扫码推事件;
- scancode_waitmsg:扫码推事件且弹出“消息接收中”提示框用户点击按钮后;
- pic_sysphoto:弹出系统拍照发图;
- pic_photo_or_album:弹出拍照或者相册发图;
- pic_weixin:弹出微信相册发图器;
- location_select:弹出地理位置选择器;
- media_id:下发消息(除文本消息)用户点击media_id类型按钮后,微信服务器会将开发者填写的永久素材id对应的素材下发给用户,永久素材类型可以是图片、音频、视频 、图文消息。
开发之前需要先配置好微信公众号接入Django,参考文章Django之微信公众号开发接入,接着获取acces_token,参考文章Django获取微信公众号access_token。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/45769.html
完成上述两部既可在admin上编写自定义菜单创建、获取和删除。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/45769.html
创建自定义菜单文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/45769.html@login_required()def create_menu(request):
#创建自定义菜单
token = share_tools.get_accesstoken()
print(token)
url="https://api.weixin.qq.com/cgi-bin/menu/create?access_token="+token
menu={
"button": [
{
"name": "服务",
"sub_button": [
{
"type": "view",
"name": "官网",
"url": "http://www.xxx.cn"
},
{
"type": "view",
"name": "视频",
"url": "http://video.xxx.cn"
},
]
},
{
"name": "服务",
"sub_button": [
{
"type": "view",
"name": "咨询",
"url": "http://zixun.xxx.cn",
},
]
},
{
"name": "工具",
"sub_button": [
{
"type": "view",
"name": "搜索",
"url": "http://search.xxx.cn"
},
]
} ,
]
}
response = requests.post(url,data=json.dumps(menu, ensure_ascii=False).encode('utf8'),)
print(response.json())
if response.json() is None:
return HttpResponse('success')
# return HttpResponse('菜单创建失败,请核对')
elif response.json()['errmsg']=='ok':
return JsonResponse('failed',safe=False)
# return HttpResponse('菜单创建成功')
else:
return HttpResponse('failed')
获取自定义菜单文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/45769.html@login_required()
def get_menu(request):
#获取自定义菜单
token= share_tools.get_accesstoken()
url=" https://api.weixin.qq.com/cgi-bin/menu/get?access_token="+token
response=requests.get(url)
return JsonResponse(response.json())
删除自定义菜单文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/45769.htmldef delete_menu():
#删除自定义菜单
token = share_tools.get_accesstoken()
url="https://api.weixin.qq.com/cgi-bin/menu/delete?access_token="+token
response = requests.get(url)
return response.json()
使用Django实现菜单的自定义开发就是这么简单文章源自菜鸟学院-https://www.cainiaoxueyuan.com/bc/45769.html