mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2024-12-25 15:34:38 +03:00
feat: add ability to use webhook
This commit is contained in:
parent
ac46187050
commit
c546b175d6
10
lib/InvalidConfigurationException.ts
Normal file
10
lib/InvalidConfigurationException.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
export class InvalidConfigurationException extends Error {
|
||||||
|
public constructor(
|
||||||
|
public readonly invalidField,
|
||||||
|
public readonly invalidCause,
|
||||||
|
) {
|
||||||
|
super(
|
||||||
|
`Options validation failed, "${invalidField}" invalid — ${invalidCause}`,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
@ -11,17 +11,21 @@ import { Bot } from './Bot'
|
|||||||
import { TelegramActionHandler } from './decorators/TelegramActionHandler'
|
import { TelegramActionHandler } from './decorators/TelegramActionHandler'
|
||||||
import { TokenInjectionToken } from './TokenInjectionToken'
|
import { TokenInjectionToken } from './TokenInjectionToken'
|
||||||
import { TelegramModuleOptionsFactory } from './TelegramModuleOptionsFactory'
|
import { TelegramModuleOptionsFactory } from './TelegramModuleOptionsFactory'
|
||||||
|
import { InvalidConfigurationException } from 'InvalidConfigurationException'
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class TelegramBot {
|
export class TelegramBot {
|
||||||
private readonly token: string
|
private readonly token: string
|
||||||
|
private readonly sitePublicUrl?: string
|
||||||
private bot: Bot
|
private bot: Bot
|
||||||
private ref: ModuleRef
|
private ref: ModuleRef
|
||||||
|
|
||||||
public constructor(
|
public constructor(
|
||||||
@Inject(TokenInjectionToken) factory: TelegramModuleOptionsFactory,
|
@Inject(TokenInjectionToken) factory: TelegramModuleOptionsFactory,
|
||||||
) {
|
) {
|
||||||
this.token = factory.createOptions().token
|
const { token, sitePublicUrl } = factory.createOptions()
|
||||||
|
this.token = token
|
||||||
|
this.sitePublicUrl = sitePublicUrl
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(ref: ModuleRef) {
|
public init(ref: ModuleRef) {
|
||||||
@ -37,7 +41,22 @@ export class TelegramBot {
|
|||||||
this.bot = bot
|
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()
|
this.bot.startPolling()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
export interface TelegramModuleOptions {
|
export interface TelegramModuleOptions {
|
||||||
token: string
|
token: string
|
||||||
|
sitePublicUrl?: string
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user