From 973849540a34405471bdc58107bbb86eb020b329 Mon Sep 17 00:00:00 2001 From: Ching Date: Tue, 26 Oct 2021 15:57:06 +0800 Subject: [PATCH] feat(utils lark client): [A] add lark webhook client [A] add lark webhook client Signed-off-by: Ching --- dsite/settings.py | 45 --------------------------------------- dsite/settings_default.py | 4 ++++ utils/const.py | 3 +++ utils/lark.py | 37 ++++++++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 45 deletions(-) delete mode 100644 dsite/settings.py create mode 100644 utils/lark.py diff --git a/dsite/settings.py b/dsite/settings.py deleted file mode 100644 index dbe3d83..0000000 --- a/dsite/settings.py +++ /dev/null @@ -1,45 +0,0 @@ -from .settings_default import * - -ALLOWED_HOSTS = ['*'] - -CORS_ALLOW_CREDENTIALS = True # 允许携带cookie -# CORS_ALLOW_ALL_ORIGINS = True # If this is used then `CORS_ALLOWED_ORIGINS` will not have any effect -CORS_ALLOWED_ORIGINS = [ - 'http://localhost:8080', - 'http://127.0.0.1:8080', - 'http://192.168.1.101:8080' -] - -CORS_ALLOW_METHODS = [ - "DELETE", - "GET", - "OPTIONS", - "PATCH", - "POST", - "PUT", -] - -CORS_ALLOW_HEADERS = [ - "accept", - "accept-encoding", - "authorization", - "content-type", - "dnt", - "origin", - "user-agent", - "x-csrftoken", - "x-requested-with", -] - -CORS_ALLOW_HEADERS = ('*') - -BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) -PROJECT_DIR = os.path.dirname(os.path.abspath(__file__)) -STATIC_ROOT = os.path.join(BASE_DIR, 'static') - -STATICFILES_DIRS = [ - os.path.join(BASE_DIR, "frontend/dist/"), - ] - -MEDIA_URL = '/media/' -MEDIA_ROOT = os.path.join(BASE_DIR, 'media') diff --git a/dsite/settings_default.py b/dsite/settings_default.py index b4a9f57..825719b 100644 --- a/dsite/settings_default.py +++ b/dsite/settings_default.py @@ -155,3 +155,7 @@ REST_FRAMEWORK = { CORS_ALLOWED_ORIGINS = [] CORS_ALLOW_CREDENTIALS = True # 允许携带cookie # CORS_ALLOW_ALL_ORIGINS = False # If this is used then `CORS_ALLOWED_ORIGINS` will not have any effect + + +LARK_WEBHOOK_URL = '' +LARK_WEBHOOK_SECRET = '' diff --git a/utils/const.py b/utils/const.py index 618f1dd..3dadd28 100644 --- a/utils/const.py +++ b/utils/const.py @@ -19,3 +19,6 @@ RECIPE_TYPE_CHOICE = [ FILTER_EXACT = ['exact'] FILTER_GTE_LTE = ['exact', 'gte', 'gt', 'lte', 'lt'] + + +LARK_WEBHOOK_MSG_TYPE_TEXT = 'text' diff --git a/utils/lark.py b/utils/lark.py new file mode 100644 index 0000000..f9f3c92 --- /dev/null +++ b/utils/lark.py @@ -0,0 +1,37 @@ +# -*- coding: UTF-8 -*- + +from dsite import settings +import utils +from utils import const + +import hashlib +import base64 +import hmac +import json +import requests +from django.utils.timezone import now + + +def gen_sign(timestamp, secret): + # 拼接timestamp和secret + string_to_sign = '{}\n{}'.format(timestamp, secret) + hmac_code = hmac.new(string_to_sign.encode("utf-8"), digestmod=hashlib.sha256).digest() + + # 对结果进行base64处理 + sign = base64.b64encode(hmac_code).decode('utf-8') + + return sign + +def request(content, msg_type=const.LARK_WEBHOOK_MSG_TYPE_TEXT): + """ content: {'text': 'xxxxx} + """ + timestamp = utils.timestamp_of(now()) + data = { + "timestamp": timestamp, + "sign": gen_sign(timestamp, settings.LARK_WEBHOOK_SECRET), + "msg_type": msg_type, + "content": content} + resp = requests.post(settings.LARK_WEBHOOK_URL, data=json.dumps(data)) + if resp.status_code == 200 and resp.json().get('StatusCode') == 0: + return True + return False