# -*- coding: utf-8 -*-
import requests
import json
class DLF:
def __init__(self, corpid, corpsecret):
self.url = "https://qyapi.weixin.qq.com/cgi-bin"
self.corpid = corpid #填写corpid
self.corpsecret = corpsecret #填写corpsecret
self._token = self._get_token()
def _get_token(self):
'''
获取企业微信API接口的access_token
:return:
'''
token_url = self.url + "/gettoken?corpid=%s&corpsecret=%s" %(self.corpid,
self.corpsecret)
try:
res = requests.get(token_url).json()
token = res['access_token']
return token
except Exception as e:
return str(e)
def _get_media_id(self, file_obj):
get_media_url = self.url + "/media/upload?access_token=
{}&type=file".format(self._token)
data = {"media": file_obj}
try:
res = requests.post(url=get_media_url, files=data)
media_id = res.json()['media_id']
return media_id
except Exception as e:
return str(e)
def send_text(self, agentid, content, touser=None, toparty=None):
send_msg_url = self.url + "/message/send?access_token=%s" %
(self._token)
send_data = {
"touser": touser,
"toparty": toparty,
"msgtype": "text",
"agentid": agentid,
"text": {
"content": content
}
}
try:
res = requests.post(send_msg_url, data=json.dumps(send_data))
except Exception as e:
return str(e)
def send_image(self, agentid, file_obj, touser=None, toparty=None):
media_id = self._get_media_id(file_obj)
send_msg_url = self.url + "/message/send?access_token=%s" %
(self._token)
send_data = {
"touser": touser,
"toparty": toparty,
"msgtype": "image",
"agentid": agentid,
"image": {
"media_id": media_id
}
}
try:
res = requests.post(send_msg_url, data=json.dumps(send_data))
except Exception as e:
return str(e)
这段代码是一个用于与企业微信(WeChat Work)交互的 Python 类,用于发送文本消息和图片消息。让我们逐行解释代码的功能和每个部分的作用:
1. 导入模块和设置编码
# -*- coding: utf-8 -*-
import requests
import json
requests
模块用于发送 HTTP 请求。json
模块用于处理 JSON 数据。# -*- coding: utf-8 -*-
表示该文件采用 UTF-8 编码。
2. 定义 DLF 类
class DLF:
def __init__(self, corpid, corpsecret):
self.url = "https://qyapi.weixin.qq.com/cgi-bin"
self.corpid = corpid # 填写 corpid
self.corpsecret = corpsecret # 填写 corpsecret
self._token = self._get_token()
- 定义了一个名为
DLF
的类,用于与企业微信进行交互。 __init__
方法用于初始化对象,需要提供corpid
和corpsecret
作为参数。url
是企业微信 API 的基础 URL。corpid
和corpsecret
是企业微信应用的身份验证信息。_token
是一个私有属性,用于存储获取的 access_token。
3. 获取 access_token
def _get_token(self):
'''
获取企业微信API接口的access_token
:return:
'''
token_url = self.url + "/gettoken?corpid=%s&corpsecret=%s" % (self.corpid, self.corpsecret)
try:
res = requests.get(token_url).json()
token = res['access_token']
return token
except Exception as e:
return str(e)
_get_token
方法用于从企业微信服务器获取 access_token。- 使用
requests.get
发送 GET 请求获取 access_token。 - 如果请求成功,从返回的 JSON 数据中提取出 access_token 并返回;如果请求失败,返回错误信息。
4. 获取媒体文件 ID
def _get_media_id(self, file_obj):
get_media_url = self.url + "/media/upload?access_token={}&type=file".format(self._token)
data = {"media": file_obj}
try:
res = requests.post(url=get_media_url, files=data)
media_id = res.json()['media_id']
return media_id
except Exception as e:
return str(e)
_get_media_id
方法用于上传媒体文件并获取其对应的 media_id。- 使用
requests.post
发送 POST 请求上传媒体文件。 - 如果上传成功,从返回的 JSON 数据中提取出 media_id 并返回;如果上传失败,返回错误信息。
5. 发送文本消息
def send_text(self, agentid, content, touser=None, toparty=None):
send_msg_url = self.url + "/message/send?access_token=%s" % (self._token)
send_data = {
"touser": touser,
"toparty": toparty,
"msgtype": "text",
"agentid": agentid,
"text": {
"content": content
}
}
try:
res = requests.post(send_msg_url, data=json.dumps(send_data))
except Exception as e:
return str(e)
send_text
方法用于发送文本消息。- 构建消息内容和接收者信息,并将其转换为 JSON 格式。
- 使用
requests.post
发送 POST 请求发送消息。
6. 发送图片消息
def send_image(self, agentid, file_obj, touser=None, toparty=None):
media_id = self._get_media_id(file_obj)
send_msg_url = self.url + "/message/send?access_token=%s" % (self._token)
send_data = {
"touser": touser,
"toparty": toparty,
"msgtype": "image",
"agentid": agentid,
"image": {
"media_id": media_id
}
}
try:
res = requests.post(send_msg_url, data=json.dumps(send_data))
except Exception as e:
return str(e)
send_image
方法用于发送图片消息。- 调用
_get_media_id
方法获取图片文件的 media_id。 - 构建消息内容和接收者信息,并将其转换为 JSON 格式。
- 使用
requests.post
发送 POST 请求发送消息。
这个类可以用于在企业微信中发送文本消息和图片消息,提供了简单易用的接口。