Skip to content

Commit

Permalink
support batch send message and more msg_type (#54)
Browse files Browse the repository at this point in the history
* support batch send message and more msg_type

* bump version
  • Loading branch information
LeoQuote authored Nov 15, 2021
1 parent 58b6603 commit aeab070
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
47 changes: 37 additions & 10 deletions sa_tools_core/libs/notify/lark.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import json
import logging
from typing import Union

import pylark

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

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


def send_message(addrs, content, **kwargs):
def send_message(addrs: [str], content: str, **kwargs):
"""
发送消息
:param addrs: 接收人的邮箱地址, 或部门id
:param content: 消息内容, 可以是字符串, 也可以是json格式的字符串
:param kwargs: 其他参数
:return: None
"""
secret = get_config("lark-multi-tenant")
# config template:
# {
Expand All @@ -51,12 +59,31 @@ def send_message(addrs, content, **kwargs):
lark_bundle = LarkApp.load_from_configs(configs=json.loads(secret))
company = kwargs.get("company", DEFAULT_LARK_TENANT) or DEFAULT_LARK_TENANT
lark = lark_bundle.get_lark(company=company)
for addr in addrs:
res, response = lark.message.send_raw_message(
pylark.SendRawMessageReq(
receive_id_type="email",
receive_id=addr,
content=json.dumps({"text": content}),
msg_type="text",
address_type = kwargs.get("address_type", "email")
msg_type = kwargs.get("msg_type", "text")
if address_type == "email":
if msg_type == "text":
content = json.dumps({"text": content})
for addr in addrs:
lark.message.send_raw_message(
pylark.SendRawMessageReq(
receive_id_type="email",
receive_id=addr,
content=content,
msg_type=msg_type,
)
)
)

return
if address_type == "department_id":
if msg_type == "text":
content = {"text": content}
else:
content = json.loads(content)
lark.message.batch_send_old_raw_message(pylark.BatchSendOldRawMessageReq(
department_ids=addrs,
content=content,
msg_type=msg_type,
))
return

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from io import open
from setuptools import setup, find_packages

version = '0.5.1'
version = '0.6.0'

requirements = [
'setuptools',
Expand Down

0 comments on commit aeab070

Please sign in to comment.