pixelarraylib 1.2.1__tar.gz → 1.2.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pixelarraylib-1.2.1/pixelarraylib.egg-info → pixelarraylib-1.2.3}/PKG-INFO +1 -1
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/__init__.py +1 -1
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/aliyun/oss.py +177 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/monitor/feishu.py +222 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/scripts/__init__.py +1 -1
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3/pixelarraylib.egg-info}/PKG-INFO +1 -1
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pyproject.toml +1 -1
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/LICENSE +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/MANIFEST.in +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/README.md +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/__main__.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/aliyun/__init__.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/aliyun/acr.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/aliyun/aliyun_email.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/aliyun/billing.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/aliyun/content_scanner.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/aliyun/domain.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/aliyun/eci.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/aliyun/ecs.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/aliyun/eip.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/aliyun/fc.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/aliyun/polardb.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/aliyun/sms.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/aliyun/sts.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/db_utils/mysql.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/db_utils/redis.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/decorators/__init__.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/decorators/decorators.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/gitlab/__init__.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/gitlab/code_analyzer.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/gitlab/pypi_package_manager.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/monitor/__init__.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/scripts/build_website.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/scripts/collect_code_to_txt.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/scripts/create_test_case_files.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/scripts/nginx_proxy_to_ecs.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/scripts/remove_empty_lines.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/scripts/tson_convert.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/system/__init__.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/system/common.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/system/cron_manager.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/system/tson.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib/utils/name_generator.py +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib.egg-info/SOURCES.txt +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib.egg-info/dependency_links.txt +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib.egg-info/entry_points.txt +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib.egg-info/requires.txt +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/pixelarraylib.egg-info/top_level.txt +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/requirements.txt +0 -0
- {pixelarraylib-1.2.1 → pixelarraylib-1.2.3}/setup.cfg +0 -0
|
@@ -7,6 +7,7 @@ import base64
|
|
|
7
7
|
import aiohttp
|
|
8
8
|
import asyncio
|
|
9
9
|
import xml.etree.ElementTree as ET
|
|
10
|
+
import requests
|
|
10
11
|
from hashlib import sha1
|
|
11
12
|
from datetime import datetime
|
|
12
13
|
from urllib.parse import quote, urlencode
|
|
@@ -39,6 +40,8 @@ class OSSUtils:
|
|
|
39
40
|
bucket_name(str): OSS存储桶名称
|
|
40
41
|
use_vpc(bool): 是否使用VPC内网端点,默认为False
|
|
41
42
|
"""
|
|
43
|
+
self.access_key_id = access_key_id
|
|
44
|
+
self.access_key_secret = access_key_secret
|
|
42
45
|
self.bucket_name = bucket_name
|
|
43
46
|
if use_vpc:
|
|
44
47
|
self.endpoint = f"https://oss-{region_id}-internal.aliyuncs.com"
|
|
@@ -47,6 +50,8 @@ class OSSUtils:
|
|
|
47
50
|
self.client = self.get_oss_client(
|
|
48
51
|
access_key_id, access_key_secret, self.endpoint, bucket_name
|
|
49
52
|
)
|
|
53
|
+
# 创建oss2的Auth对象用于签名
|
|
54
|
+
self.auth = oss2.Auth(access_key_id, access_key_secret)
|
|
50
55
|
|
|
51
56
|
def get_oss_client(
|
|
52
57
|
self, access_key_id, access_key_secret, endpoint, bucket_name, retry=3
|
|
@@ -71,6 +76,94 @@ class OSSUtils:
|
|
|
71
76
|
print(e)
|
|
72
77
|
return None
|
|
73
78
|
|
|
79
|
+
def _sign_request(self, method, url, headers=None, params=None):
|
|
80
|
+
"""
|
|
81
|
+
description:
|
|
82
|
+
使用oss2库生成OSS签名
|
|
83
|
+
parameters:
|
|
84
|
+
method(str): HTTP方法
|
|
85
|
+
url(str): 请求URL
|
|
86
|
+
headers(dict): 请求头
|
|
87
|
+
params(dict): 请求参数
|
|
88
|
+
return:
|
|
89
|
+
headers(dict): 包含签名的请求头
|
|
90
|
+
"""
|
|
91
|
+
if headers is None:
|
|
92
|
+
headers = {}
|
|
93
|
+
if params is None:
|
|
94
|
+
params = {}
|
|
95
|
+
|
|
96
|
+
# 创建oss2的Request对象
|
|
97
|
+
req = oss2.http.Request(method, url, params=params, headers=headers)
|
|
98
|
+
|
|
99
|
+
# 使用oss2的签名功能
|
|
100
|
+
self.auth._sign_request(req, "", "")
|
|
101
|
+
|
|
102
|
+
# 过滤掉None键和None值
|
|
103
|
+
result = {}
|
|
104
|
+
for k, v in req.headers.items():
|
|
105
|
+
if k is not None and v is not None:
|
|
106
|
+
result[str(k)] = str(v)
|
|
107
|
+
return result
|
|
108
|
+
|
|
109
|
+
def list_regions(self):
|
|
110
|
+
"""
|
|
111
|
+
description:
|
|
112
|
+
列出OSS支持的区域
|
|
113
|
+
return:
|
|
114
|
+
regions(list): 区域列表,每个元素为字典,包含 region_id, region_name, internal_endpoint, internet_endpoint
|
|
115
|
+
"""
|
|
116
|
+
try:
|
|
117
|
+
# 使用通用的OSS endpoint来获取区域列表
|
|
118
|
+
url = "https://oss-cn-hangzhou.aliyuncs.com/?regions"
|
|
119
|
+
|
|
120
|
+
# 生成签名
|
|
121
|
+
headers = self._sign_request("GET", url)
|
|
122
|
+
|
|
123
|
+
# 发送请求
|
|
124
|
+
response = requests.get(url, headers=headers)
|
|
125
|
+
response.raise_for_status()
|
|
126
|
+
|
|
127
|
+
# 解析XML响应
|
|
128
|
+
root = ET.fromstring(response.text)
|
|
129
|
+
regions = []
|
|
130
|
+
|
|
131
|
+
# 查找所有 RegionInfo 元素
|
|
132
|
+
for region_info_elem in root.findall(".//RegionInfo"):
|
|
133
|
+
region_elem = region_info_elem.find("Region")
|
|
134
|
+
internal_endpoint_elem = region_info_elem.find("InternalEndpoint")
|
|
135
|
+
internet_endpoint_elem = region_info_elem.find("InternetEndpoint")
|
|
136
|
+
|
|
137
|
+
region_id = region_elem.text if region_elem is not None else ""
|
|
138
|
+
# 从 region_id 中提取区域名称(去掉 oss- 前缀)
|
|
139
|
+
region_name = region_id.replace("oss-", "") if region_id else None
|
|
140
|
+
internal_endpoint = internal_endpoint_elem.text if internal_endpoint_elem is not None else None
|
|
141
|
+
internet_endpoint = internet_endpoint_elem.text if internet_endpoint_elem is not None else None
|
|
142
|
+
|
|
143
|
+
if region_id:
|
|
144
|
+
regions.append({
|
|
145
|
+
"region_id": region_id,
|
|
146
|
+
"region_name": region_name,
|
|
147
|
+
"internal_endpoint": internal_endpoint,
|
|
148
|
+
"internet_endpoint": internet_endpoint
|
|
149
|
+
})
|
|
150
|
+
self.regions = regions
|
|
151
|
+
return regions
|
|
152
|
+
except Exception as e:
|
|
153
|
+
print(f"list_regions error: {traceback.format_exc()}, {e}")
|
|
154
|
+
self.regions = []
|
|
155
|
+
return []
|
|
156
|
+
|
|
157
|
+
def list_region_ids(self):
|
|
158
|
+
"""
|
|
159
|
+
description:
|
|
160
|
+
列出OSS支持的区域ID
|
|
161
|
+
return:
|
|
162
|
+
region_ids(list): 区域ID列表
|
|
163
|
+
"""
|
|
164
|
+
regions = self.list_regions()
|
|
165
|
+
return [region["region_id"] for region in regions]
|
|
166
|
+
|
|
74
167
|
def list_objects(self, prefix, batch_size=100):
|
|
75
168
|
"""
|
|
76
169
|
description:
|
|
@@ -310,6 +403,19 @@ class OSSObject:
|
|
|
310
403
|
self.etag = etag
|
|
311
404
|
|
|
312
405
|
|
|
406
|
+
class OSSRegion:
|
|
407
|
+
"""OSS 区域表示类"""
|
|
408
|
+
|
|
409
|
+
def __init__(self, region_id, region_name=None, internal_endpoint=None, internet_endpoint=None):
|
|
410
|
+
self.region_id = region_id
|
|
411
|
+
self.region_name = region_name
|
|
412
|
+
self.internal_endpoint = internal_endpoint
|
|
413
|
+
self.internet_endpoint = internet_endpoint
|
|
414
|
+
|
|
415
|
+
def __repr__(self):
|
|
416
|
+
return f"OSSRegion(region_id='{self.region_id}', region_name='{self.region_name}', internet_endpoint='{self.internet_endpoint}')"
|
|
417
|
+
|
|
418
|
+
|
|
313
419
|
class OSSUtilsAsync:
|
|
314
420
|
def __init__(
|
|
315
421
|
self, access_key_id, access_key_secret, region_id, bucket_name, use_vpc=False
|
|
@@ -440,6 +546,77 @@ class OSSUtilsAsync:
|
|
|
440
546
|
print(f"XML 解析错误: {e}")
|
|
441
547
|
return []
|
|
442
548
|
|
|
549
|
+
async def list_regions(self):
|
|
550
|
+
"""
|
|
551
|
+
description:
|
|
552
|
+
列出OSS支持的区域
|
|
553
|
+
return:
|
|
554
|
+
regions(list): 区域列表,每个元素为字典,包含 region_id, region_name, internal_endpoint, internet_endpoint
|
|
555
|
+
"""
|
|
556
|
+
try:
|
|
557
|
+
# 使用通用的OSS endpoint来获取区域列表
|
|
558
|
+
url = "https://oss-cn-hangzhou.aliyuncs.com/?regions"
|
|
559
|
+
|
|
560
|
+
# 生成签名(对于 list_regions API,不需要 bucket_name)
|
|
561
|
+
# 直接使用空字符串作为 bucket_name,因为 list_regions 不需要 bucket
|
|
562
|
+
headers = {}
|
|
563
|
+
params = {}
|
|
564
|
+
req = oss2.http.Request("GET", url, params=params, headers=headers)
|
|
565
|
+
# 使用空字符串作为 bucket_name,因为 list_regions 不需要 bucket
|
|
566
|
+
self.auth._sign_request(req, "", "")
|
|
567
|
+
signed_headers = {}
|
|
568
|
+
for k, v in req.headers.items():
|
|
569
|
+
if k is not None and v is not None:
|
|
570
|
+
signed_headers[str(k)] = str(v)
|
|
571
|
+
|
|
572
|
+
# 发送请求
|
|
573
|
+
session = await self._ensure_session()
|
|
574
|
+
async with session.get(url, headers=signed_headers) as resp:
|
|
575
|
+
if resp.status != 200:
|
|
576
|
+
resp.raise_for_status()
|
|
577
|
+
|
|
578
|
+
# 解析XML响应
|
|
579
|
+
text = await resp.text()
|
|
580
|
+
root = ET.fromstring(text)
|
|
581
|
+
regions = []
|
|
582
|
+
|
|
583
|
+
# 查找所有 RegionInfo 元素
|
|
584
|
+
for region_info_elem in root.findall(".//RegionInfo"):
|
|
585
|
+
region_elem = region_info_elem.find("Region")
|
|
586
|
+
internal_endpoint_elem = region_info_elem.find("InternalEndpoint")
|
|
587
|
+
internet_endpoint_elem = region_info_elem.find("InternetEndpoint")
|
|
588
|
+
|
|
589
|
+
region_id = region_elem.text if region_elem is not None else ""
|
|
590
|
+
# 从 region_id 中提取区域名称(去掉 oss- 前缀)
|
|
591
|
+
region_name = region_id.replace("oss-", "") if region_id else None
|
|
592
|
+
internal_endpoint = internal_endpoint_elem.text if internal_endpoint_elem is not None else None
|
|
593
|
+
internet_endpoint = internet_endpoint_elem.text if internet_endpoint_elem is not None else None
|
|
594
|
+
|
|
595
|
+
if region_id:
|
|
596
|
+
regions.append({
|
|
597
|
+
"region_id": region_id,
|
|
598
|
+
"region_name": region_name,
|
|
599
|
+
"internal_endpoint": internal_endpoint,
|
|
600
|
+
"internet_endpoint": internet_endpoint
|
|
601
|
+
})
|
|
602
|
+
|
|
603
|
+
self.regions = regions
|
|
604
|
+
return regions
|
|
605
|
+
except Exception as e:
|
|
606
|
+
print(f"list_regions error: {traceback.format_exc()}, {e}")
|
|
607
|
+
self.regions = []
|
|
608
|
+
return []
|
|
609
|
+
|
|
610
|
+
async def list_region_ids(self):
|
|
611
|
+
"""
|
|
612
|
+
description:
|
|
613
|
+
列出OSS支持的区域ID
|
|
614
|
+
return:
|
|
615
|
+
region_ids(list): 区域ID列表
|
|
616
|
+
"""
|
|
617
|
+
regions = await self.list_regions()
|
|
618
|
+
return [region["region_id"] for region in regions]
|
|
619
|
+
|
|
443
620
|
async def list_objects(self, prefix="", max_keys=100):
|
|
444
621
|
"""列出对象,返回所有匹配的对象"""
|
|
445
622
|
session = await self._ensure_session()
|
|
@@ -20,6 +20,7 @@ class Feishu:
|
|
|
20
20
|
"picturetransform服务报警": "https://open.feishu.cn/open-apis/bot/v2/hook/e975aa0a-acef-4e3f-bee4-6dc507b87ebd",
|
|
21
21
|
"cloudstorage服务报警": "https://open.feishu.cn/open-apis/bot/v2/hook/a632d0bc-e400-40ce-a3bb-1b9b9ee634f4",
|
|
22
22
|
"deployengine服务报警": "https://open.feishu.cn/open-apis/bot/v2/hook/9a347a63-58fb-4e10-9a3d-ff4918ddb3a9",
|
|
23
|
+
"browseruse服务报警": "https://open.feishu.cn/open-apis/bot/v2/hook/a7cf22c5-f50d-4031-b17f-a75f92a3aa91",
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
def __init__(
|
|
@@ -571,3 +572,224 @@ class FeishuDocumentManager:
|
|
|
571
572
|
return result, True
|
|
572
573
|
except requests.RequestException as e:
|
|
573
574
|
return {"code": -1, "msg": f"请求失败: {str(e)}"}, False
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+
class FeishuUserManager:
|
|
578
|
+
"""
|
|
579
|
+
飞书用户管理器,用于管理飞书用户的发送消息和查询
|
|
580
|
+
"""
|
|
581
|
+
|
|
582
|
+
def __init__(
|
|
583
|
+
self,
|
|
584
|
+
app_id: Optional[str] = None,
|
|
585
|
+
app_secret: Optional[str] = None,
|
|
586
|
+
):
|
|
587
|
+
"""
|
|
588
|
+
description:
|
|
589
|
+
初始化飞书用户管理器
|
|
590
|
+
parameters:
|
|
591
|
+
app_id(Optional[str]): 飞书应用ID,如果不提供则从settings中读取
|
|
592
|
+
app_secret(Optional[str]): 飞书应用密钥,如果不提供则从settings中读取
|
|
593
|
+
"""
|
|
594
|
+
try:
|
|
595
|
+
from settings import FEISHU_APP_ID, FEISHU_APP_SECRET
|
|
596
|
+
self.app_id = app_id or FEISHU_APP_ID
|
|
597
|
+
self.app_secret = app_secret or FEISHU_APP_SECRET
|
|
598
|
+
except ImportError:
|
|
599
|
+
if not app_id or not app_secret:
|
|
600
|
+
raise ValueError(
|
|
601
|
+
"必须提供app_id和app_secret,或者在settings中配置FEISHU_APP_ID和FEISHU_APP_SECRET"
|
|
602
|
+
)
|
|
603
|
+
self.app_id = app_id
|
|
604
|
+
self.app_secret = app_secret
|
|
605
|
+
|
|
606
|
+
self.base_url = "https://open.feishu.cn/open-apis"
|
|
607
|
+
self._tenant_access_token = None
|
|
608
|
+
self._token_expire_time = 0
|
|
609
|
+
|
|
610
|
+
def _get_tenant_access_token(self) -> str:
|
|
611
|
+
"""
|
|
612
|
+
description:
|
|
613
|
+
获取飞书应用的租户访问令牌(tenant_access_token,带缓存机制)
|
|
614
|
+
return:
|
|
615
|
+
str: 租户访问令牌
|
|
616
|
+
"""
|
|
617
|
+
import time
|
|
618
|
+
|
|
619
|
+
# 如果token未过期,直接返回缓存的token
|
|
620
|
+
if self._tenant_access_token and time.time() < self._token_expire_time:
|
|
621
|
+
return self._tenant_access_token
|
|
622
|
+
|
|
623
|
+
url = f"{self.base_url}/auth/v3/tenant_access_token/internal"
|
|
624
|
+
headers = {"Content-Type": "application/json; charset=utf-8"}
|
|
625
|
+
data = {
|
|
626
|
+
"app_id": self.app_id,
|
|
627
|
+
"app_secret": self.app_secret,
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
try:
|
|
631
|
+
response = requests.post(url, headers=headers, json=data)
|
|
632
|
+
response.raise_for_status()
|
|
633
|
+
result = response.json()
|
|
634
|
+
|
|
635
|
+
if result.get("code") != 0:
|
|
636
|
+
raise Exception(
|
|
637
|
+
f"获取tenant_access_token失败: {result.get('msg', 'unknown error')}"
|
|
638
|
+
)
|
|
639
|
+
|
|
640
|
+
self._tenant_access_token = result.get("tenant_access_token")
|
|
641
|
+
# token有效期默认7200秒,提前300秒刷新
|
|
642
|
+
expire = result.get("expire", 7200)
|
|
643
|
+
self._token_expire_time = time.time() + expire - 300
|
|
644
|
+
|
|
645
|
+
return self._tenant_access_token
|
|
646
|
+
except requests.RequestException as e:
|
|
647
|
+
raise Exception(f"请求tenant_access_token时发生错误: {str(e)}")
|
|
648
|
+
|
|
649
|
+
def _get_headers(self) -> dict:
|
|
650
|
+
"""
|
|
651
|
+
description:
|
|
652
|
+
获取请求头,包含tenant_access_token
|
|
653
|
+
return:
|
|
654
|
+
dict: 请求头字典
|
|
655
|
+
"""
|
|
656
|
+
return {
|
|
657
|
+
"Authorization": f"Bearer {self._get_tenant_access_token()}",
|
|
658
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
def send_message_to_user(
|
|
662
|
+
self,
|
|
663
|
+
receive_id: str,
|
|
664
|
+
text: str,
|
|
665
|
+
receive_id_type: str = "open_id",
|
|
666
|
+
) -> tuple[dict, bool]:
|
|
667
|
+
"""
|
|
668
|
+
description:
|
|
669
|
+
发送消息给指定的飞书用户
|
|
670
|
+
parameters:
|
|
671
|
+
receive_id(str): 接收者ID(用户ID)
|
|
672
|
+
text(str): 要发送的文本内容
|
|
673
|
+
receive_id_type(str): 接收者ID类型,默认为"open_id",可选值:"open_id"、"user_id"、"union_id"、"email"、"chat_id"
|
|
674
|
+
return:
|
|
675
|
+
result(dict): API返回的结果
|
|
676
|
+
success(bool): 是否成功
|
|
677
|
+
"""
|
|
678
|
+
url = f"{self.base_url}/im/v1/messages"
|
|
679
|
+
headers = self._get_headers()
|
|
680
|
+
# receive_id 和 receive_id_type 作为查询参数
|
|
681
|
+
params = {
|
|
682
|
+
"receive_id_type": receive_id_type,
|
|
683
|
+
}
|
|
684
|
+
# 请求体只包含消息类型和内容
|
|
685
|
+
data = {
|
|
686
|
+
"receive_id": receive_id,
|
|
687
|
+
"msg_type": "text",
|
|
688
|
+
"content": json.dumps({"text": text}),
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
try:
|
|
692
|
+
response = requests.post(url, headers=headers, params=params, json=data)
|
|
693
|
+
response.raise_for_status()
|
|
694
|
+
result = response.json()
|
|
695
|
+
|
|
696
|
+
if result.get("code") != 0:
|
|
697
|
+
return result, False
|
|
698
|
+
|
|
699
|
+
return result, True
|
|
700
|
+
except requests.RequestException as e:
|
|
701
|
+
# 尝试获取更详细的错误信息
|
|
702
|
+
error_detail = {}
|
|
703
|
+
if hasattr(e, 'response') and e.response is not None:
|
|
704
|
+
try:
|
|
705
|
+
error_detail = e.response.json()
|
|
706
|
+
except:
|
|
707
|
+
error_detail = {"text": e.response.text if hasattr(e.response, 'text') else str(e)}
|
|
708
|
+
return {"code": -1, "msg": f"请求失败: {str(e)}", "detail": error_detail}, False
|
|
709
|
+
|
|
710
|
+
async def send_message_to_user_async(
|
|
711
|
+
self,
|
|
712
|
+
receive_id: str,
|
|
713
|
+
text: str,
|
|
714
|
+
receive_id_type: str = "open_id",
|
|
715
|
+
) -> tuple[dict, bool]:
|
|
716
|
+
"""
|
|
717
|
+
description:
|
|
718
|
+
异步发送消息给指定的飞书用户
|
|
719
|
+
parameters:
|
|
720
|
+
receive_id(str): 接收者ID(用户ID)
|
|
721
|
+
text(str): 要发送的文本内容
|
|
722
|
+
receive_id_type(str): 接收者ID类型,默认为"open_id"
|
|
723
|
+
return:
|
|
724
|
+
result(dict): API返回的结果
|
|
725
|
+
success(bool): 是否成功
|
|
726
|
+
"""
|
|
727
|
+
return await asyncio.to_thread(
|
|
728
|
+
self.send_message_to_user, receive_id, text, receive_id_type
|
|
729
|
+
)
|
|
730
|
+
|
|
731
|
+
def list_users(
|
|
732
|
+
self,
|
|
733
|
+
department_id: Optional[str] = None,
|
|
734
|
+
page_size: int = 50,
|
|
735
|
+
page_token: Optional[str] = None,
|
|
736
|
+
user_id_type: str = "open_id",
|
|
737
|
+
) -> tuple[dict, bool]:
|
|
738
|
+
"""
|
|
739
|
+
description:
|
|
740
|
+
列出飞书中的所有用户(支持按部门筛选)
|
|
741
|
+
parameters:
|
|
742
|
+
department_id(Optional[str]): 部门ID,如果提供则只返回该部门的用户
|
|
743
|
+
page_size(int): 每页数量,默认50,最大100
|
|
744
|
+
page_token(Optional[str]): 分页token,用于获取下一页
|
|
745
|
+
user_id_type(str): 用户ID类型,默认为"open_id",可选值:"open_id"、"user_id"、"union_id"
|
|
746
|
+
return:
|
|
747
|
+
result(dict): API返回的结果,包含用户列表
|
|
748
|
+
success(bool): 是否成功
|
|
749
|
+
"""
|
|
750
|
+
url = f"{self.base_url}/contact/v3/users"
|
|
751
|
+
headers = self._get_headers()
|
|
752
|
+
params = {
|
|
753
|
+
"page_size": min(page_size, 100), # 限制最大值为100
|
|
754
|
+
"user_id_type": user_id_type,
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
if department_id:
|
|
758
|
+
params["department_id"] = department_id
|
|
759
|
+
if page_token:
|
|
760
|
+
params["page_token"] = page_token
|
|
761
|
+
|
|
762
|
+
try:
|
|
763
|
+
response = requests.get(url, headers=headers, params=params)
|
|
764
|
+
response.raise_for_status()
|
|
765
|
+
result = response.json()
|
|
766
|
+
|
|
767
|
+
if result.get("code") != 0:
|
|
768
|
+
return result, False
|
|
769
|
+
|
|
770
|
+
return result, True
|
|
771
|
+
except requests.RequestException as e:
|
|
772
|
+
return {"code": -1, "msg": f"请求失败: {str(e)}"}, False
|
|
773
|
+
|
|
774
|
+
async def list_users_async(
|
|
775
|
+
self,
|
|
776
|
+
department_id: Optional[str] = None,
|
|
777
|
+
page_size: int = 50,
|
|
778
|
+
page_token: Optional[str] = None,
|
|
779
|
+
user_id_type: str = "open_id",
|
|
780
|
+
) -> tuple[dict, bool]:
|
|
781
|
+
"""
|
|
782
|
+
description:
|
|
783
|
+
异步列出飞书中的所有用户
|
|
784
|
+
parameters:
|
|
785
|
+
department_id(Optional[str]): 部门ID,如果提供则只返回该部门的用户
|
|
786
|
+
page_size(int): 每页数量,默认50
|
|
787
|
+
page_token(Optional[str]): 分页token,用于获取下一页
|
|
788
|
+
user_id_type(str): 用户ID类型,默认为"open_id"
|
|
789
|
+
return:
|
|
790
|
+
result(dict): API返回的结果,包含用户列表
|
|
791
|
+
success(bool): 是否成功
|
|
792
|
+
"""
|
|
793
|
+
return await asyncio.to_thread(
|
|
794
|
+
self.list_users, department_id, page_size, page_token, user_id_type
|
|
795
|
+
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|