Переглянути джерело

钉钉app_id,变更为_client_id,和逻辑优化

master
huiwen 11 місяці тому
джерело
коміт
a21dd97786
3 змінених файлів з 58 додано та 44 видалено
  1. +54
    -41
      channel/dingtalk/dingtalk_channel.py
  2. +2
    -1
      channel/dingtalk/dingtalk_message.py
  3. +2
    -2
      config.py

+ 54
- 41
channel/dingtalk/dingtalk_channel.py Переглянути файл

@@ -32,8 +32,8 @@ import dingtalk_stream

@singleton
class DingTalkChanel(ChatChannel,dingtalk_stream.ChatbotHandler):
dingtalk_app_id = conf().get('dingtalk_app_id')
dingtalk_app_secret = conf().get('dingtalk_app_secret')
dingtalk_client_id = conf().get('dingtalk_client_id')
dingtalk_client_secret = conf().get('dingtalk_client_secret')
def setup_logger(self):
logger = logging.getLogger()
@@ -51,15 +51,16 @@ class DingTalkChanel(ChatChannel,dingtalk_stream.ChatbotHandler):
# 历史消息id暂存,用于幂等控制
self.receivedMsgs = ExpiredDict(60 * 60 * 7.1)
logger.info("[dingtalk] app_id={}, app_secret={} ".format(
self.dingtalk_app_id, self.dingtalk_app_secret))
logger.info("[dingtalk] client_id={}, client_secret={} ".format(
self.dingtalk_client_id, self.dingtalk_client_secret))
# 无需群校验和前缀
conf()["group_name_white_list"] = ["ALL_GROUP"]
conf()["single_chat_prefix"] = []

def startup(self):
credential = dingtalk_stream.Credential( self.dingtalk_app_id, self.dingtalk_app_secret)
credential = dingtalk_stream.Credential( self.dingtalk_client_id, self.dingtalk_client_secret)
client = dingtalk_stream.DingTalkStreamClient(credential)
client.register_callback_handler(dingtalk_stream.chatbot.ChatbotMessage.TOPIC,self)
client.start_forever()
@@ -78,11 +79,32 @@ class DingTalkChanel(ChatChannel,dingtalk_stream.ChatbotHandler):
elif cmsg.ctype == ContextType.TEXT:
expression = cmsg.my_msg
cmsg.content = conf()["single_chat_prefix"][0] + cmsg.content
context = self._compose_context(cmsg.ctype, cmsg.content, isgroup=False, msg=cmsg)
if context:
self.produce(context)

def handle_group(self, cmsg:DingTalkMessage):
# 处理群聊消息
#
if cmsg.ctype == ContextType.VOICE:
logger.debug("[dingtalk]receive voice msg: {}".format(cmsg.content))
elif cmsg.ctype == ContextType.IMAGE:
logger.debug("[dingtalk]receive image msg: {}".format(cmsg.content))
elif cmsg.ctype == ContextType.PATPAT:
logger.debug("[dingtalk]receive patpat msg: {}".format(cmsg.content))
elif cmsg.ctype == ContextType.TEXT:
expression = cmsg.my_msg
cmsg.content = conf()["group_chat_prefix"][0] + cmsg.content
context = self._compose_context(cmsg.ctype, cmsg.content, isgroup=True, msg=cmsg)
context['no_need_at']=True
if context:
self.produce(context)


async def process(self, callback: dingtalk_stream.CallbackMessage):
@@ -96,7 +118,7 @@ class DingTalkChanel(ChatChannel,dingtalk_stream.ChatbotHandler):
if incoming_message.conversation_type == '1':
self.handle_single(dingtalk_msg)
else:
self.handle_single(dingtalk_msg)
self.handle_group(dingtalk_msg)
return AckMessage.STATUS_OK, 'OK'
except Exception as e:
logger.error(e)
@@ -111,41 +133,32 @@ class DingTalkChanel(ChatChannel,dingtalk_stream.ChatbotHandler):

class dingtalkController:
# 类常量
FAILED_MSG = '{"success": false}'
SUCCESS_MSG = '{"success": true}'
MESSAGE_RECEIVE_TYPE = "im.message.receive_v1"

def GET(self):
return "dingtalk service start success!"

# def _compose_context(self, ctype: ContextType, content, **kwargs):
# context = Context(ctype, content)
# context.kwargs = kwargs
# if "origin_ctype" not in context:
# context["origin_ctype"] = ctype

def _compose_context(self, ctype: ContextType, content, **kwargs):
context = Context(ctype, content)
context.kwargs = kwargs
if "origin_ctype" not in context:
context["origin_ctype"] = ctype

cmsg = context["msg"]
context["session_id"] = cmsg.from_user_id
context["receiver"] = cmsg.other_user_id

if ctype == ContextType.TEXT:
# 1.文本请求
# 图片生成处理
img_match_prefix = check_prefix(content, conf().get("image_create_prefix"))
if img_match_prefix:
content = content.replace(img_match_prefix, "", 1)
context.type = ContextType.IMAGE_CREATE
else:
context.type = ContextType.TEXT
context.content = content.strip()
# cmsg = context["msg"]
# context["session_id"] = cmsg.from_user_id
# context["receiver"] = cmsg.other_user_id

# if ctype == ContextType.TEXT:
# # 1.文本请求
# # 图片生成处理
# img_match_prefix = check_prefix(content, conf().get("image_create_prefix"))
# if img_match_prefix:
# content = content.replace(img_match_prefix, "", 1)
# context.type = ContextType.IMAGE_CREATE
# else:
# context.type = ContextType.TEXT
# context.content = content.strip()

elif context.type == ContextType.VOICE:
# 2.语音请求
if "desire_rtype" not in context and conf().get("voice_reply_voice"):
context["desire_rtype"] = ReplyType.VOICE
# elif context.type == ContextType.VOICE:
# # 2.语音请求
# if "desire_rtype" not in context and conf().get("voice_reply_voice"):
# context["desire_rtype"] = ReplyType.VOICE

return context
# return context

+ 2
- 1
channel/dingtalk/dingtalk_message.py Переглянути файл

@@ -30,7 +30,8 @@ class DingTalkMessage(ChatMessage):
self.from_user_id = event.sender_id
self.to_user_id = event.chatbot_user_id

self.other_user_nickname = event.conversation_title
user_id = event.sender_id
nickname =event.sender_nick



+ 2
- 2
config.py Переглянути файл

@@ -125,8 +125,8 @@ available_setting = {
"feishu_bot_name": "", # 飞书机器人的名字
# 钉钉配置
"dingtalk_app_id": "", # 钉钉机器人应用APP Id
"dingtalk_app_secret": "", # 钉钉机器人APP secret
"dingtalk_client_id": "", # 钉钉机器人Client ID
"dingtalk_client_secret": "", # 钉钉机器人Client Secret
# chatgpt指令自定义触发词
"clear_memory_commands": ["#清除记忆"], # 重置会话指令,必须以#开头


Завантаження…
Відмінити
Зберегти