You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

linkai_client.py 3.8KB

1 yıl önce
1 yıl önce
1 yıl önce
1 yıl önce
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. chat_client: LinkAIClient
  8. class ChatClient(LinkAIClient):
  9. def __init__(self, api_key, host, channel):
  10. super().__init__(api_key, host)
  11. self.channel = channel
  12. self.client_type = channel.channel_type
  13. def on_message(self, push_msg: PushMsg):
  14. session_id = push_msg.session_id
  15. msg_content = push_msg.msg_content
  16. logger.info(f"receive msg push, session_id={session_id}, msg_content={msg_content}")
  17. context = Context()
  18. context.type = ContextType.TEXT
  19. context["receiver"] = session_id
  20. context["isgroup"] = push_msg.is_group
  21. self.channel.send(Reply(ReplyType.TEXT, content=msg_content), context)
  22. def on_config(self, config: dict):
  23. if not self.client_id:
  24. return
  25. logger.info(f"[LinkAI] 从客户端管理加载远程配置: {config}")
  26. if config.get("enabled") != "Y":
  27. return
  28. local_config = conf()
  29. for key in config.keys():
  30. if key in available_setting and config.get(key) is not None:
  31. local_config[key] = config.get(key)
  32. # 语音配置
  33. reply_voice_mode = config.get("reply_voice_mode")
  34. if reply_voice_mode:
  35. if reply_voice_mode == "voice_reply_voice":
  36. local_config["voice_reply_voice"] = True
  37. elif reply_voice_mode == "always_reply_voice":
  38. local_config["always_reply_voice"] = True
  39. if config.get("admin_password") and plugin_config["Godcmd"]:
  40. plugin_config["Godcmd"]["password"] = config.get("admin_password")
  41. PluginManager().instances["GODCMD"].reload()
  42. if config.get("group_app_map") and pconf("linkai"):
  43. local_group_map = {}
  44. for mapping in config.get("group_app_map"):
  45. local_group_map[mapping.get("group_name")] = mapping.get("app_code")
  46. pconf("linkai")["group_app_map"] = local_group_map
  47. PluginManager().instances["LINKAI"].reload()
  48. def start(channel):
  49. global chat_client
  50. chat_client = ChatClient(api_key=conf().get("linkai_api_key"),
  51. host="link-ai.chat", channel=channel)
  52. chat_client.config = _build_config()
  53. chat_client.start()
  54. def _build_config():
  55. local_conf = conf()
  56. config = {
  57. "linkai_app_code": local_conf.get("linkai_app_code"),
  58. "single_chat_prefix": local_conf.get("single_chat_prefix"),
  59. "single_chat_reply_prefix": local_conf.get("single_chat_reply_prefix"),
  60. "single_chat_reply_suffix": local_conf.get("single_chat_reply_suffix"),
  61. "group_chat_prefix": local_conf.get("group_chat_prefix"),
  62. "group_chat_reply_prefix": local_conf.get("group_chat_reply_prefix"),
  63. "group_chat_reply_suffix": local_conf.get("group_chat_reply_suffix"),
  64. "group_name_white_list": local_conf.get("group_name_white_list"),
  65. "nick_name_black_list": local_conf.get("nick_name_black_list"),
  66. "speech_recognition": "Y" if local_conf.get("speech_recognition") else "N",
  67. "text_to_image": local_conf.get("text_to_image"),
  68. "image_create_prefix": local_conf.get("image_create_prefix")
  69. }
  70. if local_conf.get("always_reply_voice"):
  71. config["reply_voice_mode"] = "always_reply_voice"
  72. elif local_conf.get("voice_reply_voice"):
  73. config["reply_voice_mode"] = "voice_reply_voice"
  74. if pconf("linkai"):
  75. config["group_app_map"] = pconf("linkai").get("group_app_map")
  76. if plugin_config.get("Godcmd"):
  77. config["admin_password"] = plugin_config.get("Godcmd").get("password")
  78. return config