Skip to content

Commit

Permalink
add lark multi tenant support (#50)
Browse files Browse the repository at this point in the history
* bump version

add lark multi tenant support

* delete unused init function
  • Loading branch information
LeoQuote authored Nov 3, 2021
1 parent 2428de0 commit 3e5c494
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ local_config.py
/build

.vscode/

secrets
2 changes: 2 additions & 0 deletions local_config.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ PROXIES = {
# can be a string pattern which contains {cb_token}
DNS_MONITOR_CALLBACK_URL = 'http://example.com/api/callback/dnsmonitor/{cb_token}'
DEFAULT_DNS_DOMAIN = 'example.com'

DEFAULT_LARK_TENANT = "your_company"
2 changes: 2 additions & 0 deletions sa_tools_core/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
"https": "http://gfw:2333",
}

DEFAULT_LARK_TENANT = "your_company"

########################
# # Elasticsearch
########################
Expand Down
41 changes: 36 additions & 5 deletions sa_tools_core/libs/notify/lark.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,51 @@
import pylark

from sa_tools_core.utils import get_config
from sa_tools_core.consts import DEFAULT_LARK_TENANT

logger = logging.getLogger(__name__)

APP_ID = "aaaaaaaaaaaaaaaaaa"
APP_SECRET = "bbbbbbbbbbbbbbbbbbbbbb"


class LarkApp(object):
tenants = {}

@staticmethod
def load_from_configs(configs: dict):
app = LarkApp()
for name, config in configs.items():
lark = pylark.Lark(
app_id=config["app_id"],
app_secret=config["app_secret"]
)
app.tenants[name] = lark
return app

def get_lark(self, company="douban"):
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):
secret = get_config("lark").split(":")
APP_ID = secret[0]
APP_SECRET = secret[1]
cli = pylark.Lark(app_id=APP_ID, app_secret=APP_SECRET)
secret = get_config("lark-multi-tenant")
# config template:
# {
# "your_company": {
# "app_id": "1",
# "app_secret": "1",
# "encrypt_key": "1",
# "verification_token": "1"
# }
# }
lark_bundle = LarkApp.load_from_configs(configs=json.loads(secret))
company = kwargs.get("company", DEFAULT_LARK_TENANT)
lark = lark_bundle.get_lark(company=company)
for addr in addrs:
res, response = cli.message.send_raw_message(
res, response = lark.message.send_raw_message(
pylark.SendRawMessageReq(
receive_id_type="email",
receive_id=addr,
Expand Down
5 changes: 3 additions & 2 deletions sa_tools_core/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def _wework(self, addrs, content=None, **kw):
send_wework(addrs, content, msg_type=self.msg_type)

def _lark(self, addrs, content=None, **kw):
send_lark(addrs, content)
send_lark(addrs, content, **kw)

def _email(self, addrs, content=None, title=None, from_addr=None, **kw):
content = content if content is not None else self.content
Expand Down Expand Up @@ -99,7 +99,7 @@ def notify(args):
if values:
addrs = [i for v in values for i in re.split(r"[,\s]+", v)]
try:
getattr(notifier, type_)(addrs, from_addr=args.from_addr)
getattr(notifier, type_)(addrs, from_addr=args.from_addr, company=args.company)
except Exception as e:
logger.exception("Notifier.%s(%s) failed: %s", type_, addrs, e)
report()
Expand All @@ -120,6 +120,7 @@ def main(args=None):
"-s", "--subject", help='subject. default="%(default)s"', default=DEFAULT_TITLE
)
parser.add_argument("-f", "--from-addr", help="From address, currently only works for email.")
parser.add_argument("--company", help="Company user in, used when multiple company or tenant is configured.")
for type_ in NOTIFY_TYPES:
parser.add_argument(
"--%s" % type_, nargs="*", help="your enterprise address of %s." % type_
Expand Down
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.4.4'
version = '0.5.0'

requirements = [
'setuptools',
Expand Down

0 comments on commit 3e5c494

Please sign in to comment.