Skip to content

Commit

Permalink
✉️ Enable Telegram bot
Browse files Browse the repository at this point in the history
  • Loading branch information
jmsalcido committed Dec 14, 2023
1 parent b3e8e81 commit 3baa58a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
40 changes: 40 additions & 0 deletions src/bot/telegram/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {Context, Telegraf} from "telegraf";
import {getBotMessage} from "../../data/network";
import logger from "../../logger";
import {Update} from "telegraf/typings/core/types/typegram";

const TELEGRAM_BOT_TOKEN = process.env.TELEGRAM_BOT_TOKEN;

export const initTelegramBot = async () => {
if (TELEGRAM_BOT_TOKEN) {
const bot = new Telegraf(TELEGRAM_BOT_TOKEN);
const response = async (ctx: Context<Update>) => {
if (ctx.message && 'text' in ctx.message && ctx.reply) {
const message = await getBotMessage()
await ctx.reply(message);
}
};

bot.command('dolar', response);
bot.command('dólar', response);
bot.command('dollar', response);
bot.command('about', (ctx) => ctx.reply('Dolarenbancos bot. More info at https://dolarenbancos.com.'));

bot.catch((err) => {
logger.error(err);
});

bot.start((ctx) => ctx.reply('STARTED'));

bot.launch()
.then(() => {
logger.info('* Telegram bot [ONLINE]');
})
.catch((err) => {
logger.info('* Telegram bot couldn\'t run');
logger.error(err);
});
} else {
logger.error('TELEGRAM_TOKEN not available');
}
}
8 changes: 6 additions & 2 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import express, {Application} from 'express';
import apiRoutes from './routes/apiRoutes';
import {initDiscord} from './bot/discord';
import logger from "./logger";
import {initTelegramBot} from "./bot/telegram";

const app: Application = express();
const port = 3000;
Expand All @@ -11,5 +12,8 @@ app.listen(port, () => {
logger.info(`Server is running on port ${port}`);
});

initDiscord()
.then(_value => logger.info('Discord bot initialized'))

Promise.all([
initDiscord(),
initTelegramBot()
]).then(_value => logger.info('Bots are initialized'))

0 comments on commit 3baa58a

Please sign in to comment.