1
0
Fork 0
mirror of https://github.com/muerwre/vk-tg-bot.git synced 2025-04-25 15:06:41 +07:00

#5 added webhook probe

This commit is contained in:
Fedor Katurov 2021-05-12 13:45:21 +07:00
parent 1fe3d35540
commit 6344f6dc5a
4 changed files with 35 additions and 5 deletions

View file

@ -11,8 +11,8 @@ import { corsMiddleware, errorMiddleware } from "./middleware";
import { WebhookConfig } from "../../service/telegram/types";
export class HttpApi {
app: Express;
webhook: WebhookConfig;
app!: Express;
webhook!: WebhookConfig;
constructor(
private props: HttpConfig,

View file

@ -1,4 +1,6 @@
import { TelegramService } from "../../service/telegram";
import axios from "axios";
import logger from "../../service/logger";
export class TelegramApi {
constructor(private telegram: TelegramService) {}
@ -16,4 +18,25 @@ export class TelegramApi {
"CAACAgIAAxkBAAIB6F82KSeJBEFer895bb7mFI7_GzYoAAISAAOwODIrOXeFNb5v4aEaBA"
);
}
/**
* Probes webhook url and falls back to polling mode on error
*/
public probe = async () => {
if (!this.telegram.webhook.enabled || !this.telegram.webhook.url) {
return;
}
try {
await axios.get(this.telegram.webhook.url);
logger.info(
`probing telegram webhook at ${this.telegram.webhook.url} succeeded`
);
} catch (e) {
logger.warn(
`probing telegram webhook at ${this.telegram.webhook.url} failed, falling back to polling mode`
);
await this.telegram.bot.launch();
}
};
}