Skip to content

Commit

Permalink
configure webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
adidoesnt committed Feb 4, 2024
1 parent 5a5d554 commit 4eaa6f1
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 26 deletions.
Binary file modified telegram-bot/bun.lockb
Binary file not shown.
52 changes: 27 additions & 25 deletions telegram-bot/package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
{
"name": "echofinder-telegram",
"module": "src/index.ts",
"type": "module",
"author": "Aditya Banerjee",
"scripts": {
"format": "prettier --write .",
"start": "bun run src/index.ts",
"dev": "bun run --hot src/index.ts",
"build": "docker build -t echofinder-telegram:1.0 .",
"local": "docker run --env-file .env echofinder-telegram:1.0",
"prod": "docker run echofinder-telegram:1.0"
},
"devDependencies": {
"@types/bun": "latest",
"@types/node-telegram-bot-api": "^0.64.2",
"prettier": "^3.2.5"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"axios": "^1.6.7",
"log4js": "^6.9.1",
"node-telegram-bot-api": "^0.64.0"
}
"name": "echofinder-telegram",
"module": "src/index.ts",
"type": "module",
"author": "Aditya Banerjee",
"scripts": {
"format": "prettier --write .",
"start": "bun run src/index.ts",
"dev": "bun run --hot src/index.ts",
"build": "docker build -t echofinder-telegram:1.0 .",
"local": "docker run --env-file .env echofinder-telegram:1.0",
"prod": "docker run echofinder-telegram:1.0"
},
"devDependencies": {
"@types/bun": "latest",
"@types/node-telegram-bot-api": "^0.64.2",
"prettier": "^3.2.5"
},
"peerDependencies": {
"typescript": "^5.0.0"
},
"dependencies": {
"axios": "^1.6.7",
"body-parser": "^1.20.2",
"express": "^4.18.2",
"log4js": "^6.9.1",
"node-telegram-bot-api": "^0.64.0"
}
}
12 changes: 11 additions & 1 deletion telegram-bot/src/components/bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { MESSAGE } from 'constants/message';
import { ERROR } from 'constants/error';
import { ApiClient } from './apiClient';
import { commands } from 'constants/command';
import type { Request, Response } from 'express';

const {
TELEGRAM_BOT_TOKEN: token = '',
Expand Down Expand Up @@ -32,10 +33,14 @@ export class Bot {
return Bot.instance;
}

setWebhook(): void {
this.client.setWebHook(webhook_url + token);
}

initialize(): void {
this.logger.info('Initialising bot');
this.client.setMyCommands(commands);
if (env !== 'DEV') this.client.setWebHook(webhook_url);
if (env !== 'DEV') this.setWebhook();
this.client.onText(/\/(start|help)/, (message: Message) => {
this.help(message);
});
Expand Down Expand Up @@ -151,4 +156,9 @@ export class Bot {
this.logger.error(error);
}
}

processUpdate(req: Request, response: Response) {
this.client.processUpdate(req.body);
response.status(200).json({ message: MESSAGE.PROCESSING_UPDATE });
}
}
34 changes: 34 additions & 0 deletions telegram-bot/src/components/server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import express, { type Express } from 'express';
import { json } from 'body-parser';
import type { Bot } from './bot';

const { PORT: port = 3000, TELEGRAM_BOT_TOKEN: token = '' } = process.env;

export class Server {
static instance: Server;
private app: Express;
private readonly port: number;

private constructor() {
this.app = express();
this.port = Number(port);
this.app.use(json());
this.app.listen(this.port, () => {
console.log(`Server running on port ${this.port}`);
});
}

static getInstance(): Server {
if (!Server.instance) {
Server.instance = new Server();
}
return Server.instance;
}

setupBot(bot: Bot): void {
this.app.post(`/${token}`, (req, res) => {
bot.processUpdate(req, res);
res.sendStatus(200);
});
}
}
1 change: 1 addition & 0 deletions telegram-bot/src/constants/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export const MESSAGE = {
HELP: 'This is the EchoFinder Telegram bot. It allows you to search for messages, and returns results based on similarity. Usage: /search <search string>',
PROMPT: 'Search usage: /search <search string>',
NO_RESULTS: 'No results found. Try a different search string.',
PROCESSING_UPDATE: 'Processing update'
};
8 changes: 8 additions & 0 deletions telegram-bot/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Bot } from 'components/bot';
import { Server } from 'components/server';

const { NODE_ENV: env = 'DEV' } = process.env;

const bot = Bot.getInstance();
bot.initialize();

if (env !== 'DEV') {
const server = Server.getInstance();
server.setupBot(bot);
}

0 comments on commit 4eaa6f1

Please sign in to comment.