【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
一. 概述
上文主要是关于微信主动发送消息给用户, 这篇文章主要是用来描述如何开启微信企业号的回调模式
二 . 操作
1. 登陆微信企业号应用中心--我的应用--模式选择--回调模式
2. 代码
#coding=utf-8
from flask import abort, request
from wechatpy.enterprise import create_reply, parse_message
from wechatpy.enterprise.crypto import WeChatCrypto
from app.config import CORP_ID
from flask import Blueprint
#应用ID
sCorp_Id = CORP_ID
#回调模式里面随机生成的那个Token,EncodingAESKey
sToken = ''
sEncodingAESKey =""
crm = Blueprint('settings', __name__, url_prefix='/crm')
crypto = WeChatCrypto(token=sToken, encoding_aes_key=sEncodingAESKey, corp_id=sCorp_Id)
#对应回调模式中的URL
@crm.route('/weixin')
def weixin():
echo_str = signature(request)
if request.method == 'GET':
return echo_str
msg = parse_message(request.data)
print(msg.type)
if msg.type == 'text':
reply = create_reply(msg.content, msg)
else:
reply = create_reply('Sorry, can not handle this for now', msg)
return reply.render()
def signature(request):
msg_signature = request.args.get('msg_signature', '')
timestamp = request.args.get('timestamp', '')
nonce = request.args.get('nonce', '')
echo_str = request.args.get('echostr', '')
print(request.args)
try:
#认证并对echo_str进行解密并返回明文
echo_str = crypto.check_signature(msg_signature, timestamp, nonce, echo_str)
print(echo_str)
except Exception as ex:
print(ex)
print(request)
abort(403)
return echo_str
@crm.route('/hello')
def hello():
return "hello! index"
3.说明
按照如上进行配置就可以成功开启回调模式,然后就可以成功接受发过来的消息。
来源:oschina
链接:https://my.oschina.net/u/1406293/blog/631143