diff --git a/lib/InvalidConfigurationException.ts b/lib/InvalidConfigurationException.ts new file mode 100644 index 0000000..deddd73 --- /dev/null +++ b/lib/InvalidConfigurationException.ts @@ -0,0 +1,10 @@ +export class InvalidConfigurationException extends Error { + public constructor( + public readonly invalidField, + public readonly invalidCause, + ) { + super( + `Options validation failed, "${invalidField}" invalid — ${invalidCause}`, + ) + } +} diff --git a/lib/TelegramBot.ts b/lib/TelegramBot.ts index 2570315..f151aee 100644 --- a/lib/TelegramBot.ts +++ b/lib/TelegramBot.ts @@ -11,17 +11,21 @@ import { Bot } from './Bot' import { TelegramActionHandler } from './decorators/TelegramActionHandler' import { TokenInjectionToken } from './TokenInjectionToken' import { TelegramModuleOptionsFactory } from './TelegramModuleOptionsFactory' +import { InvalidConfigurationException } from 'InvalidConfigurationException' @Injectable() export class TelegramBot { private readonly token: string + private readonly sitePublicUrl?: string private bot: Bot private ref: ModuleRef public constructor( @Inject(TokenInjectionToken) factory: TelegramModuleOptionsFactory, ) { - this.token = factory.createOptions().token + const { token, sitePublicUrl } = factory.createOptions() + this.token = token + this.sitePublicUrl = sitePublicUrl } public init(ref: ModuleRef) { @@ -37,7 +41,22 @@ export class TelegramBot { this.bot = bot } - public start() { + public getMiddleware(path: string) { + if (!this.sitePublicUrl) { + throw new InvalidConfigurationException( + 'sitePublicUrl', + 'does not exist, but webook used', + ) + } + + this.bot.telegram + .setWebhook(`${this.sitePublicUrl}/${path}`) + .then(() => console.log('Webhook set success')) + + return this.bot.webhookCallback(path) + } + + public startPolling() { this.bot.startPolling() } diff --git a/lib/TelegramModuleOptions.ts b/lib/TelegramModuleOptions.ts index ceb7124..5eef4f4 100644 --- a/lib/TelegramModuleOptions.ts +++ b/lib/TelegramModuleOptions.ts @@ -1,3 +1,4 @@ export interface TelegramModuleOptions { token: string + sitePublicUrl?: string }