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

feat: run bot's setMy... methods #24

Merged
merged 6 commits into from
Apr 8, 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
2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"scripts": {
"dev": "tsx --watch ./src/main.ts",
"start": "tsx ./src/main.ts",
"start:set-my": "tsx ./src/main.ts --set-my",
"typecheck": "tsc --noEmit",
"prisma:studio": "prisma studio --schema ./src/services/database/schema.prisma",
"prisma:generate": "prisma generate --schema ./src/services/database/schema.prisma",
Expand All @@ -20,6 +21,7 @@
"@telegum/grammy-messages": "^0.5.1",
"@telegum/tgx": "^0.2.1",
"axios": "^1.6.8",
"commander": "^12.0.0",
"dotenv": "^16.4.5",
"grammy": "^1.22.4",
"jsdom": "^24.0.0",
Expand Down
55 changes: 55 additions & 0 deletions backend/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,76 @@
import process from 'node:process'
import { program } from 'commander'
import { run } from '@grammyjs/runner'
import { Bot } from 'grammy'
import type { Config } from './config'
import { loadConfigFromEnv } from './config'
import type { Logger } from './utils/logging'
import { createLogger } from './utils/logging'
import { createBot } from './bot'
import { Domain } from './domain'
import { createDatabase } from './services/database'
import { SportClient } from './services/sport'
import { InnohassleClient } from './services/innohassle'
import translations from './translations'

async function main() {
program
.option('--set-my', 'Call bot\'s `setMy...` methods before starting the bot.', false)
.option('--set-my-only', 'Only call bot\'s `setMy...` methods without starting the bot.', false)

program.parse()

const options = program.opts()
const config = loadConfigFromEnv()
const logger = createLogger(
config.environment === 'development'
? { level: 'debug', pretty: true }
: { level: 'info', pretty: false },
)

if (options.setMyOnly) {
await setMy(config, logger)
return
}

if (options.setMy) {
try {
await setMy(config, logger)
} catch (err) {
console.warn('Error calling \'setMy...\' methods:', err)
}
}

await runBot(config, logger)
}

async function setMy(config: Config, logger: Logger) {
const bot = new Bot(config.bot.token)

logger.info('setMyCommands(default)...')
await bot.api.setMyCommands(
Object.entries(translations.en['Bot.Commands']).map(([command, description]) => ({ command, description })),
{ scope: { type: 'all_private_chats' } },
)
logger.info('setMyDescription(default)...')
await bot.api.setMyDescription(translations.en['Bot.About'])
logger.info('setMyShortDescription(default)...')
await bot.api.setMyShortDescription(translations.en['Bot.Bio'])

for (const [language, translation] of Object.entries(translations).filter(([lang]) => lang !== 'en')) {
logger.info(`setMyCommands(${language})...`)
await bot.api.setMyCommands(
Object.entries(translation['Bot.Commands']).map(([command, description]) => ({ command, description })),
{ scope: { type: 'all_private_chats' }, language_code: language },
)
logger.info(`setMyDescription(${language})...`)
await bot.api.setMyDescription(translation['Bot.About'], { language_code: language })
logger.info(`setMyShortDescription(${language})...`)
await bot.api.setMyShortDescription(translation['Bot.Bio'], { language_code: language })
}
}

async function runBot(config: Config, logger: Logger) {
const db = await createDatabase({
logger: logger.child({ tag: 'database' }),
connectionUrl: config.postgresUrl,
Expand Down
7 changes: 7 additions & 0 deletions backend/src/translations/_en.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ const INSPIRING_QUOTES = [
]

export default {
'Bot.Commands': {
menu: 'send main menu',
help: 'about this bot',
},
'Bot.About': 'I help Innopolis University students to track their sports progress, check-in for training classes and export personal calendar.',
'Bot.Bio': 'Easy sports management for Innopolis University students.\n\ngithub.com/one-zero-eight/sport-bot',

'Messages.WelcomeUnauthorized': (
<>
<b>Welcome to IU Sport Bot! 💪</b>
Expand Down
7 changes: 7 additions & 0 deletions backend/src/translations/_ru.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ const INSPIRING_QUOTES = [
]

export default {
'Bot.Commands': {
menu: 'главное меню',
help: 'о боте',
},
'Bot.About': 'Я помогаю студентам Иннополиса следить за прогрессом по спорту и записываться на занятия в пару кликов, а ещё показываю личный календарь со всеми занятиями.',
'Bot.Bio': 'Помогаю закрывать спорт студентам Иннополиса.\n\ngithub.com/one-zero-eight/sport-bot',

'Messages.WelcomeUnauthorized': (
<>
<b>Привет, я IU Sport бот! 💪</b>
Expand Down
2 changes: 1 addition & 1 deletion bot.Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ COPY ./backend ./backend

RUN pnpm run -C backend prisma:generate

CMD ["pnpm", "run", "-C", "backend", "start"]
CMD ["pnpm", "run", "-C", "backend", "start:set-my"]
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading