调用企业微信机器人发通知,中文乱码
解决方案:
json.dumps(datas)
修改为
json.dumps(datas, ensure_ascii=False).encode('utf-8')
企业微信机器人通知源码如下:
# -*- coding:utf-8 -*-
'''
Description: 上报到企业微信群
Author: faith
Date: 2020-12-17 15:00:40
FilePath: \tools\jenkins\deploy\shell\deploy_stage_auto_reporter\notice.py
LastEditors: faith
LastEditTime: 2020-12-18 11:14:27
'''
import json
#import time
import urllib2
import sys
#import os
defaultencoding = 'utf-8'
if sys.getdefaultencoding() != defaultencoding:
reload(sys)
sys.setdefaultencoding(defaultencoding)
print(sys.argv)
print("auto reporter the state of updation")
region = sys.argv[1]
region = region.lower()
action = sys.argv[2]
thirdparam = ""
if len(sys.argv) > 3:
thirdparam = sys.argv[3]
thirdparam = thirdparam.encode('utf8')
print("input:", region, action, thirdparam)
desc_region={"test":u"测试" }
desc_action={
"test_info":u"这是一个测试"}
addr_list={"tets":"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=111",}
mention_list={
"test":"testman",
}
d = { 'msgtype' : 'text', 'text': { } }
if d['text'].has_key('mentioned_list'):
del d['text']['mentioned_list']
if len(thirdparam) > 0:
msg = desc_region[region] + thirdparam + desc_action[action]
else:
msg = desc_region[region] + desc_action[action]
d['text']['content'] = msg
d['text']['mentioned_list'] = mention_list[region]
print("reporter info:",d)
req = urllib2.Request(addr_list[region])
res = urllib2.urlopen(req, json.dumps(d, ensure_ascii=False).encode('utf-8'))
print("reporter rlt:", res.read())
调用方式:
python notice.py test test_info