作者:灰灰t2502911555 | 来源:互联网 | 2023-09-13 09:59
企业微信群机器人SDKPython版#!usrbinpython3#-*-coding:utf-8-*-#微信企业号APIPython3版importtime,datetim
企业微信群机器人SDK Python版
import time,datetime
import requests
import jsonclass WXQY:apiurl = "https://qyapi.weixin.qq.com/cgi-bin/"def __init__(self):passdef send_message_group(self, key, type, data):if not key or not data: returnparams = {'msgtype' : type,}if type == "text":params[type] = {'content' : data}elif type == "markdown":params[type] = {'content' : data}elif type == "news":params[type] = {'articles' : data}else:params["msgtype"] = "text"params["text"] = {'content' : "不支持的消息类型 " + type}url = self.apiurl + 'webhook/send?key=%s'%(key)return self.http_request_v2(url, "POST", params=params)def http_request_v2(self, url, method="GET", headers={}, params=None):headers['User-Agent'] = 'Mozilla/5.0 \(Windows NT 6.1; WOW64\) AppleWebKit/537.36 \(KHTML, like Gecko\) Chrome/39.0.2171.71 Safari/537.36'if method == "GET":response = requests.get(url)elif method == "POST":data = bytes(json.dumps(params), 'utf-8')response = requests.post(url, data= data)elif method == "DELETE":response = requests.delete(url, data= data)result = response.json()return result
调用方法
weixin = WXQY()key = "fa7ec3ec-e85c-0000-8d79-000000000000000"result = weixin.send_message_group(key, "text", datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+" https://blog.csdn.net/bitquant 比特量化");card = '实时新增用户反馈132例,请相关同事注意。\n> 类型:用户反馈> 普通用户反馈:117例> VIP用户反馈:15例'result = weixin.send_message_group(key, "markdown", card);data = [] data.append({"title":"中秋节礼品领取", "description":"今年中秋节公司有豪礼相送", "picurl":"http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png", "url" :"https://www.baidu.com/"})data.append({"title":"多图文2标题", "description":"多图文2描述", "picurl":"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "url" :"https://www.baidu.com/"})data.append({"title":"多图文3标题", "description":"多图文3描述", "picurl":"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "url" :"https://www.baidu.com/"})result = weixin.send_message_group(key, "news", data);print(result)
效果图
![在这里插入图片描述](https://img4.php1.cn/3cdc5/6990/b64/f52e2f526d38b41e.jpeg)