diff --git a/README.md b/README.md index f310419..247ba7a 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ - [ ] 企业微信 - [x] [Telegram](https://github.com/zhayujie/bot-on-anything#6telegram) - [x] [QQ](https://github.com/zhayujie/bot-on-anything#5qq) - - [ ] 钉钉 + - [x] 钉钉 - [ ] 飞书 - [x] [Gmail](https://github.com/zhayujie/bot-on-anything#7gmail) - [x] [Slack](https://github.com/zhayujie/bot-on-anything#8slack) @@ -503,13 +503,13 @@ pip3 install requests flask ```bash "channel": { - "type": "dingding", - "dingding": { + "type": "dingtalk", + "dingtalk": { "image_create_prefix": ["画", "draw", "Draw"], "port": "8081", //对外端口 - "dd_token": "xx", //webhook地址的access_token - "dd_post_token": "xx", //钉钉post回消息时header中带的检验token - "dd_secret": "xx"// 安全加密加签串,群机器人中 + "dingtalk_token": "xx", //webhook地址的access_token + "dingtalk_post_token": "xx", //钉钉post回消息时header中带的检验token + "dingtalk_secret": "xx"// 安全加密加签串,群机器人中 } } ``` diff --git a/channel/channel_factory.py b/channel/channel_factory.py index 2c0bde3..78a1a49 100644 --- a/channel/channel_factory.py +++ b/channel/channel_factory.py @@ -45,9 +45,9 @@ def create_channel(channel_type): from channel.http.http_channel import HttpChannel return HttpChannel() - elif channel_type == const.DINGDING: - from channel.dd.dd_channel import DDChannel - return DDChannel() + elif channel_type == const.DINGTALK: + from channel.dingtalk.dingtalk_channel import DingTalkChannel + return DingTalkChannel() else: raise RuntimeError("unknown channel_type in config.json: " + channel_type) diff --git a/channel/dd/dd_channel.py b/channel/dingtalk/dingtalk_channel.py similarity index 69% rename from channel/dd/dd_channel.py rename to channel/dingtalk/dingtalk_channel.py index 6664daa..47c5d7f 100644 --- a/channel/dd/dd_channel.py +++ b/channel/dingtalk/dingtalk_channel.py @@ -14,18 +14,18 @@ from config import channel_conf from config import channel_conf_val from channel.channel import Channel -class DDChannel(Channel): +class DingTalkChannel(Channel): def __init__(self): - self.dd_token = channel_conf(const.DINGDING).get('dd_token') - self.dd_post_token = channel_conf(const.DINGDING).get('dd_post_token') - self.dd_secret = channel_conf(const.DINGDING).get('dd_secret') - log.info("[DingDing] dd_secret={}, dd_token={} dd_post_token={}".format(self.dd_secret, self.dd_token, self.dd_post_token)) + self.dingtalk_token = channel_conf(const.DINGTALK).get('dingtalk_token') + self.dingtalk_post_token = channel_conf(const.DINGTALK).get('dingtalk_post_token') + self.dingtalk_secret = channel_conf(const.DINGTALK).get('dingtalk_secret') + log.info("[DingTalk] dingtalk_secret={}, dingtalk_token={} dingtalk_post_token={}".format(self.dingtalk_secret, self.dingtalk_token, self.dingtalk_post_token)) def startup(self): - http_app.run(host='0.0.0.0', port=channel_conf(const.DINGDING).get('port')) + http_app.run(host='0.0.0.0', port=channel_conf(const.DINGTALK).get('port')) - def notify_dingding(self, answer): + def notify_dingtalk(self, answer): data = { "msgtype": "text", "text": { @@ -41,18 +41,18 @@ class DDChannel(Channel): } timestamp = round(time.time() * 1000) - secret_enc = bytes(self.dd_secret, encoding='utf-8') - string_to_sign = '{}\n{}'.format(timestamp, self.dd_secret) + secret_enc = bytes(self.dingtalk_secret, encoding='utf-8') + string_to_sign = '{}\n{}'.format(timestamp, self.dingtalk_secret) string_to_sign_enc = bytes(string_to_sign, encoding='utf-8') hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest() sign = quote_plus(base64.b64encode(hmac_code)) - notify_url = f"https://oapi.dingtalk.com/robot/send?access_token={self.dd_token}×tamp={timestamp}&sign={sign}" + notify_url = f"https://oapi.dingtalk.com/robot/send?access_token={self.dingtalk_token}×tamp={timestamp}&sign={sign}" try: r = requests.post(notify_url, json=data) reply = r.json() - # log.info("[DingDing] reply={}".format(str(reply))) + # log.info("[DingTalk] reply={}".format(str(reply))) except Exception as e: log.error(e) @@ -62,7 +62,7 @@ class DDChannel(Channel): sender_id = data['senderId'] context = dict() img_match_prefix = functions.check_prefix( - prompt, channel_conf_val(const.DINGDING, 'image_create_prefix')) + prompt, channel_conf_val(const.DINGTALK, 'image_create_prefix')) if img_match_prefix: prompt = prompt.split(img_match_prefix, 1)[1].strip() context['type'] = 'IMAGE_CREATE' @@ -79,24 +79,23 @@ class DDChannel(Channel): return reply -dd = DDChannel() +dd = DingTalkChannel() http_app = Flask(__name__,) @http_app.route("/", methods=['POST']) def chat(): - # log.info("[DingDing] chat_headers={}".format(str(request.headers))) - log.info("[DingDing] chat={}".format(str(request.data))) + # log.info("[DingTalk] chat_headers={}".format(str(request.headers))) + log.info("[DingTalk] chat={}".format(str(request.data))) token = request.headers.get('token') - if dd.dd_post_token and token != dd.dd_post_token: + if dd.dingtalk_post_token and token != dd.dingtalk_post_token: return {'ret': 203} - #TODO: Verify identity data = json.loads(request.data) if data: content = data['text']['content'] if not content: return reply_text = dd.handle(data=data) - dd.notify_dingding(reply_text) + dd.notify_dingtalk(reply_text) return {'ret': 200} return {'ret': 201} diff --git a/common/const.py b/common/const.py index 95e10bc..6324c82 100644 --- a/common/const.py +++ b/common/const.py @@ -8,7 +8,7 @@ GMAIL = "gmail" TELEGRAM = "telegram" SLACK = "slack" HTTP = "http" -DINGDING = "dingding" +DINGTALK = "dingtalk" # model OPEN_AI = "openai" diff --git a/config-template.json b/config-template.json index 4cb73cd..c0aea2f 100644 --- a/config-template.json +++ b/config-template.json @@ -60,12 +60,12 @@ "port": "80" }, - "dingding": { + "dingtalk": { "image_create_prefix": ["画", "draw", "Draw"], "port": "8081", - "dd_token": "xx", - "dd_post_token": "xx", - "dd_secret": "xx" + "dingtalk_token": "xx", + "dingtalk_post_token": "xx", + "dingtalk_secret": "xx" } }, "common": {