公众号小程序发送模板消息 python

小程序文档

https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/uniform-message/uniformMessage.send.html

公众平台测试号地址

测试号

代码示例


# todo 获取access_token
def get_token():
	from kungfu.settings import REDIS_HOST, REDIS_POST, REDIS_DB, OSWHERE, REDIS_PWD

	if sys.platform == OSWHERE:

		conn_redis = redis.Redis(host=REDIS_HOST, port=REDIS_POST, db=REDIS_DB)
	else:
		conn_redis = redis.Redis(host=REDIS_HOST, port=REDIS_POST, password=REDIS_PWD, db=REDIS_DB)
	token = conn_redis.get("kungfu_access_token")
	if token:
		return token.decode("utf=8")

	url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}'.format(APPID, SECRET)
	respon = requests.get(url)
	content = respon.content

	content = content.decode('utf-8')
	data = json.loads(content)
	token = data.get("access_token")
	if token:
		conn_redis.set("kungfu_access_token",token,3600)
		return token
	return ''

def uniformMessage_send(touser,weapp_template_msg,mp_template_msg):
	"""统一服务消息"""
	token = get_token()
	if not token:
		return False
	url = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/uniform_send?access_token={}".format(token)

	data = {}
	data["touser"] = touser
	if weapp_template_msg:
		data["weapp_template_msg"] = weapp_template_msg

	data["mp_template_msg"] = mp_template_msg

	response = requests.post(url,json=data)
	content = response.content.decode('utf-8')
	data = json.loads(content)
	print(data)
	return data



请求示例

weapp_template_msg = {
        "template_id":"-QQQNKEVORnwRUmwotyQQoZ_ObaSjkA44Qfn6AtIJUg",
        "page":"page/page/index",
        "form_id":"2056",
        "data":{
            "thing1":{
                "value":"李总"
            },
            "thing2":{
                "value":"消息通知"
            },
            "date3":{
                "value":"2010/7/8"
            }
        },
        "emphasis_keyword":"thing2.DATA"
    }
	mp_template_msg= {
		"appid": "wxbc2354bf7e6b010e",
		"template_id": "omNeh2uPKUCnudHTo5OzfhB7bNRgBWz8pnQef2LL6tk",
		"url": "https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.login.html",
		"miniprogram": {
			"appid": APPID,
			"pagepath": "index"
		},
		"data": {
			"first": {
				"value": "恭喜你购买成功!",
				"color": "#173177"
			},
			"keyword1": {
				"value": "45794546",
				"color": "#173177"
			},
			"keyword2": {
				"value": "39.8元",
				"color": "#173177"
			},
			"keyword3": {
				"value": "2014年9月22日",
				"color": "#173177"
			},
			"remark": {
				"value": "欢迎再次购买!",
				"color": "#173177"
			}
		}
	}

	uniformMessage_send("o_Ezd4km_mrPxdMJ19ff0oU8ASuA",{},mp_template_msg)
发布了73 篇原创文章 · 获赞 41 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/weixin_37989267/article/details/105391757
今日推荐