123 lines
3.8 KiB
TypeScript
123 lines
3.8 KiB
TypeScript
import express, { Response } from 'express';
|
|
import cors from 'cors';
|
|
import dotenv from 'dotenv';
|
|
import path from 'path';
|
|
import Bot from './bot';
|
|
import { HttpInstanceFactory } from './utils/HttpsInstanceFactory';
|
|
import { TypedRequestBody } from './types/express.type';
|
|
import { getDifferenceInDays } from './utils/GetDifferenceInDays';
|
|
|
|
dotenv.config({ path: path.resolve(__dirname, '../.env') });
|
|
|
|
export type NotifyParams = {
|
|
telegram_id: string[] | number[];
|
|
exchange_id: string | number;
|
|
};
|
|
|
|
export let bot: Bot;
|
|
const app = express();
|
|
|
|
const PORT = process.env.PORT as string;
|
|
const restartTimeout = 10;
|
|
|
|
app.use(express.json());
|
|
app.use(cors());
|
|
|
|
app.post('/notify', async (req: TypedRequestBody<NotifyParams>, res: Response) => {
|
|
bot.notifyByNewExchange(req.body);
|
|
|
|
return res.status(200).send({});
|
|
});
|
|
|
|
app.get('/link-bot', async (req, res) => {
|
|
const linkBot = bot.getBotLink();
|
|
|
|
return res.status(200).send({ link: linkBot });
|
|
});
|
|
|
|
app.get('/config-bot-data', async (req, res) => {
|
|
HttpInstanceFactory.getAuthorizedInstance()
|
|
.get('/config')
|
|
.then(async response => {
|
|
const { token, dateLifetimeJwt } = response.data.data.attributes;
|
|
|
|
if (!token || getDifferenceInDays(new Date(), dateLifetimeJwt) >= 30) {
|
|
const baseInstance = HttpInstanceFactory.getBaseInstance();
|
|
const dateLifeTime = new Date();
|
|
|
|
const user = await baseInstance.post(`auth/local`, {
|
|
identifier: process.env.STRAPI_USER ?? '',
|
|
password: process.env.STRAPI_PASSWORD ?? '',
|
|
});
|
|
|
|
await HttpInstanceFactory.getAuthorizedInstance().put(`config`, {
|
|
data: {
|
|
token: user.data.jwt,
|
|
dateLifetimeJwt: dateLifeTime,
|
|
},
|
|
});
|
|
|
|
return res.status(200).send({
|
|
token: user.data.jwt,
|
|
dateLifetimeJwt: dateLifeTime,
|
|
});
|
|
} else {
|
|
return res.status(200).send({
|
|
token: token,
|
|
dateLifetimeJwt: dateLifetimeJwt,
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
app.post('/restart-bot', async (req, res) => {
|
|
if (bot) {
|
|
try {
|
|
await bot.stop();
|
|
console.log('Bot stopped successfully.');
|
|
} catch (error) {
|
|
console.log('Error stopping the bot:', error);
|
|
}
|
|
}
|
|
|
|
launchBot();
|
|
|
|
return res.status(200).send({ message: 'Bot is restarting...' });
|
|
});
|
|
|
|
const launchBot = async () => {
|
|
console.log(`Starting on port ${PORT}...`);
|
|
|
|
HttpInstanceFactory.updateAuthorizedInstance()
|
|
.then(() => {
|
|
HttpInstanceFactory.getAuthorizedInstance()
|
|
.get('bot-config')
|
|
.then(res => {
|
|
bot = new Bot(res.data.data.attributes.token);
|
|
bot.launch().catch(() => {
|
|
console.log(`ERROR: launching bot. Restarting in ${restartTimeout} sec.`);
|
|
setTimeout(() => {
|
|
launchBot();
|
|
}, restartTimeout * 1000);
|
|
});
|
|
bot.start();
|
|
})
|
|
.catch(() => {
|
|
console.log(`ERROR: getting bot config. Restarting in ${restartTimeout} sec.`);
|
|
setTimeout(() => {
|
|
launchBot();
|
|
}, restartTimeout * 1000);
|
|
});
|
|
})
|
|
.catch(() => {
|
|
console.log(`ERROR: authorization. Restarting in ${restartTimeout} sec.`);
|
|
setTimeout(() => {
|
|
launchBot();
|
|
}, restartTimeout * 1000);
|
|
});
|
|
};
|
|
|
|
app.listen(PORT, () => {
|
|
launchBot();
|
|
});
|