linkai_client.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. local_config["always_reply_voice"] = False
  39. elif reply_voice_mode == "always_reply_voice":
  40. local_config["always_reply_voice"] = True
  41. local_config["voice_reply_voice"] = True
  42. elif reply_voice_mode == "no_reply_voice":
  43. local_config["always_reply_voice"] = False
  44. local_config["voice_reply_voice"] = False
  45. if config.get("admin_password"):
  46. if not plugin_config.get("Godcmd"):
  47. plugin_config["Godcmd"] = {"password": config.get("admin_password"), "admin_users": []}
  48. else:
  49. plugin_config["Godcmd"]["password"] = config.get("admin_password")
  50. PluginManager().instances["GODCMD"].reload()
  51. if config.get("group_app_map") and pconf("linkai"):
  52. local_group_map = {}
  53. for mapping in config.get("group_app_map"):
  54. local_group_map[mapping.get("group_name")] = mapping.get("app_code")
  55. pconf("linkai")["group_app_map"] = local_group_map
  56. PluginManager().instances["LINKAI"].reload()
  57. if config.get("text_to_image") and config.get("text_to_image") == "midjourney" and pconf("linkai"):
  58. if pconf("linkai")["midjourney"]:
  59. pconf("linkai")["midjourney"]["enabled"] = True
  60. pconf("linkai")["midjourney"]["use_image_create_prefix"] = True
  61. elif config.get("text_to_image") and config.get("text_to_image") in ["dall-e-2", "dall-e-3"]:
  62. if pconf("linkai")["midjourney"]:
  63. pconf("linkai")["midjourney"]["use_image_create_prefix"] = False
  64. def start(channel):
  65. global chat_client
  66. chat_client = ChatClient(api_key=conf().get("linkai_api_key"), host="", channel=channel)
  67. chat_client.config = _build_config()
  68. chat_client.start()
  69. time.sleep(1.5)
  70. if chat_client.client_id:
  71. logger.info("[LinkAI] 可前往控制台进行线上登录和配置:https://link-ai.tech/console/clients")
  72. def _build_config():
  73. local_conf = conf()
  74. config = {
  75. "linkai_app_code": local_conf.get("linkai_app_code"),
  76. "single_chat_prefix": local_conf.get("single_chat_prefix"),
  77. "single_chat_reply_prefix": local_conf.get("single_chat_reply_prefix"),
  78. "single_chat_reply_suffix": local_conf.get("single_chat_reply_suffix"),
  79. "group_chat_prefix": local_conf.get("group_chat_prefix"),
  80. "group_chat_reply_prefix": local_conf.get("group_chat_reply_prefix"),
  81. "group_chat_reply_suffix": local_conf.get("group_chat_reply_suffix"),
  82. "group_name_white_list": local_conf.get("group_name_white_list"),
  83. "nick_name_black_list": local_conf.get("nick_name_black_list"),
  84. "speech_recognition": "Y" if local_conf.get("speech_recognition") else "N",
  85. "text_to_image": local_conf.get("text_to_image"),
  86. "image_create_prefix": local_conf.get("image_create_prefix")
  87. }
  88. if local_conf.get("always_reply_voice"):
  89. config["reply_voice_mode"] = "always_reply_voice"
  90. elif local_conf.get("voice_reply_voice"):
  91. config["reply_voice_mode"] = "voice_reply_voice"
  92. if pconf("linkai"):
  93. config["group_app_map"] = pconf("linkai").get("group_app_map")
  94. if plugin_config.get("Godcmd"):
  95. config["admin_password"] = plugin_config.get("Godcmd").get("password")
  96. return config