Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question service #28

Merged
merged 11 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
Expand All @@ -24,8 +24,8 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
Expand All @@ -36,8 +36,8 @@ jobs:
needs: [ test ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
Expand All @@ -51,9 +51,9 @@ jobs:
needs: [ test, lint ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: docker/setup-buildx-action@v2
- uses: docker/login-action@v2
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
Expand All @@ -62,7 +62,7 @@ jobs:
id: meta
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
- uses: docker/build-push-action@v4
- uses: docker/build-push-action@v6
with:
context: .
target: production
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ COPY . .
RUN npm run build bot
RUN npm run build event-store
RUN npm run build graph-store
RUN npm run build question-service

FROM node:alpine AS production

Expand Down
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;
}
}
5 changes: 4 additions & 1 deletion 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 @@ -19,7 +20,9 @@ import { Services } from '@app/common';
}),
RmqModule.register(Services.EventStore),
RmqModule.register(Services.GraphStore),
RmqModule.register(Services.TGQuestionService),
],
controllers: [BotController],
providers: [BotService],
})
export class BotModule {}
export class BotModule { }
6 changes: 6 additions & 0 deletions apps/bot/src/bot.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ describe('BotService', () => {
emit: jest.fn(),
},
},
{
provide: Services.TGQuestionService.name,
useValue: {
emit: jest.fn(),
},
},
{
provide: ConfigService,
useValue: {
Expand Down
20 changes: 19 additions & 1 deletion 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 } from 'grammy/types';

@Injectable()
export class BotService implements OnModuleInit {
Expand All @@ -12,6 +14,8 @@ export class BotService implements OnModuleInit {
constructor(
@Inject(Services.EventStore.name) private readonly eventClient: ClientProxy,
@Inject(Services.GraphStore.name) private readonly graphClient: ClientProxy,
@Inject(Services.TGQuestionService.name)
private readonly tgQuestionClient: ClientProxy,
private readonly configService: ConfigService,
) {
this.bot = new Bot(configService.get<string>('telegram.token'));
Expand All @@ -27,6 +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);
if (!ctx.update.message.from.is_bot) {
this.tgQuestionClient.emit(event, ctx);
}
return;
});
});
}
Expand All @@ -36,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;
}
}
1 change: 0 additions & 1 deletion apps/bot/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { BotModule } from './bot.module';
import { RmqService, Services } from '@app/common';

async function bootstrap() {
// const app = await NestFactory.createMicroservice(BotModule);
const app = await NestFactory.create(BotModule);
const rmqService = app.get<RmqService>(RmqService);
app.connectMicroservice(rmqService.getOptions(Services.TelegramBot.queue));
Expand Down
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
65 changes: 0 additions & 65 deletions apps/event-store/src/interceptors/events.interceptor.spec.ts

This file was deleted.

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.

Loading
Loading