您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

linkai_client.py 3.9KB

10 个月前
10 个月前
10 个月前
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. from bridge.context import Context, ContextType
  2. from bridge.reply import Reply, ReplyType
  3. from common.log import logger
  4. from linkai import LinkAIClient, PushMsg
  5. from config import conf, pconf, plugin_config, available_setting
  6. from plugins import PluginManager
  7. import time
  8. chat_client: LinkAIClient
  9. class ChatClient(LinkAIClient):
  10. def __init__(self, api_key, host, channel):
  11. super().__init__(api_key, host)
  12. self.channel = channel
  13. self.client_type = channel.channel_type
  14. def on_message(self, push_msg: PushMsg):
  15. session_id = push_msg.session_id
  16. msg_content = push_msg.msg_content
  17. logger.info(f"receive msg push, session_id={session_id}, msg_content={msg_content}")
  18. context = Context()
  19. context.type = ContextType.TEXT
  20. context["receiver"] = session_id
  21. context["isgroup"] = push_msg.is_group
  22. self.channel.send(Reply(ReplyType.TEXT, content=msg_content), context)
  23. def on_config(self, config: dict):
  24. if not self.client_id:
  25. return
  26. logger.info(f"[LinkAI] 从客户端管理加载远程配置: {config}")
  27. if config.get("enabled") != "Y":
  28. return
  29. local_config = conf()
  30. for key in config.keys():
  31. if key in available_setting and config.get(key) is not None:
  32. local_config[key] = config.get(key)
  33. # 语音配置
  34. reply_voice_mode = config.get("reply_voice_mode")
  35. if reply_voice_mode:
  36. if reply_voice_mode == "voice_reply_voice":
  37. local_config["voice_reply_voice"] = True
  38. elif reply_voice_mode == "always_reply_voice":
  39. local_config["always_reply_voice"] = True
  40. if config.get("admin_password") and plugin_config["Godcmd"]:
  41. plugin_config["Godcmd"]["password"] = config.get("admin_password")
  42. PluginManager().instances["GODCMD"].reload()
  43. if config.get("group_app_map") and pconf("linkai"):
  44. local_group_map = {}
  45. for mapping in config.get("group_app_map"):
  46. local_group_map[mapping.get("group_name")] = mapping.get("app_code")
  47. pconf("linkai")["group_app_map"] = local_group_map
  48. PluginManager().instances["LINKAI"].reload()
  49. def start(channel):
  50. global chat_client
  51. chat_client = ChatClient(api_key=conf().get("linkai_api_key"), host="", channel=channel)
  52. chat_client.config = _build_config()
  53. chat_client.start()
  54. time.sleep(1.5)
  55. if chat_client.client_id:
  56. logger.info("[LinkAI] 可前往控制台进行线上登录和配置:https://link-ai.tech/console/clients")
  57. def _build_config():
  58. local_conf = conf()
  59. config = {
  60. "linkai_app_code": local_conf.get("linkai_app_code"),
  61. "single_chat_prefix": local_conf.get("single_chat_prefix"),
  62. "single_chat_reply_prefix": local_conf.get("single_chat_reply_prefix"),
  63. "single_chat_reply_suffix": local_conf.get("single_chat_reply_suffix"),
  64. "group_chat_prefix": local_conf.get("group_chat_prefix"),
  65. "group_chat_reply_prefix": local_conf.get("group_chat_reply_prefix"),
  66. "group_chat_reply_suffix": local_conf.get("group_chat_reply_suffix"),
  67. "group_name_white_list": local_conf.get("group_name_white_list"),
  68. "nick_name_black_list": local_conf.get("nick_name_black_list"),
  69. "speech_recognition": "Y" if local_conf.get("speech_recognition") else "N",
  70. "text_to_image": local_conf.get("text_to_image"),
  71. "image_create_prefix": local_conf.get("image_create_prefix")
  72. }
  73. if local_conf.get("always_reply_voice"):
  74. config["reply_voice_mode"] = "always_reply_voice"
  75. elif local_conf.get("voice_reply_voice"):
  76. config["reply_voice_mode"] = "voice_reply_voice"
  77. if pconf("linkai"):
  78. config["group_app_map"] = pconf("linkai").get("group_app_map")
  79. if plugin_config.get("Godcmd"):
  80. config["admin_password"] = plugin_config.get("Godcmd").get("password")
  81. return config