Skip to content

Commit

Permalink
added telegram stats
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedegeofroy committed Oct 15, 2024
1 parent 810b2a8 commit 6e4a592
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 282 deletions.
24 changes: 1 addition & 23 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,4 @@ services:
env_file:
- .env
ports:
- "5432:5432"
networks:
- shampsdev-db
backend:
container_name: backend
build: .
env_file:
- .env
environment:
HTTP_PORT: 8000
POSTGRES_HOST: database
depends_on:
- database
ports:
- "8000:8000"
restart: unless-stopped
networks:
- shampsdev-db

networks:
shampsdev-db:
name: shampsdev-db
external: true
- "5432:5432"
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@
"graphql": "^16.9.0",
"graphql-subscriptions": "^2.0.0",
"graphql-tools": "^9.0.1",
"nestjs-telegram": "^1.2.0",
"nestjs-telegraf": "^2.8.1",
"pg": "^8.13.0",
"reflect-metadata": "^0.2.1",
"rxjs": "^7.8.1",
"sqlite3": "^5.1.7",
"telegraf": "^4.16.3",
"typeorm": "^0.3.20"
},
"devDependencies": {
Expand Down
16 changes: 11 additions & 5 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { GraphQLModule } from '@nestjs/graphql/dist/graphql.module';
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { join } from 'path';
import { TypeOrmModule } from '@nestjs/typeorm';
import { TelegramModule } from 'nestjs-telegram';
import { TelegramStatsService } from './telegram-stats/telegram-stats.service';
import { ConfigModule } from '@nestjs/config';
import { TelegrafModule } from 'nestjs-telegraf';
import { TelegramStatsService } from './telegram-stats/telegram-stats.service';
import { TelegramStatsModule } from './telegram-stats/telegram-stats.module';
import { PubSubService } from './pub-sub/pub-sub.service';

@Module({
imports: [
Expand All @@ -29,12 +31,16 @@ import { ConfigModule } from '@nestjs/config';
entities: ['dist/**/*.entity{.ts,.js}'],
synchronize: process.env.AUTO_MIGRATE === 'True',
}),
TelegramModule.forRoot({
botKey: process.env.BOT_KEY,
TelegrafModule.forRoot({
token: process.env.BOT_KEY,
launchOptions: {
allowedUpdates: ['chat_member', 'chat_boost', 'removed_chat_boost'],
},
}),
StatsModule,
TelegramStatsModule,
],
controllers: [AppController],
providers: [AppService, TelegramStatsService],
providers: [AppService, StatsModule, TelegramStatsService, PubSubService],
})
export class AppModule {}
18 changes: 18 additions & 0 deletions src/pub-sub/pub-sub.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PubSubService } from './pub-sub.service';

describe('PubSubService', () => {
let service: PubSubService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [PubSubService],
}).compile();

service = module.get<PubSubService>(PubSubService);
});

it('should be defined', () => {
expect(service).toBeDefined();
});
});
15 changes: 15 additions & 0 deletions src/pub-sub/pub-sub.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Injectable } from '@nestjs/common';
import { PubSub } from 'graphql-subscriptions';

@Injectable()
export class PubSubService {
private pubSub: PubSub;

constructor() {
this.pubSub = new PubSub();
}

getPubSub(): PubSub {
return this.pubSub;
}
}
4 changes: 3 additions & 1 deletion src/stats/stats.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ import { StatsService } from './stats.service';
import { StatsResolver } from './stats.resolver';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Stat } from './stat.entity';
import { PubSubService } from 'src/pub-sub/pub-sub.service';

@Module({
imports: [TypeOrmModule.forFeature([Stat])],
providers: [StatsService, StatsResolver],
providers: [StatsService, StatsResolver, PubSubService],
exports: [StatsService]
})
export class StatsModule {}
18 changes: 8 additions & 10 deletions src/stats/stats.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { Args, Mutation, Query, Resolver, Subscription } from '@nestjs/graphql';
import { StatsService } from './stats.service';
import { Stat } from './stat.entity';
import { CreateStatInput } from './dto/create-stat.input';
import { PubSub } from 'graphql-subscriptions';

export const pubSub = new PubSub();
import { PubSubService } from 'src/pub-sub/pub-sub.service';

@Resolver(() => Stat)
export class StatsResolver {
constructor(private statService: StatsService) {}
constructor(
private statService: StatsService,
private pubSubService: PubSubService
) {}

@Query(() => [Stat])
async stats(): Promise<Stat[]> {
Expand All @@ -17,16 +18,13 @@ export class StatsResolver {

@Mutation(() => Stat)
async createStat(
@Args('createStatInput') createStatInput: CreateStatInput,
@Args('createStatInput') createStatInput: CreateStatInput
): Promise<Stat> {
const newStat = await this.statService.createStat(createStatInput);
const newStats = await this.statService.findLatest();
pubSub.publish('statCreated', { statCreated: newStats });
return newStat;
return await this.statService.createStat(createStatInput);
}

@Subscription(() => [Stat])
statCreated() {
return pubSub.asyncIterator('statCreated');
return this.pubSubService.getPubSub().asyncIterator('statCreated');
}
}
7 changes: 7 additions & 0 deletions src/stats/stats.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { Stat } from './stat.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CreateStatInput } from './dto/create-stat.input';
import { PubSubService } from 'src/pub-sub/pub-sub.service';

@Injectable()
export class StatsService {
constructor(
@InjectRepository(Stat) private statsRepository: Repository<Stat>,
private pubSubService: PubSubService
) {}

async createStat(createStatInput: CreateStatInput): Promise<Stat> {
Expand All @@ -16,6 +18,11 @@ export class StatsService {
timestamp: new Date(),
});

const newStats = await this.findLatest();
this.pubSubService
.getPubSub()
.publish('statCreated', { statCreated: newStats });

return this.statsRepository.save(newStat);
}

Expand Down
4 changes: 4 additions & 0 deletions src/telegram-stats/telegram-stats.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { Module } from '@nestjs/common';

@Module({})
export class TelegramStatsModule {}
41 changes: 40 additions & 1 deletion src/telegram-stats/telegram-stats.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,43 @@
import { Injectable } from '@nestjs/common';
import { Hears, Help, On, Start, Update } from 'nestjs-telegraf';
import { StatsService } from 'src/stats/stats.service';
import { Context } from 'telegraf';

@Update()
@Injectable()
export class TelegramStatsService {}
export class TelegramStatsService {
constructor(private statsService: StatsService) {}

getData(): { message: string } {
return { message: 'Welcome to server!' };
}

@Start()
async startCommand(ctx: Context) {
await ctx.reply('Welcome');
}

@Help()
async helpCommand(ctx: Context) {
await ctx.reply('Send me a sticker');
}

@Hears('hi')
async hearsHi(ctx: Context) {
await ctx.reply('Hey there');
}

@On('chat_member')
async onNewChatMembers(ctx: Context) {
await this.statsService.createStat({
stat_id: 'telegram_channel_count',
name: 'подписчиков в телеграм канале',
count: await ctx.getChatMembersCount(),
});
}

@On('boost_added')
async onBoost(ctx: Context) {
console.log(ctx.botInfo);
}
}
Loading

0 comments on commit 6e4a592

Please sign in to comment.