一个奇怪的现象,写了个webhook的应用,然后写中文内容就发不出去,提示我content是空的,写英文数字就可以 Warning: wrong json format. empty content
wuzhi-dev opened this issue · comments
wuzhi-dev commented
import requests
import json
def send_message(params, assets, context_info):
"""发送消息到机器人"""
# 填写wecom机器人的key,如机器人地址是https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=xxxxxx,则添加xxxxxx即可
webhook_key = assets["webhook_key"]
# 消息内容
mess = params["mess"]
# 消息的格式,有text及markdown,默认markdown
mess_type = "markdown" if "mess_type" not in params.keys() or params["mess_type"] == "" else params["mess_type"]
# @谁
if "user" in params.keys() and params["user"] != "" and params["user"] is not None: user= params["user"]
# 返回值
json_ret = {"code": 200, "msg": "","data": {"status": ""}}
'''添加函数实现'''
tmpl = '''{"msgtype": "{mess_type}","{mess_type}": {"content": "{mess}"}}'''.replace("{mess_type}", mess_type,).replace("{mess}", mess)
url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={}".format(webhook_key)
resp = requests.post(url,data=tmpl)
json_ret["msg"] = json.loads(resp.text).get("errmsg")
json_ret["data"]["status"] = resp.status_code
return json_ret
wuzhi-dev commented
resp = requests.post(url, data=tmpl.encode(encoding='utf-8'))
对data做个编码转换就可以了