Skip to content

Commit e6d3a57

Browse files
committed
添加小程序模版消息支持
1 parent 0f0289f commit e6d3a57

2 files changed

Lines changed: 57 additions & 5 deletions

File tree

example/wxapp_api.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,45 @@
11
#! -*- coding: utf-8 -*-
22
from os import environ
3+
from datetime import datetime
34

45
from weixin import WXAPPAPI
6+
from weixin.helper import smart_str
57

68
appid = environ.get("WXAPP_APPID", "appid")
79
secret = environ.get("WXAPP_SECRET", "secret")
810

911
api = WXAPPAPI(appid=appid, app_secret=secret, grant_type="client_credential")
1012
token = api.client_credential_for_access_token().get("access_token")
1113
print(token)
14+
15+
# send template
16+
template_id = ""
17+
openid = ""
18+
form_id = "0e282a55bc674d9abf9aa2e33eed2768"
19+
imageId = ""
20+
json_body = {
21+
"touser": openid,
22+
"template_id": template_id,
23+
"page": "/pages/history/detail/detail?id=%s" % imageId,
24+
"form_id": form_id,
25+
"data": {
26+
"keyword1": {
27+
# 时间
28+
"value": datetime.now().strftime("%Y-%m-%d %H:%M:%S")
29+
},
30+
"keyword2": {
31+
# 备注
32+
"value": "请点击查看".encode("utf-8").decode("latin1")
33+
},
34+
"keyword3": {
35+
# 状态
36+
"value": "图片已经生成".encode("utf-8").decode("latin1"),
37+
"color": "#173177",
38+
},
39+
},
40+
"emphasis_keyword": "keyword3.DATA",
41+
}
42+
43+
api = WXAPPAPI(access_token=token)
44+
resp = api.send_template(json_body=json_body)
45+
print(resp)

weixin/client.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44
"""
55
File: client.py
66
Author: goodspeed
7-
Email: cacique1103@gmail.com
7+
Email: hi@gusibi.mobi
88
Github: https://github.com/gusibi
99
Date: 2015-02-06
10+
UpdatedDate: 2019-06-10
1011
Description: Weixin OAuth2
1112
"""
1213

@@ -199,6 +200,25 @@ def __init__(self, *args, **kwargs):
199200
raise Exception("Unsupported format")
200201
super(WXAPPAPI, self).__init__(*args, **kwargs)
201202

203+
send_template = bind_method(
204+
path="/cgi-bin/message/wxopen/template/send",
205+
method="POST",
206+
accepts_parameters=["json_body"],
207+
response_type="entry",
208+
)
209+
send_template.__doc__ = """
210+
# 发送模板消息
211+
参数:
212+
json_body: object
213+
json_body 结构:
214+
touser string 是 接收者(用户)的 openid
215+
template_id string 是 所需下发的模板消息的id
216+
page string 否 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。
217+
form_id string 是 表单提交场景下,为 submit 事件带上的 formId;支付场景下,为本次支付的 prepay_id
218+
data Object 否 模板内容,不填则下发空模板。具体格式请参考示例。
219+
emphasis_keyword string 否 模板需要放大的关键词,不填则默认无放大
220+
"""
221+
202222

203223
class WxAppCloudAPI(oauth2.OAuth2API):
204224

@@ -352,7 +372,7 @@ def upload_file(self, json_body=None):
352372
("x-cos-meta-fileid", pre_resp.get("cos_file_id")),
353373
("Signature", pre_resp.get("authorization")),
354374
("file", file_data), # file 一定要放到最后,血泪的教训
355-
]
375+
]
356376
# encode
357377
params = [(smart_str(k), v) for k, v in files]
358378
resp = requests.post(pre_resp.get("url"), files=params)
@@ -364,9 +384,7 @@ def upload_file(self, json_body=None):
364384
results = content_obj.get("Error", {})
365385
raise WeixinAPIError(status_code, results["Code"], results["Message"])
366386
else:
367-
raise WeixinAPIError(
368-
400, pre_resp["errcode"], pre_resp["errmsg"]
369-
)
387+
raise WeixinAPIError(400, pre_resp["errcode"], pre_resp["errmsg"])
370388

371389
# 获取文件下载链接
372390
batch_download_file = bind_method(

0 commit comments

Comments
 (0)