Skip to content

Commit aeab070

Browse files
authored
support batch send message and more msg_type (#54)
* support batch send message and more msg_type * bump version
1 parent 58b6603 commit aeab070

File tree

2 files changed

+38
-11
lines changed

2 files changed

+38
-11
lines changed

sa_tools_core/libs/notify/lark.py

+37-10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import json
66
import logging
7+
from typing import Union
78

89
import pylark
910

@@ -30,14 +31,21 @@ def load_from_configs(configs: dict):
3031
app.tenants[name] = lark
3132
return app
3233

33-
def get_lark(self, company="douban"):
34+
def get_lark(self, company="douban") -> Union[pylark.Lark, None]:
3435
lark = self.tenants.get(company)
3536
if lark:
3637
return lark
3738
logger.error(f"no tenant named {company}, please check your code and configs")
3839

3940

40-
def send_message(addrs, content, **kwargs):
41+
def send_message(addrs: [str], content: str, **kwargs):
42+
"""
43+
发送消息
44+
:param addrs: 接收人的邮箱地址, 或部门id
45+
:param content: 消息内容, 可以是字符串, 也可以是json格式的字符串
46+
:param kwargs: 其他参数
47+
:return: None
48+
"""
4149
secret = get_config("lark-multi-tenant")
4250
# config template:
4351
# {
@@ -51,12 +59,31 @@ def send_message(addrs, content, **kwargs):
5159
lark_bundle = LarkApp.load_from_configs(configs=json.loads(secret))
5260
company = kwargs.get("company", DEFAULT_LARK_TENANT) or DEFAULT_LARK_TENANT
5361
lark = lark_bundle.get_lark(company=company)
54-
for addr in addrs:
55-
res, response = lark.message.send_raw_message(
56-
pylark.SendRawMessageReq(
57-
receive_id_type="email",
58-
receive_id=addr,
59-
content=json.dumps({"text": content}),
60-
msg_type="text",
62+
address_type = kwargs.get("address_type", "email")
63+
msg_type = kwargs.get("msg_type", "text")
64+
if address_type == "email":
65+
if msg_type == "text":
66+
content = json.dumps({"text": content})
67+
for addr in addrs:
68+
lark.message.send_raw_message(
69+
pylark.SendRawMessageReq(
70+
receive_id_type="email",
71+
receive_id=addr,
72+
content=content,
73+
msg_type=msg_type,
74+
)
6175
)
62-
)
76+
77+
return
78+
if address_type == "department_id":
79+
if msg_type == "text":
80+
content = {"text": content}
81+
else:
82+
content = json.loads(content)
83+
lark.message.batch_send_old_raw_message(pylark.BatchSendOldRawMessageReq(
84+
department_ids=addrs,
85+
content=content,
86+
msg_type=msg_type,
87+
))
88+
return
89+

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from io import open
44
from setuptools import setup, find_packages
55

6-
version = '0.5.1'
6+
version = '0.6.0'
77

88
requirements = [
99
'setuptools',

0 commit comments

Comments
 (0)