1
0
Fork 0
mirror of https://github.com/muerwre/vk-tg-bot.git synced 2025-04-24 22:46: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();
}
};
}

View file

@ -16,10 +16,15 @@ async function main() {
const telegram = new TelegramService(config.telegram);
const vkService = new VkService(config.vk, telegram, config.templates, db);
const telegramApi = new TelegramApi(telegram).listen();
const telegramApi = new TelegramApi(telegram);
telegramApi.listen();
await telegram.start();
const httpApi = new HttpApi(config.http, telegram, vkService).listen();
const httpApi = new HttpApi(config.http, telegram, vkService);
await httpApi.listen();
await telegramApi.probe();
} catch (e) {
logger.error(e.message);
}

View file

@ -10,9 +10,11 @@
likes: ['😱','🤔','😃']
---
{{!--
use handlebars template here
available variables are: text, user, group, type
(see PostNewHandler)
(see PostNewHandler)
--}}
{{#ifEq type 'suggest'}}
Предложка: