前言:本文是学习网易微专业的《python全栈工程师 - Django快速建站》课程的笔记,欢迎学习交流。同时感谢老师们的精彩传授!
click、view、scancode_push、scancode_waiting、pic_sysphoto、pic_photo_or_album、pic_weixin、location_select、media_id、view_limited
详细介绍参考微信公众号官方文档:https://developers.weixin.qq.com/doc/offiaccount/Custom_Menus/Creating_Custom-Defined_Menu.html
URL
https://api.weixin.qq.com/cgi-bin/menu/create?access_token=ACCESS_TOKEN
wechatpy
封装的方法,可参考官方文档:http://docs.wechatpy.org/zh_CN/master/client/menu.htmlURL
返回信息实操:
Step1: 启动内网穿透,并将域名添加到wechat/wechat/settings.py
中的ALLOWED_HOSTS
中
# 比如我这里是这样的:
ALLOWED_HOSTS = ['sz242a.natappfree.cc',]
Step2: 在测试公众号里填写路由http://sz242a.natappfree.cc/wxmessage/replytype/
,这里内网穿透的域名会跟小编的不一样,要注意。
url
填写好了之后,点击“提交”
。此处注意本地的项目服务器要运行着。下图表示配置成功
Step3: 在wechat/wxmessage/urls.py
中添加create_menu
路由
# -*- coding=utf-8 -*-
from django.urls import path, re_path
from . import views
app_name = 'wxmessage'
urlpatterns = [
re_path('^replytype/', views.send_message, name='send_message'),
path('create_menu/', views.create_menu, name='create_menu'), # new
]
Step4: 修改wechat/wxmessage/views.py
,创建create_menu
视图函数
from django.http.response import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from wechatpy import parse_message, create_reply
from wechatpy.exceptions import InvalidSignatureException
from wechatpy.utils import check_signature
from wechatpy.replies import ArticlesReply
from wechatpy import WeChatClient # new
AUTH_TOKEN = 'f0760e4300a684b6' # 来自natapp.cn,如果不使用内网穿透,可以自己定义
APPID = 'wx4095f32e13c73b9b' # 来自测试公众号的 new
APPSECRET = '1c7affb2196a0758f6bade337c65ef32' # 来自测试公众号的 new
@csrf_exempt
def send_message(request):
if request.method == 'GET': # 验证 url
signature = request.GET.get('signature', '')
timestamp = request.GET.get('timestamp', '')
nonce = request.GET.get('nonce', '')
echo_str = request.GET.get('echostr', '')
try:
check_signature(AUTH_TOKEN, signature, timestamp, nonce)
except InvalidSignatureException:
echo_str = 'error'
response = HttpResponse(echo_str, content_type='text/plain')
return response
elif request.method == 'POST': # 接收微信服务器发来的信息
msg = parse_message(request.body)
if msg.type == 'text':
# reply = create_reply('搜索问答技术的公众号:老齐教室', msg)
# 下面是图文消息
reply = ArticlesReply(message=msg)
reply.add_article({
'title': '老齐教室',
'description': '搜索技术问答的公众号。/n你在这个公众号里,还能阅读到很多优秀的技术文章,看到公开课。',
'image': 'https://public-tuchuang.oss-cn-hangzhou.aliyuncs.com/officialaccounts_20200311104512.png',
'url': 'https://itdiffer.com'
})
elif msg.type == 'image':
reply = create_reply('你刚才发给我的是一张图片', msg)
elif msg.type == 'voice':
reply = create_reply('你刚才发给我的是语音', msg)
elif msg.event== 'click': # 相应菜单 new
reply = ArticlesReply(message=msg)
reply.add_article({
'title': 'Python全栈',
'description': '全栈工程师成功必备课程.',
'image': 'https://public-tuchuang.oss-cn-hangzhou.aliyuncs.com/officialaccounts_20200311104512.png',
'url': 'https://mooc.study.163.com/smartSpec/detail/1202847601.htm'
})
else:
reply = create_reply('这是条其他类型消息', msg)
response = HttpResponse(reply.render(), content_type='application/xml') # reply.render() 生成 xml
return response
else:
logger.info('--------------------')
# new
def create_menu(request):
client = WeChatClient(APPID, APPSECRET)
client.menu.create({
'button': [
{
'type': 'click',
'name': '全栈课程',
'key': 'python_course',
},
{
'name': '文章',
'sub_button': [
{
'type': 'view',
'name': 'Python编程',
'url': 'https://mp.weixin.qq.com/s/zkfCSuyMndWXkUashl3peg',
},
{
'type': 'scancode_waitmsg',
'name': '扫码关注',
'key': 'scan_QR',
}
]
}
]
})
return HttpResponse('已经创建菜单。')
Step5: 访问http://sz242a.natappfree.cc/wxmessage/create_menu/
,
然后用手机微信扫描测试二维码
测试结果如下图: