mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
refactor: OB11ActiveHttpAdapter
This commit is contained in:
@@ -11,7 +11,8 @@ import { ActionMap } from '@/onebot/action';
|
|||||||
export class OB11ActiveHttpAdapter implements IOB11NetworkAdapter {
|
export class OB11ActiveHttpAdapter implements IOB11NetworkAdapter {
|
||||||
logger: LogWrapper;
|
logger: LogWrapper;
|
||||||
isEnable: boolean = false;
|
isEnable: boolean = false;
|
||||||
public config: HttpClientConfig;
|
config: HttpClientConfig;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
public name: string,
|
public name: string,
|
||||||
config: HttpClientConfig,
|
config: HttpClientConfig,
|
||||||
@@ -24,39 +25,27 @@ export class OB11ActiveHttpAdapter implements IOB11NetworkAdapter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onEvent<T extends OB11EmitEventContent>(event: T) {
|
onEvent<T extends OB11EmitEventContent>(event: T) {
|
||||||
if (!this.isEnable) {
|
this.emitEventAsync(event).catch(e => this.logger.logError('[OneBot] [Http Client] 新消息事件HTTP上报返回快速操作失败', e));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async emitEventAsync<T extends OB11EmitEventContent>(event: T) {
|
||||||
|
if (!this.isEnable) return;
|
||||||
|
|
||||||
const headers: Record<string, string> = {
|
const headers: Record<string, string> = {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'x-self-id': this.core.selfInfo.uin,
|
'x-self-id': this.core.selfInfo.uin,
|
||||||
};
|
};
|
||||||
|
|
||||||
const msgStr = JSON.stringify(event);
|
const msgStr = JSON.stringify(event);
|
||||||
if (this.config.token && this.config.token.length > 0) {
|
if (this.config.token) {
|
||||||
const hmac = createHmac('sha1', this.config.token);
|
const hmac = createHmac('sha1', this.config.token);
|
||||||
hmac.update(msgStr);
|
hmac.update(msgStr);
|
||||||
const sig = hmac.digest('hex');
|
headers['x-signature'] = 'sha1=' + hmac.digest('hex');
|
||||||
headers['x-signature'] = 'sha1=' + sig;
|
|
||||||
}
|
}
|
||||||
RequestUtil.HttpGetText(this.config.url, 'POST', msgStr, headers).then(async (res) => {
|
|
||||||
let resJson: QuickAction;
|
const data = await RequestUtil.HttpGetText(this.config.url, 'POST', msgStr, headers);
|
||||||
try {
|
const resJson: QuickAction = JSON.parse(data);
|
||||||
resJson = JSON.parse(res);
|
await this.obContext.apis.QuickActionApi.handleQuickOperation(event as QuickActionEvent, resJson);
|
||||||
//logDebug('新消息事件HTTP上报返回快速操作: ', JSON.stringify(resJson));
|
|
||||||
} catch (e) {
|
|
||||||
this.logger.logDebug('[OneBot] [Http Client] 新消息事件HTTP上报没有返回快速操作,不需要处理');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
this.obContext.apis.QuickActionApi
|
|
||||||
.handleQuickOperation(event as QuickActionEvent, resJson)
|
|
||||||
.catch(e => this.logger.logError(e));
|
|
||||||
} catch (e: any) {
|
|
||||||
this.logger.logError('[OneBot] [Http Client] 新消息事件HTTP上报返回快速操作失败', e);
|
|
||||||
}
|
|
||||||
}).catch((e) => {
|
|
||||||
this.logger.logError('[OneBot] [Http Client] 新消息事件HTTP上报失败', e);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
open() {
|
open() {
|
||||||
@@ -66,20 +55,24 @@ export class OB11ActiveHttpAdapter implements IOB11NetworkAdapter {
|
|||||||
close() {
|
close() {
|
||||||
this.isEnable = false;
|
this.isEnable = false;
|
||||||
}
|
}
|
||||||
async reload(newconfig: HttpClientConfig) {
|
|
||||||
|
async reload(newConfig: HttpClientConfig) {
|
||||||
const wasEnabled = this.isEnable;
|
const wasEnabled = this.isEnable;
|
||||||
const oldUrl = this.config.url;
|
const oldUrl = this.config.url;
|
||||||
this.config = newconfig;
|
this.config = newConfig;
|
||||||
if (newconfig.enable && !wasEnabled) {
|
|
||||||
|
if (newConfig.enable && !wasEnabled) {
|
||||||
this.open();
|
this.open();
|
||||||
return OB11NetworkReloadType.NetWorkOpen;
|
return OB11NetworkReloadType.NetWorkOpen;
|
||||||
} else if (!newconfig.enable && wasEnabled) {
|
} else if (!newConfig.enable && wasEnabled) {
|
||||||
this.close();
|
this.close();
|
||||||
return OB11NetworkReloadType.NetWorkClose;
|
return OB11NetworkReloadType.NetWorkClose;
|
||||||
}
|
}
|
||||||
if (oldUrl !== newconfig.url) {
|
|
||||||
|
if (oldUrl !== newConfig.url) {
|
||||||
return OB11NetworkReloadType.NetWorkReload;
|
return OB11NetworkReloadType.NetWorkReload;
|
||||||
}
|
}
|
||||||
|
|
||||||
return OB11NetworkReloadType.Normal;
|
return OB11NetworkReloadType.Normal;
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -354,8 +354,6 @@ export class NapCatShell {
|
|||||||
};
|
};
|
||||||
this.core = new NapCatCore(this.context, selfInfo);
|
this.core = new NapCatCore(this.context, selfInfo);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
async InitNapCat() {
|
async InitNapCat() {
|
||||||
await this.core.initCore();
|
await this.core.initCore();
|
||||||
|
Reference in New Issue
Block a user