Skip to content

Commit

Permalink
feat: qq -> tg 正在输入状态
Browse files Browse the repository at this point in the history
  • Loading branch information
clansty committed Dec 28, 2024
1 parent bf38d5d commit 845355a
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 3 deletions.
8 changes: 7 additions & 1 deletion main/src/client/NapCatClient/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CreateQQClientParamsBase, Friend, FriendIncreaseEvent, Group, GroupMemberDecreaseEvent, GroupMemberIncreaseEvent, MessageEvent, MessageRecallEvent, PokeEvent, QQClient, SendableElem } from '../QQClient';
import { CreateQQClientParamsBase, Friend, FriendIncreaseEvent, Group, GroupMemberDecreaseEvent, GroupMemberIncreaseEvent, InputStatusChangeEvent, MessageEvent, MessageRecallEvent, PokeEvent, QQClient, SendableElem } from '../QQClient';
import random from '../../utils/random';
import { getLogger, Logger } from 'log4js';
import posthog from '../../models/posthog';
Expand Down Expand Up @@ -82,6 +82,8 @@ export class NapCatClient extends QQClient {
await this.handleMessageRecall(data);
else if (data.post_type === 'notice' && data.notice_type === 'notify' && data.sub_type === 'poke')
await this.handlePoke(data);
else if (data.post_type === 'notice' && data.notice_type === 'notify' && data.sub_type === 'input_status')
await this.handleInput(data);
else if (data.post_type === 'request' && data.request_type === 'friend')
await this.handleFriendRequest(data);
else if (data.post_type === 'request' && data.request_type === 'group' && data.sub_type === 'invite')
Expand Down Expand Up @@ -162,6 +164,10 @@ export class NapCatClient extends QQClient {
await this.callHandlers(this.onPokeHandlers, event);
}

private async handleInput(data: WSReceiveHandler['notice.notify.input_status']) {
await this.callHandlers(this.onInputStatusChangeHandlers, new InputStatusChangeEvent(await this.pickFriend(data.user_id), !!data.status_text));
}

private async handleFriendRequest(data: WSReceiveHandler['request.friend']) {
const event = await NapCatFriendRequestEvent.create(this, data);
await this.callHandlers(this.onFriendRequestHandlers, event);
Expand Down
7 changes: 6 additions & 1 deletion main/src/client/OicqClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { pb } from '@icqqjs/icqq/lib/core';
import env from '../models/env';
import {
CreateQQClientParamsBase, ForwardMessage, Friend, FriendIncreaseEvent, GroupMemberDecreaseEvent,
GroupMemberIncreaseEvent,
GroupMemberIncreaseEvent, InputStatusChangeEvent,
MessageEvent, MessageRecallEvent, PokeEvent,
QQClient,
} from './QQClient';
Expand Down Expand Up @@ -121,6 +121,7 @@ export default class OicqClient extends QQClient {
client.oicq.trap('notice.group.poke', client.onPoke);
client.oicq.trap('request.friend', client.onFriendRequest);
client.oicq.trap('request.group.invite', client.onGroupInvite);
client.oicq.trap('internal.input', client.onInput);
client.isOnMessageCreated = true;
}

Expand Down Expand Up @@ -229,6 +230,10 @@ export default class OicqClient extends QQClient {
await this.callHandlers(this.onGroupInviteHandlers, event);
};

private onInput = async (event: { user_id: number, end: boolean }) => {
await this.callHandlers(this.onInputStatusChangeHandlers, new InputStatusChangeEvent(await this.pickFriend(event.user_id), !event.end));
};

public async makeForwardMsgSelf(msglist: Forwardable[] | Forwardable, dm?: boolean): Promise<{
resid: string,
tSum: number
Expand Down
8 changes: 8 additions & 0 deletions main/src/client/QQClient/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,11 @@ export class PokeEvent extends ChatEvent {
super(chat);
}
}

export class InputStatusChangeEvent {
constructor(
public readonly chat: Friend,
public readonly typing: boolean,
) {
}
}
14 changes: 13 additions & 1 deletion main/src/client/QQClient/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Friend, Group, SendableElem } from './entity';
import {
FriendIncreaseEvent,
GroupMemberDecreaseEvent,
GroupMemberIncreaseEvent,
GroupMemberIncreaseEvent, InputStatusChangeEvent,
MessageEvent,
MessageRecallEvent, PokeEvent,
} from './events';
Expand Down Expand Up @@ -172,6 +172,18 @@ export abstract class QQClient {
this.onGroupInviteHandlers.splice(this.onGroupInviteHandlers.indexOf(handler), 1);
}

//
protected readonly onInputStatusChangeHandlers: Array<(e: InputStatusChangeEvent) => Promise<void | boolean>> = [];

public addInputStatusChangeHandler(handler: (e: InputStatusChangeEvent) => Promise<void | boolean>) {
this.onInputStatusChangeHandlers.push(handler);
}

public removeInputStatusChangeHandler(handler: (e: InputStatusChangeEvent) => Promise<void | boolean>) {
this.onInputStatusChangeHandlers.includes(handler) &&
this.onInputStatusChangeHandlers.splice(this.onInputStatusChangeHandlers.indexOf(handler), 1);
}

// End Handlers

public getChat(roomId: number, tempChatFromGroupId?: number): Promise<Group | Friend> {
Expand Down
9 changes: 9 additions & 0 deletions main/src/client/TelegramChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,15 @@ export default class TelegramChat {
);
}

public async setTyping(action: Api.TypeSendMessageAction = new Api.SendMessageTypingAction()) {
return await this.client.invoke(
new Api.messages.SetTyping({
peer: this.inputPeer,
action,
}),
);
}

public async startImportSession(textFile: CustomFile, mediaCount: number) {
await this.client.invoke(
new Api.messages.CheckHistoryImportPeer({
Expand Down
28 changes: 28 additions & 0 deletions main/src/controllers/TypingController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import Instance from '../models/Instance';
import Telegram from '../client/Telegram';
import { InputStatusChangeEvent, QQClient } from '../client/QQClient';
import flags from '../constants/flags';
import { Api } from 'telegram';

export default class TypingController {
constructor(private readonly instance: Instance,
private readonly tgBot: Telegram,
private readonly tgUser: Telegram,
private readonly oicq: QQClient) {
oicq.addInputStatusChangeHandler(this.handleInputStatusChange);
}

private handleInputStatusChange = async (event: InputStatusChangeEvent) => {
const pair = this.instance.forwardPairs.find(event.chat);
if (!pair) return;
if (pair.flags & flags.DISABLE_Q2TG) return;

if (event.typing) {
console.log(await pair.tg.setTyping())

}
else {
await pair.tg.setTyping(new Api.SendMessageCancelAction());
}
};
}
3 changes: 3 additions & 0 deletions main/src/models/Instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { QQClient } from '../client/QQClient';
import posthog from './posthog';
import LoadingController from '../controllers/LoadingController';
import { sleep } from 'telegram/Helpers';
import TypingController from '../controllers/TypingController';

export default class Instance {
public static readonly instances: Instance[] = [];
Expand Down Expand Up @@ -63,6 +64,7 @@ export default class Instance {
private miraiSkipFilterController: MiraiSkipFilterController;
private aliveCheckController: AliveCheckController;
private loadingController: LoadingController;
private typingController: TypingController;

private constructor(public readonly id: number) {
this.log = getLogger(`Instance - ${this.id}`);
Expand Down Expand Up @@ -165,6 +167,7 @@ export default class Instance {
this.miraiSkipFilterController = new MiraiSkipFilterController(this, this.tgBot, this.tgUser, this.oicq);
this.inChatCommandsController = new InChatCommandsController(this, this.tgBot, this.tgUser, this.oicq);
this.quotLyController = new QuotLyController(this, this.tgBot, this.oicq);
this.typingController = new TypingController(this, this.tgBot, this.tgUser, this.oicq);
this.forwardController = new ForwardController(this, this.tgBot, this.tgUser, this.oicq);
if (this.workMode === 'group') {
this.hugController = new HugController(this, this.tgBot, this.oicq);
Expand Down

0 comments on commit 845355a

Please sign in to comment.