generated from QuantGeekDev/ts-tg-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
251b3fa
commit 98af5f8
Showing
17 changed files
with
148 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,33 @@ | ||
import { loadEnv } from '../helpers/load-env.js' | ||
import { validateEnv } from '../helpers/validate-env.js' | ||
import { startBot } from './bot.js' | ||
import { connectToDb } from './database.js' | ||
import debugCreator from 'debug' | ||
import { loadEnv } from '../helpers/load-env.js'; | ||
import { validateEnv } from '../helpers/validate-env.js'; | ||
import { startBot } from './bot.js'; | ||
import { connectToDb } from './database.js'; | ||
import debugCreator from 'debug'; | ||
|
||
const debug = debugCreator('app:') | ||
const debug = debugCreator('app:'); | ||
|
||
export async function startApp() { | ||
debug('Starting app...') | ||
debug('Starting app...'); | ||
try { | ||
loadEnv() | ||
validateEnv(['TELEGRAM_TOKEN', 'DB_CONNECTION_STRING']) | ||
loadEnv(); | ||
validateEnv(['TELEGRAM_TOKEN', 'DB_CONNECTION_STRING']); | ||
} catch (error) { | ||
console.error('Error occurred while loading environment:', error) | ||
process.exit(1) | ||
console.error('Error occurred while loading environment:', error); | ||
process.exit(1); | ||
} | ||
|
||
let database | ||
let database; | ||
try { | ||
database = await connectToDb() | ||
database = await connectToDb(); | ||
} catch (error) { | ||
console.error('Error occurred while connecting to the database:', error) | ||
process.exit(2) | ||
console.error('Error occurred while connecting to the database:', error); | ||
process.exit(2); | ||
} | ||
|
||
try { | ||
await startBot(database) | ||
await startBot(database); | ||
} catch (error) { | ||
console.error('Error occurred while starting the bot:', error) | ||
process.exit(3) | ||
console.error('Error occurred while starting the bot:', error); | ||
process.exit(3); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import { MongoClient } from 'mongodb' | ||
import type { Chat, Database, User } from '../types/database.js' | ||
import { MongoClient } from 'mongodb'; | ||
import type { Chat, Database, User } from '../types/database.js'; | ||
|
||
export async function connectToDb() { | ||
const client = new MongoClient(process.env.DB_CONNECTION_STRING) | ||
await client.connect() | ||
const mongoDb = client.db() | ||
const user = mongoDb.collection<User>('user') | ||
const chat = mongoDb.collection<Chat>('chat') | ||
const database: Database = { user, chat } | ||
return database | ||
const client = new MongoClient(process.env.DB_CONNECTION_STRING); | ||
await client.connect(); | ||
const mongoDb = client.db(); | ||
const user = mongoDb.collection<User>('user'); | ||
const chat = mongoDb.collection<Chat>('chat'); | ||
const database: Database = { user, chat }; | ||
return database; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import { I18n } from '@grammyjs/i18n' | ||
import { I18n } from '@grammyjs/i18n'; | ||
|
||
export function initLocaleEngine(path: string, defaultLanguage = 'en') { | ||
const i18n = new I18n({ | ||
directory: path, | ||
defaultLanguage: defaultLanguage, | ||
defaultLanguageOnMissing: true, | ||
useSession: true | ||
}) | ||
return i18n | ||
}); | ||
return i18n; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { Composer } from 'grammy' | ||
import type { CustomContext } from '../types/context.js' | ||
import { Composer } from 'grammy'; | ||
import type { CustomContext } from '../types/context.js'; | ||
|
||
export const startController = new Composer<CustomContext>() | ||
export const startController = new Composer<CustomContext>(); | ||
startController.command('start', async ctx => { | ||
await ctx.text('start', { | ||
name: ctx.entities.user.name, | ||
chatName: ctx.entities.chat?.title ?? 'PM' | ||
}) | ||
}) | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
import { Composer } from 'grammy' | ||
import type { CustomContext } from '../types/context.js' | ||
import { Composer } from 'grammy'; | ||
import type { CustomContext } from '../types/context.js'; | ||
|
||
export const stopController = new Composer<CustomContext>() | ||
export const stopController = new Composer<CustomContext>(); | ||
stopController.command('stop', async ctx => { | ||
await ctx.text('stop', { | ||
name: ctx.entities.user.name, | ||
chatName: ctx.entities.chat?.title ?? 'PM' | ||
}) | ||
}) | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
export declare global { | ||
namespace NodeJS { | ||
interface ProcessEnv { | ||
TELEGRAM_TOKEN: string | ||
DB_CONNECTION_STRING: string | ||
TELEGRAM_TOKEN: string; | ||
DB_CONNECTION_STRING: string; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import dotenv from 'dotenv' | ||
import { resolvePath } from './resolve-path.js' | ||
import dotenv from 'dotenv'; | ||
import { resolvePath } from './resolve-path.js'; | ||
|
||
export function loadEnv(configPath = '../../.env') { | ||
const fullPath = resolvePath(import.meta.url, configPath) | ||
const fullPath = resolvePath(import.meta.url, configPath); | ||
dotenv.config({ | ||
path: fullPath, | ||
}) | ||
path: fullPath | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import pathModule from 'path' | ||
import { fileURLToPath } from 'url' | ||
import pathModule from 'path'; | ||
import { fileURLToPath } from 'url'; | ||
|
||
export function resolvePath(localPath: string, targetPath: string) { | ||
const __filename = fileURLToPath(localPath) | ||
const __dirname = pathModule.dirname(__filename) | ||
return pathModule.join(__dirname, targetPath) | ||
const __filename = fileURLToPath(localPath); | ||
const __dirname = pathModule.dirname(__filename); | ||
return pathModule.join(__dirname, targetPath); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
export function validateEnv(requiredEnvs: string[]) { | ||
for (const env of requiredEnvs) { | ||
if (process.env[env] === undefined) { | ||
throw new Error(`ERROR: Required variable "${env}" is not specified`) | ||
throw new Error(`ERROR: Required variable "${env}" is not specified`); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,8 @@ | ||
import { startApp } from './config/app.js' | ||
import debugCreator from 'debug' | ||
import { startApp } from './config/app.js'; | ||
import debugCreator from 'debug'; | ||
|
||
const debug = debugCreator('index:') | ||
const debug = debugCreator('index:'); | ||
|
||
debug('Starting App') | ||
startApp() | ||
debug('App started') | ||
debug('Starting App'); | ||
startApp(); | ||
debug('App started'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,34 @@ | ||
import type { Chat, Database } from '../types/database.js' | ||
import type { Chat, Database } from '../types/database.js'; | ||
|
||
async function createChat(args: { | ||
db: Database | ||
chatId: number | ||
title: string | ||
db: Database; | ||
chatId: number; | ||
title: string; | ||
}): Promise<Chat> { | ||
const chatObject = { | ||
chatId: args.chatId, | ||
title: args.title | ||
} as Chat | ||
} as Chat; | ||
|
||
await args.db.chat.insertOne(chatObject) | ||
await args.db.chat.insertOne(chatObject); | ||
|
||
return chatObject | ||
return chatObject; | ||
} | ||
|
||
export async function getOrCreateChat(args: { | ||
db: Database | ||
chatId: number | ||
title: string | ||
db: Database; | ||
chatId: number; | ||
title: string; | ||
}): Promise<Chat> { | ||
const chat = await args.db.chat.findOneAndUpdate( | ||
{ chatId: args.chatId }, | ||
{ $set: { title: args.title } }, | ||
{ returnDocument: 'after' } | ||
) | ||
); | ||
|
||
if (chat.ok && chat.value) { | ||
return chat.value | ||
return chat.value; | ||
} | ||
|
||
return createChat(args) | ||
return createChat(args); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
import type { CustomContext, CustomContextMethods } from '../types/context.js' | ||
import type { CustomContext, CustomContextMethods } from '../types/context.js'; | ||
|
||
export function createReplyWithTextFunc( | ||
ctx: CustomContext | ||
): CustomContextMethods['text'] { | ||
return (resourceKey, templateData, extra = {}) => { | ||
extra.parse_mode = 'HTML' | ||
extra.disable_web_page_preview = true | ||
const text = ctx.i18n.t(resourceKey, templateData) | ||
return ctx.reply(text, extra) | ||
} | ||
extra.parse_mode = 'HTML'; | ||
extra.disable_web_page_preview = true; | ||
const text = ctx.i18n.t(resourceKey, templateData); | ||
return ctx.reply(text, extra); | ||
}; | ||
} |
Oops, something went wrong.