#! /usr/bin/env python
# -*- coding: UTF-8 -*-
import requests, sys
class SendWeiXinWork():
def __init__(self):
self.CORP_ID = "xxx" # 企业号的标识
self.SECRET = "xxx" # 管理组凭证密钥
self.AGENT_ID = xxx # 应用ID
self.token = self.get_token()
def get_token(self):
url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken"
data = {
"corpid": self.CORP_ID,
"corpsecret": self.SECRET
}
req = requests.get(url=url, params=data)
res = req.json()
if res['errmsg'] == 'ok':
return res["access_token"]
else:
return res
def send_message(self, to_user, content):
url = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s" % self.token
data = {
# "touser": to_user, # 发送个人就填用户账号
"toparty": to_user, # 发送组内成员就填部门ID
"msgtype": "text",
"agentid": self.AGENT_ID,
"text": {"content": content},
"safe": "0"
}
req = requests.post(url=url, json=data)
res = req.json()
if res['errmsg'] == 'ok':
print("send message sucessed")
return "send message sucessed"
else:
return res
if __name__ == '__main__':
SendWeiXinWork = SendWeiXinWork()
SendWeiXinWork.send_message("2", "测试a")
到此这篇关于教你利用python实现企业微信发送消息的文章就介绍到这了,更多相关python企业微信发送消息内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!