mirror of
https://github.com/muerwre/vk-tg-bot.git
synced 2025-04-25 23:16:41 +07:00
added working webhooks
This commit is contained in:
parent
f444a7d194
commit
4076b4427d
9 changed files with 89 additions and 31 deletions
|
@ -4,19 +4,20 @@ import logger from "../logger";
|
|||
import { Response } from "express";
|
||||
import { Update } from "typegram";
|
||||
import loggerTgMiddleware from "../logger/tg";
|
||||
import { WebhookConfig } from "../../config/types";
|
||||
|
||||
// import SocksProxyAgent from 'socks-proxy-agent';
|
||||
|
||||
export class TelegramService {
|
||||
public readonly bot: Telegraf;
|
||||
|
||||
constructor(private props: TelegramConfig) {
|
||||
constructor(private props: TelegramConfig, private webhook: WebhookConfig) {
|
||||
// const agent = (CONFIG.PROXY && new SocksProxyAgent(CONFIG.PROXY)) || null;
|
||||
const options = {
|
||||
channelMode: true,
|
||||
const options: Partial<Telegraf.Options<any>> = {
|
||||
telegram: {
|
||||
webhookReply: true,
|
||||
apiMode: "bot",
|
||||
// agent, // TODO: add proxy support
|
||||
webhookReply: !!props.webhookUrl,
|
||||
},
|
||||
};
|
||||
|
||||
|
@ -31,10 +32,25 @@ export class TelegramService {
|
|||
* Connects to telegram
|
||||
*/
|
||||
public async start() {
|
||||
await this.bot.telegram.deleteWebhook().then(
|
||||
() => this.bot.launch(),
|
||||
() => this.bot.launch()
|
||||
);
|
||||
if (this.webhook.enabled && this.webhook.url) {
|
||||
await this.bot.telegram
|
||||
.deleteWebhook()
|
||||
.then(() => this.bot.telegram.setWebhook(this.webhook.url))
|
||||
.then(async () => {
|
||||
const info = await this.bot.telegram.getWebhookInfo();
|
||||
if (!info.url) {
|
||||
throw new Error(`telegram hasn't set webhook`);
|
||||
}
|
||||
|
||||
logger.info(`telegram started webhook at ${this.webhook.url}`);
|
||||
})
|
||||
.catch(logger.warn);
|
||||
} else {
|
||||
await this.bot.telegram.deleteWebhook().then(
|
||||
() => this.bot.launch(),
|
||||
() => this.bot.launch()
|
||||
);
|
||||
}
|
||||
|
||||
logger.info("telegram service started");
|
||||
}
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
export interface TelegramConfig {
|
||||
key: string
|
||||
webhookUrl?: string
|
||||
key: string;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import * as yup from 'yup';
|
||||
import * as yup from "yup";
|
||||
|
||||
export const telegramConfigSchema = yup.object().required().shape({
|
||||
key: yup.string().required(),
|
||||
webhookUrl: yup.string().notRequired(),
|
||||
})
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue