4
4
5
5
import json
6
6
import logging
7
+ from typing import Union
7
8
8
9
import pylark
9
10
@@ -30,14 +31,21 @@ def load_from_configs(configs: dict):
30
31
app .tenants [name ] = lark
31
32
return app
32
33
33
- def get_lark (self , company = "douban" ):
34
+ def get_lark (self , company = "douban" ) -> Union [ pylark . Lark , None ] :
34
35
lark = self .tenants .get (company )
35
36
if lark :
36
37
return lark
37
38
logger .error (f"no tenant named { company } , please check your code and configs" )
38
39
39
40
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
+ """
41
49
secret = get_config ("lark-multi-tenant" )
42
50
# config template:
43
51
# {
@@ -51,12 +59,31 @@ def send_message(addrs, content, **kwargs):
51
59
lark_bundle = LarkApp .load_from_configs (configs = json .loads (secret ))
52
60
company = kwargs .get ("company" , DEFAULT_LARK_TENANT ) or DEFAULT_LARK_TENANT
53
61
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
+ )
61
75
)
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
+
0 commit comments