mirror of
https://github.com/zhayujie/bot-on-anything.git
synced 2026-02-10 16:22:07 +08:00
rename dingding to dingtalk
This commit is contained in:
12
README.md
12
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"// 安全加密加签串,群机器人中
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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}
|
||||
@@ -8,7 +8,7 @@ GMAIL = "gmail"
|
||||
TELEGRAM = "telegram"
|
||||
SLACK = "slack"
|
||||
HTTP = "http"
|
||||
DINGDING = "dingding"
|
||||
DINGTALK = "dingtalk"
|
||||
|
||||
# model
|
||||
OPEN_AI = "openai"
|
||||
|
||||
@@ -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": {
|
||||
|
||||
Reference in New Issue
Block a user