chore: tidy some files
This commit is contained in:
+6
-6
@@ -213,8 +213,8 @@ def initBot(cfg, prov):
|
||||
gu.log("- 逆向ChatGPT库 -", gu.LEVEL_INFO)
|
||||
if cfg['rev_ChatGPT']['enable']:
|
||||
if 'account' in cfg['rev_ChatGPT']:
|
||||
from model.provider.provider_rev_chatgpt import ProviderRevChatGPT
|
||||
from model.command.command_rev_chatgpt import CommandRevChatGPT
|
||||
from model.provider.rev_chatgpt import ProviderRevChatGPT
|
||||
from model.command.rev_chatgpt import CommandRevChatGPT
|
||||
llm_instance[REV_CHATGPT] = ProviderRevChatGPT(cfg['rev_ChatGPT'], base_url=cc.get("CHATGPT_BASE_URL", None))
|
||||
llm_command_instance[REV_CHATGPT] = CommandRevChatGPT(llm_instance[REV_CHATGPT], _global_object)
|
||||
chosen_provider = REV_CHATGPT
|
||||
@@ -227,8 +227,8 @@ def initBot(cfg, prov):
|
||||
else:
|
||||
if cfg['rev_edgegpt']['enable']:
|
||||
try:
|
||||
from model.provider.provider_rev_edgegpt import ProviderRevEdgeGPT
|
||||
from model.command.command_rev_edgegpt import CommandRevEdgeGPT
|
||||
from model.provider.rev_edgegpt import ProviderRevEdgeGPT
|
||||
from model.command.rev_edgegpt import CommandRevEdgeGPT
|
||||
llm_instance[REV_EDGEGPT] = ProviderRevEdgeGPT()
|
||||
llm_command_instance[REV_EDGEGPT] = CommandRevEdgeGPT(llm_instance[REV_EDGEGPT], _global_object)
|
||||
chosen_provider = REV_EDGEGPT
|
||||
@@ -238,8 +238,8 @@ def initBot(cfg, prov):
|
||||
if OPENAI_OFFICIAL in prov:
|
||||
gu.log("- OpenAI官方 -", gu.LEVEL_INFO)
|
||||
if cfg['openai']['key'] is not None and cfg['openai']['key'] != [None]:
|
||||
from model.provider.provider_openai_official import ProviderOpenAIOfficial
|
||||
from model.command.command_openai_official import CommandOpenAIOfficial
|
||||
from model.provider.openai_official import ProviderOpenAIOfficial
|
||||
from model.command.openai_official import CommandOpenAIOfficial
|
||||
llm_instance[OPENAI_OFFICIAL] = ProviderOpenAIOfficial(cfg['openai'])
|
||||
llm_command_instance[OPENAI_OFFICIAL] = CommandOpenAIOfficial(llm_instance[OPENAI_OFFICIAL], _global_object)
|
||||
chosen_provider = OPENAI_OFFICIAL
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from model.platform.qqchan import QQChan, NakuruGuildMember, NakuruGuildMessage
|
||||
from model.platform.qq import QQ
|
||||
from model.provider.provider import Provider
|
||||
from nakuru import (
|
||||
CQHTTP,
|
||||
GroupMessage,
|
||||
@@ -54,7 +55,8 @@ class AstrMessageEvent():
|
||||
qq_sdk_platform: QQChan,
|
||||
platform: str,
|
||||
role: str,
|
||||
global_object: GlobalObject):
|
||||
global_object: GlobalObject,
|
||||
llm_provider: Provider = None):
|
||||
self.message_str = message_str
|
||||
self.message_obj = message_obj
|
||||
self.gocq_platform = gocq_platform
|
||||
@@ -62,3 +64,4 @@ class AstrMessageEvent():
|
||||
self.platform = platform
|
||||
self.role = role
|
||||
self.global_object = global_object
|
||||
self.llm_provider = llm_provider
|
||||
@@ -1,5 +1,5 @@
|
||||
from model.command.command import Command
|
||||
from model.provider.provider_openai_official import ProviderOpenAIOfficial
|
||||
from model.provider.openai_official import ProviderOpenAIOfficial
|
||||
from cores.qqbot.personality import personalities
|
||||
|
||||
from model.platform.qq import QQ
|
||||
@@ -1,5 +1,5 @@
|
||||
from model.command.command import Command
|
||||
from model.provider.provider_rev_chatgpt import ProviderRevChatGPT
|
||||
from model.provider.rev_chatgpt import ProviderRevChatGPT
|
||||
from model.platform.qq import QQ
|
||||
from cores.qqbot.personality import personalities
|
||||
from cores.qqbot.global_object import GlobalObject
|
||||
@@ -1,5 +1,5 @@
|
||||
from model.command.command import Command
|
||||
from model.provider.provider_rev_edgegpt import ProviderRevEdgeGPT
|
||||
from model.provider.rev_edgegpt import ProviderRevEdgeGPT
|
||||
import asyncio
|
||||
from model.platform.qq import QQ
|
||||
from cores.qqbot.global_object import GlobalObject
|
||||
@@ -100,8 +100,6 @@ class QQChan():
|
||||
ngm.raw_message = message
|
||||
return ngm
|
||||
|
||||
|
||||
|
||||
def send_qq_msg(self, message: NakuruGuildMessage, res):
|
||||
gu.log("回复QQ频道消息: "+str(res), level=gu.LEVEL_INFO, tag="QQ频道", max_len=500)
|
||||
self.qqchan_cnt += 1
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
import requests
|
||||
import asyncio
|
||||
import websockets
|
||||
from websockets import WebSocketClientProtocol
|
||||
import threading
|
||||
import json
|
||||
|
||||
class UnofficialQQBotSDK:
|
||||
|
||||
GET_APP_ACCESS_TOKEN_URL = "https://bots.qq.com/app/getAppAccessToken"
|
||||
OPENAPI_BASE_URL = "https://api.sgroup.qq.com"
|
||||
|
||||
def __init__(self, appid: str, client_secret: str) -> None:
|
||||
self.appid = appid
|
||||
self.client_secret = client_secret
|
||||
self.get_access_token()
|
||||
self.get_wss_endpoint()
|
||||
# self.ws_connect()
|
||||
# print(self.ws_recv())
|
||||
asyncio.get_event_loop().run_until_complete(self.ws_client())
|
||||
|
||||
|
||||
def get_access_token(self) -> None:
|
||||
# self.access_token = requests.post(self.GET_APP_ACCESS_TOKEN_URL, data={
|
||||
# "appid": self.appid,
|
||||
# "clientSecret": self.client_secret
|
||||
# }).json()['access_token']
|
||||
res = requests.post(self.GET_APP_ACCESS_TOKEN_URL, json={
|
||||
"appId": self.appid,
|
||||
"clientSecret": self.client_secret
|
||||
}, headers={
|
||||
"Content-Type": "application/json"
|
||||
})
|
||||
print(res.text)
|
||||
self.access_token = 'QQBot ' + res.json()['access_token']
|
||||
print("access_token: " + self.access_token)
|
||||
|
||||
def auth_header(self) -> str:
|
||||
return {
|
||||
'Authorization': self.access_token,
|
||||
'X-Union-Appid': self.appid,
|
||||
}
|
||||
|
||||
def get_wss_endpoint(self):
|
||||
# self.wss_endpoint = requests.get(self.OPENAPI_BASE_URL + "/gateway", headers=self.auth_header()).json()['url']
|
||||
res = requests.get(self.OPENAPI_BASE_URL + "/gateway", headers=self.auth_header())
|
||||
print(res.text)
|
||||
self.wss_endpoint = res.json()['url']
|
||||
print("wss_endpoint: " + self.wss_endpoint)
|
||||
|
||||
async def behav_heartbeat(self, ws: WebSocketClientProtocol, t: int):
|
||||
while True:
|
||||
await asyncio.sleep(t - 1)
|
||||
try:
|
||||
print("heartbeat., s: " + str(self.s))
|
||||
await ws.send(json.dumps({
|
||||
"op": 1,
|
||||
"d": self.s
|
||||
}))
|
||||
except:
|
||||
print("heartbeat error.")
|
||||
|
||||
async def handle_msg(self, ws: WebSocketClientProtocol, msg: dict):
|
||||
if msg['op'] == 10:
|
||||
# hello
|
||||
# 创建心跳任务
|
||||
print("hello.")
|
||||
asyncio.get_event_loop().create_task(self.behav_heartbeat(ws, msg['d']['heartbeat_interval'] / 1000))
|
||||
# 鉴权,获得session
|
||||
await ws.send(json.dumps({
|
||||
"op": 2,
|
||||
"d": {
|
||||
"token": self.access_token,
|
||||
"intents": 33554432,
|
||||
"shard": [0, 1],
|
||||
"properties": {
|
||||
"$os": "linux",
|
||||
"$browser": "my_library",
|
||||
"$device": "my_library"
|
||||
}
|
||||
}
|
||||
}))
|
||||
|
||||
if msg['op'] == 0:
|
||||
# ready
|
||||
print("ready.")
|
||||
data = msg['d']
|
||||
print(data)
|
||||
|
||||
if 'group_openid' in data:
|
||||
group_openid = data['group_openid']
|
||||
message_str = data['content'].strip()
|
||||
message_id = data['id']
|
||||
# 发送消息
|
||||
requests.post(self.OPENAPI_BASE_URL + f"/v2/groups/{group_openid}/messages", headers=self.auth_header(), json={
|
||||
"content": message_str,
|
||||
"message_type": 0,
|
||||
"msg_id": message_id
|
||||
})
|
||||
|
||||
async def ws_client(self):
|
||||
self.s = 0
|
||||
async with websockets.connect(self.wss_endpoint) as websocket:
|
||||
print("ws connected.")
|
||||
while True:
|
||||
msg = await websocket.recv()
|
||||
msg = json.loads(msg)
|
||||
if 's' in msg:
|
||||
self.s = msg['s']
|
||||
print("recv: " + str(msg))
|
||||
await self.handle_msg(websocket, msg)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
UnofficialQQBotSDK("102041113", "b9VebKp7B2g7KK7f")
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 239 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 59 KiB |
Reference in New Issue
Block a user