diff --git a/.env.example b/.env.example index 23c7cff..4f393e7 100644 --- a/.env.example +++ b/.env.example @@ -9,4 +9,5 @@ POSTGRES_PORT=5432 POSTGRES_DB=root AUTO_MIGRATE=True -BOT_KEY=botkey \ No newline at end of file +BOT_KEY=botkey +TELEGRAM_CHANNEL_ID=-1002123654210 \ No newline at end of file diff --git a/src/schema.gql b/src/schema.gql index 4c8f02a..c860cf3 100644 --- a/src/schema.gql +++ b/src/schema.gql @@ -2,21 +2,11 @@ # THIS FILE WAS AUTOMATICALLY GENERATED (DO NOT MODIFY) # ------------------------------------------------------ -input CreateStatInput { - count: Int! - name: String! - stat_id: String! -} - """ A date-time string at UTC, such as 2019-12-03T09:54:33Z, compliant with the date-time format. """ scalar DateTime -type Mutation { - createStat(createStatInput: CreateStatInput!): Stat! -} - type Query { stats: [Stat!]! } diff --git a/src/telegram-stats/telegram-stats.service.ts b/src/telegram-stats/telegram-stats.service.ts index b8ba1e0..81d33d1 100644 --- a/src/telegram-stats/telegram-stats.service.ts +++ b/src/telegram-stats/telegram-stats.service.ts @@ -1,7 +1,7 @@ import { Injectable } from '@nestjs/common'; -import { Hears, Help, On, Start, Update } from 'nestjs-telegraf'; +import { On, Start, Update } from 'nestjs-telegraf'; import { StatsService } from 'src/stats/stats.service'; -import { Context } from 'telegraf'; +import { Context, Markup } from 'telegraf'; // todo // check the telegram id's for god's sake @@ -13,6 +13,8 @@ export class TelegramStatsService { @On('chat_member') async onNewChatMembers(ctx: Context) { + if (ctx.chat.id !== parseInt(process.env.TELEGRAM_CHANNEL_ID)) return; + const currentCount = (await this.statsService.findLatest()).find( (x) => x.stat_id == 'telegram_channel_count' ).count; @@ -36,25 +38,89 @@ export class TelegramStatsService { } } + @Start() + async startCommand(ctx: Context) { + const isAdmin = ctx.telegram + .getChatMember(parseInt(process.env.TELEGRAM_CHANNEL_ID), ctx.from.id) + .then((x) => x.status === 'administrator' || x.status === 'creator'); + + if (!isAdmin) return; + + await ctx.reply( + 'Welcome! Use the keyboard to update stats. To update a custom value send a message in the following format \n\n [stat_id] [count] \n\n coffee_cups \n sleepless_nights \n github_commit_count \n telegram_channel_count \n telegram_channel_joined \n commits', + Markup.keyboard([ + Markup.button.callback('Update Coffee Cups', 'update_coffee_cups'), + Markup.button.callback( + 'Update Sleepless Nights', + 'update_sleepless_nights' + ), + ]) + ); + } + @On('text') - async onComment(ctx: Context) { - if (ctx.chat.type === 'private') { - console.info('updated stat'); - const stat_id = ctx.text.split(' ')[0]; - const count = parseInt(ctx.text.split(' ')[1]); - const name = ctx.text.split(' ').slice(2).join(' '); + async onText(ctx: Context) { + const message = ctx.text; + + const isAdmin = ctx.telegram + .getChatMember(parseInt(process.env.TELEGRAM_CHANNEL_ID), ctx.from.id) + .then((x) => x.status === 'administrator' || x.status === 'creator'); + + if (!isAdmin) return; + + if (message === 'Update Coffee Cups') { + const currentStat = (await this.statsService.findLatest()).find( + (x) => x.stat_id == 'coffee_cups' + ); + + const updatedCount = currentStat ? currentStat.count + 1 : 1; + await this.statsService.createStat({ - stat_id, - count, - name, + stat_id: 'coffee_cups', + name: currentStat.name, + count: updatedCount, }); + + await ctx.reply(`Coffee cups updated to ${updatedCount}!`); + } else if (message === 'Update Sleepless Nights') { + const currentStat = (await this.statsService.findLatest()).find( + (x) => x.stat_id == 'sleepless_nights' + ); + + const updatedCount = currentStat ? currentStat.count + 1 : 1; + + await this.statsService.createStat({ + stat_id: 'sleepless_nights', + name: currentStat.name, + count: updatedCount, + }); + + await ctx.reply(`Sleepless nigts updated to ${updatedCount}!`); } else { - console.info('added comment'); + const custom_data = message.split(' '); + if (custom_data.length > 2) { + await ctx.reply(`Unknown format.`); + return; + } + + const currentStat = (await this.statsService.findLatest()).find( + (x) => x.stat_id == custom_data[0] + ); + + if (currentStat == undefined) { + await ctx.reply(`Unknown current stat.`); + return; + } + await this.statsService.createStat({ - stat_id: 'telegram_channel_commented', - name: `"${ctx.text}"`, - count: 0, + stat_id: currentStat.stat_id, + name: currentStat.name, + count: parseInt(custom_data[1]), }); + + await ctx.reply( + `${currentStat.name} updated to ${parseInt(custom_data[1])}!` + ); } } }