diff options
| author | blackhao <13851610112@163.com> | 2025-03-28 05:29:26 -0500 |
|---|---|---|
| committer | blackhao <13851610112@163.com> | 2025-03-28 05:29:26 -0500 |
| commit | c716c400b934dcd0542503f30c49051dfea4768b (patch) | |
| tree | 3182315aa5dceac0bd7b3a929172f80e0efa927e /robot.py | |
Diffstat (limited to 'robot.py')
| -rw-r--r-- | robot.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/robot.py b/robot.py new file mode 100644 index 0000000..5177575 --- /dev/null +++ b/robot.py @@ -0,0 +1,48 @@ +import asyncio +import json +import os.path +import threading +from typing import Dict, List +from openai import OpenAI +import aiohttp +import qqbot + + +from qqbot.core.util.yaml_util import YamlUtil +from qqbot.model.message import MessageEmbed, MessageEmbedField, MessageEmbedThumbnail, CreateDirectMessageRequest, MessageArk, MessageArkKv, MessageArkObj, MessageArkObjKv + +def load_openai_key(filepath="openai_key.txt"): + with open(filepath, 'r') as f: + return f.read().strip() + +openai_key = load_openai_key() +test_config = YamlUtil.read(os.path.join(os.path.dirname(__file__), "config.yaml")) + +clinet = OpenAI(api_key=openai_key) + +gpt_instruction = YamlUtil.read(os.path.join(os.path.dirname(__file__), "gpt_instruction.yaml")) + +async def _message_handler(event, message: qqbot.Message): + msg_api = qqbot.AsyncMessageAPI(t_token, False) + qqbot.logger.info("event %s" % event + ",receive message %s" % message.content) + response = clinet.chat.completions.create( + model="gpt-3.5-turbo-0125", + messages=[ + {"role": "system", "content": gpt_instruction}, + {"role": "user", "content": message.content} + ] + ) + reply = response.choices[0].message + print(reply) + message_to_send = qqbot.MessageSendRequest(content=reply.content, msg_id=message.id) + await msg_api.post_message(message.channel_id, message_to_send) + +#write a funtion for the bot: when a message begins with "/copy", the bot will copy the message and reply it. + +if __name__ == "__main__": + t_token = qqbot.Token(test_config["token"]["appid"], test_config["token"]["token"]) + qqbot_handler = qqbot.Handler( + qqbot.HandlerType.AT_MESSAGE_EVENT_HANDLER, _message_handler + ) + + qqbot.async_listen_events(t_token, False, qqbot_handler)
\ No newline at end of file |
