Skip to content

Commit

Permalink
reply with question or statement
Browse files Browse the repository at this point in the history
  • Loading branch information
cyri113 committed Sep 17, 2024
1 parent 3abbe6e commit 6b8d78a
Show file tree
Hide file tree
Showing 19 changed files with 119 additions and 126 deletions.
25 changes: 25 additions & 0 deletions apps/bot/src/bot.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { Controller, Logger, UseInterceptors } from '@nestjs/common';
import { MessagePattern, Payload } from '@nestjs/microservices';
import { BotService } from './bot.service';
import { TelegramAction } from '@app/common';
import { EventsInterceptor } from '@app/common/rmq/interceptors/events.interceptor';

interface MessagePayload {
chat_id: number | string;
text: string;
other: any;
}

@Controller()
@UseInterceptors(EventsInterceptor)
export class BotController {
private readonly logger = new Logger(BotController.name);
constructor(private readonly botService: BotService) { }

@MessagePattern(TelegramAction.SEND_MESSAGE)
async sendMessage(@Payload() payload: MessagePayload): Promise<void> {
const { chat_id, text, other } = payload;
await this.botService.sendMessage(chat_id, text, other);
return;
}
}
2 changes: 2 additions & 0 deletions apps/bot/src/bot.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
RmqModule,
} from '@app/common';
import { Services } from '@app/common';
import { BotController } from './bot.controller';

@Module({
imports: [
Expand All @@ -21,6 +22,7 @@ import { Services } from '@app/common';
RmqModule.register(Services.GraphStore),
RmqModule.register(Services.TGQuestionService),
],
controllers: [BotController],
providers: [BotService],
})
export class BotModule { }
19 changes: 17 additions & 2 deletions apps/bot/src/bot.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { IgnoreEvent, Services, UpdateEvent } from '@app/common';
import { Inject, Injectable, Logger, OnModuleInit } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { ClientProxy } from '@nestjs/microservices';
import { API_CONSTANTS, Bot } from 'grammy';
import { API_CONSTANTS, Bot, RawApi } from 'grammy';
import { Other } from 'grammy/out/core/api';
import { Message, ReplyParameters } from 'grammy/types';

Check failure on line 7 in apps/bot/src/bot.service.ts

View workflow job for this annotation

GitHub Actions / lint

'ReplyParameters' is defined but never used

@Injectable()
export class BotService implements OnModuleInit {
Expand All @@ -29,7 +31,10 @@ export class BotService implements OnModuleInit {
this.logger.log(`Received ${event} from ${ctx.chat.id}`);
this.eventClient.emit(event, ctx);
this.graphClient.emit(event, ctx);
this.tgQuestionClient.emit(event, ctx);
if (!ctx.update.message.from.is_bot) {
this.tgQuestionClient.emit(event, ctx);
}
return;
});
});
}
Expand All @@ -39,4 +44,14 @@ export class BotService implements OnModuleInit {
allowed_updates: API_CONSTANTS.ALL_UPDATE_TYPES,
});
}

async sendMessage(
chat_id: number | string,
text: string,
other?: Other<RawApi, 'sendMessage', 'text' | 'chat_id'>,
): Promise<Message.TextMessage | null> {
const receipt = await this.bot.api.sendMessage(chat_id, text, other);
this.logger.log(`Message ${receipt.message_id} sent to ${receipt.chat.id}`);
return receipt;
}
}
4 changes: 2 additions & 2 deletions apps/event-store/src/event-store.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ import { Controller, Logger, UseInterceptors } from '@nestjs/common';
import { EventStoreService } from './event-store.service';
import { MessagePattern, Payload } from '@nestjs/microservices';
import { UpdateEvent } from '@app/common';
import { EventsInterceptor } from './interceptors/events.interceptor';
import { EventsInterceptor } from '@app/common/rmq/interceptors/events.interceptor';
import { API_CONSTANTS } from 'grammy';

@Controller()
@UseInterceptors(EventsInterceptor)
export class EventStoreController {
private readonly logger = new Logger(EventStoreController.name);

constructor(private readonly eventsService: EventStoreService) {}
constructor(private readonly eventsService: EventStoreService) { }

a = typeof API_CONSTANTS.ALL_UPDATE_TYPES;

Expand Down
6 changes: 3 additions & 3 deletions apps/graph-store/src/chat_member/chat_member.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { UpdateEvent } from '@app/common';
import { Controller, Logger, UseInterceptors } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';
import { DChatMemberUpdated } from '../decorators/chatMemberUpdated.decorator';
import { RmqInterceptor } from '../interceptors/rmq.interceptor';
import { EventsInterceptor } from '@app/common/rmq/interceptors/events.interceptor';
import { Chat, ChatMemberUpdated } from 'grammy/types';
import { ChatMemberService } from './chat_member.service';
import { DChat } from '../decorators/chat.decorator';
Expand All @@ -25,11 +25,11 @@ type Action =
| 'DEMOTED';

@Controller('chat-member')
@UseInterceptors(RmqInterceptor)
@UseInterceptors(EventsInterceptor)
export class ChatMemberController {
private readonly logger = new Logger(ChatMemberController.name);

constructor(private readonly chatMemberService: ChatMemberService) {}
constructor(private readonly chatMemberService: ChatMemberService) { }

@MessagePattern(UpdateEvent.CHAT_MEMBER)
async chat_member(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ import { MentionedService } from '../mentioned/mentioned.service';
import { MessageService } from '../message/message.service';
import { RepliedService } from '../replied/replied.service';
import { UserService } from '../user/user.service';
import { RmqInterceptor } from '../interceptors/rmq.interceptor';
import { EventsInterceptor } from '@app/common/rmq/interceptors/events.interceptor';

@Controller('edited-message')
@UseInterceptors(RmqInterceptor)
@UseInterceptors(EventsInterceptor)
export class EditedMessageController {
private readonly logger = new Logger(EditedMessageController.name);

Expand All @@ -28,7 +28,7 @@ export class EditedMessageController {
private readonly joinedService: JoinedService,
private readonly repliedService: RepliedService,
private readonly mentionedService: MentionedService,
) {}
) { }

@MessagePattern(UpdateEvent.EDITED_MESSAGE)
async chat_member(
Expand Down
2 changes: 1 addition & 1 deletion apps/graph-store/src/graph-store.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ import { MessageReactionModule } from './message_reaction/message_reaction.modul
MessageReactionModule,
],
})
export class GraphStoreModule {}
export class GraphStoreModule { }
65 changes: 0 additions & 65 deletions apps/graph-store/src/interceptors/rmq.interceptor.spec.ts

This file was deleted.

24 changes: 0 additions & 24 deletions apps/graph-store/src/interceptors/rmq.interceptor.ts

This file was deleted.

6 changes: 3 additions & 3 deletions apps/graph-store/src/message/message.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { UpdateEvent } from '@app/common';
import { Controller, Logger, UseInterceptors } from '@nestjs/common';
import { MessagePattern } from '@nestjs/microservices';
import { DChat } from '../decorators/chat.decorator';
import { RmqInterceptor } from '../interceptors/rmq.interceptor';
import { MessageService } from './message.service';
import { DMessage } from '../decorators/message.decorator';
import { Chat, Message, MessageEntity, User } from 'grammy/types';
Expand All @@ -14,9 +13,10 @@ import { MentionedService } from '../mentioned/mentioned.service';
import { RepliedService } from '../replied/replied.service';
import { UserService } from '../user/user.service';
import { Neo4jService } from 'nest-neo4j/dist';
import { EventsInterceptor } from '@app/common/rmq/interceptors/events.interceptor';

@Controller('message')
@UseInterceptors(RmqInterceptor)
@UseInterceptors(EventsInterceptor)
export class MessageController {
private readonly logger = new Logger(MessageController.name);

Expand All @@ -28,7 +28,7 @@ export class MessageController {
private readonly joinedService: JoinedService,
private readonly repliedService: RepliedService,
private readonly mentionedService: MentionedService,
) {}
) { }

@MessagePattern(UpdateEvent.MESSAGE)
async message(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import { DUser } from '../decorators/user.decorator';
import { JoinedService } from '../joined/joined.service';
import { UserService } from '../user/user.service';
import { MessageReactionService } from './message_reaction.service';
import { RmqInterceptor } from '../interceptors/rmq.interceptor';
import { EventsInterceptor } from '@app/common/rmq/interceptors/events.interceptor';

@Controller('message-reaction')
@UseInterceptors(RmqInterceptor)
@UseInterceptors(EventsInterceptor)
export class MessageReactionController {
private readonly logger = new Logger(MessageReactionController.name);

Expand All @@ -23,7 +23,7 @@ export class MessageReactionController {
private readonly userService: UserService,
private readonly joinedService: JoinedService,
private readonly messageReactionService: MessageReactionService,
) {}
) { }

@MessagePattern(UpdateEvent.MESSAGE_REACTION)
async message(
Expand Down
43 changes: 35 additions & 8 deletions apps/question-service/src/question-service.controller.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
import { Controller, Logger } from '@nestjs/common';
import { Controller, Inject, Logger } from '@nestjs/common';
import { QuestionServiceService } from './question-service.service';
import { MessagePattern, Payload } from '@nestjs/microservices';
import { UpdateEvent } from '@app/common';
import { ClientProxy, MessagePattern, Payload } from '@nestjs/microservices';
import { Services, TelegramAction, UpdateEvent } from '@app/common';
import { Context } from 'grammy';
import { ReplyParameters } from 'grammy/types';
import { UseInterceptors } from '@nestjs/common';
import { EventsInterceptor } from '@app/common/rmq/interceptors/events.interceptor';

@Controller()
@UseInterceptors(EventsInterceptor)
export class QuestionServiceController {
private readonly logger = new Logger(QuestionServiceController.name);
constructor(
private readonly questionServiceService: QuestionServiceService,
@Inject(Services.TelegramBot.name)
private readonly telegramClient: ClientProxy,
) { }

@MessagePattern(UpdateEvent.MESSAGE)
async message(@Payload() payload): Promise<void> {
const { update } = payload;
const { message } = update;
const { data } = await this.questionServiceService.test(message.text);
this.logger.log('Result', data);
async message(@Payload() ctx: Context): Promise<void> {
const {
message_id,
text: msg,
chat: { id: chat_id },
} = ctx.update?.message;
const {
data: { label, score },
} = await this.questionServiceService.test(msg);

const text = `${label}: ${score}`;

const reply_parameters: ReplyParameters = {
message_id,
};

const data = {
chat_id,
text,
other: {
reply_parameters,
},
};

this.telegramClient.emit(TelegramAction.SEND_MESSAGE, data);
}
}
9 changes: 8 additions & 1 deletion apps/question-service/src/question-service.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Module } from '@nestjs/common';
import { QuestionServiceController } from './question-service.controller';
import { QuestionServiceService } from './question-service.service';
import { schemaConfig, rmqConfig, mongoConfig, RmqModule } from '@app/common';
import {
schemaConfig,
rmqConfig,
mongoConfig,
RmqModule,
Services,
} from '@app/common';
import { ConfigModule } from '@nestjs/config';
import { HttpModule } from '@nestjs/axios';

Expand All @@ -14,6 +20,7 @@ import { HttpModule } from '@nestjs/axios';
}),
RmqModule,
HttpModule,
RmqModule.register(Services.TelegramBot),
],
controllers: [QuestionServiceController],
providers: [QuestionServiceService],
Expand Down
6 changes: 4 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ x-telegram-common: &telegram-common
condition: service_healthy
mongodb:
condition: service_healthy
volumes:
- ./:/usr/src/app/

services:
mongodb:
Expand Down Expand Up @@ -75,7 +77,7 @@ services:
telegram-bot:
<<: [ *telegram-common ]
container_name: telegram-bot
command: npm run start bot
command: npm run start:dev bot
telegram-events:
<<: [ *telegram-common ]
container_name: telegram-events
Expand All @@ -87,7 +89,7 @@ services:
telegram-question:
<<: [ *telegram-common ]
container_name: telegram-question
command: npm run start question-service
command: npm run start:dev question-service

question-service-ext:
image: ghcr.io/togethercrew/question-service:main
Expand Down
Loading

0 comments on commit 6b8d78a

Please sign in to comment.