-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from antonkorotkov/dev
Dev
- Loading branch information
Showing
34 changed files
with
1,188 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
RABBITMQ_DEFAULT_USER= | ||
RABBITMQ_DEFAULT_PASS= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
DB_CONNECTION_STRING=mongodb://mongodb:27017/coinMonitor | ||
BOT_TOKEN= | ||
WCI_KEY= | ||
AMQP_CONNECTION_STRING=amqp://<>:<>@rabbitmq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,13 @@ | ||
# Use the official Node.js image as the base image | ||
FROM node:18 | ||
FROM node:20-alpine | ||
|
||
# Create and change to the app directory | ||
WORKDIR /usr/src/app | ||
|
||
# Copy application dependency manifests to the container image. | ||
COPY package*.json ./ | ||
|
||
# Install dependencies | ||
RUN npm install | ||
|
||
# Copy the rest of the application code to the container image. | ||
COPY . . | ||
|
||
ENV DEBUG=* | ||
|
||
# Run the application | ||
CMD ["node", "index.js"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const amqp = require('amqplib'); | ||
|
||
class AMQP { | ||
#connection; | ||
|
||
/** @type {amqp.ConfirmChannel | undefined} */ | ||
#channel; | ||
|
||
/** | ||
* @param {string} connectionString | ||
*/ | ||
constructor(connectionString) { | ||
this.#connection = amqp.connect(connectionString); | ||
} | ||
|
||
/** | ||
* @param {string} queueName | ||
* @returns {Promise<amqp.ConfirmChannel>} | ||
*/ | ||
async getChannel(queueName) { | ||
if (!this.#channel) { | ||
this.#channel = await (await this.#connection).createConfirmChannel(); | ||
|
||
await this.#channel.assertQueue(queueName, { | ||
durable: true | ||
}); | ||
} | ||
|
||
return this.#channel; | ||
} | ||
|
||
async closeChannel() { | ||
await this.#channel.close(); | ||
this.#channel = undefined; | ||
} | ||
} | ||
|
||
module.exports = AMQP; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
const search = async ctx => { | ||
await ctx.conversation.enter('searchConversation'); | ||
const stats = await ctx.conversation.active(); | ||
|
||
if (!Object.keys(stats).length) | ||
await ctx.conversation.enter('searchConversation'); | ||
}; | ||
|
||
module.exports = search; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,67 @@ | ||
module.exports = options => bot => { | ||
const { InlineKeyboard } = require("grammy"); | ||
const Monitor = require("../../db/schemas/Monitor"); | ||
|
||
const TYPE_FIXED = 'fixed'; | ||
const TYPE_PERCENTAGE = 'percentage'; | ||
const CONFIRM_CREATE = 'create'; | ||
const CONFIRM_CANCEL = 'cancel'; | ||
|
||
module.exports = ({ logger }) => _ => { | ||
return async function addMonitorConversation(conversation, ctx) { | ||
await ctx.reply('Add monitor conversation started'); | ||
await ctx.reply(ctx.state.coin); | ||
const typeKeyboard = new InlineKeyboard(); | ||
typeKeyboard.text(ctx.t('monitor_type_fixed'), TYPE_FIXED).text(ctx.t('monitor_type_percentage'), TYPE_PERCENTAGE); | ||
|
||
await ctx.reply(ctx.t('monitor_type_prompt'), { | ||
reply_markup: typeKeyboard | ||
}); | ||
|
||
const monitorTypeCtx = await conversation.waitForCallbackQuery([TYPE_FIXED, TYPE_PERCENTAGE], { | ||
otherwise: ctx => ctx.reply(ctx.t('use_buttons_alert'), { reply_markup: typeKeyboard }), | ||
}); | ||
|
||
await monitorTypeCtx.editMessageText(ctx.t('monitor_threshold_prompt', { type: monitorTypeCtx.match === TYPE_PERCENTAGE ? '%' : '$' }), { | ||
reply_markup: undefined | ||
}); | ||
|
||
const value = await conversation.form.number( | ||
ctx => ctx.reply(ctx.t('monitor_number_validation'), { parse_mode: 'HTML' }) | ||
); | ||
|
||
if (value <= 0 || value > 1_000_000) | ||
return await ctx.reply(ctx.t('monitor_bad_threshold')); | ||
|
||
const confirmationKeyboard = new InlineKeyboard(); | ||
confirmationKeyboard.text(ctx.t('create'), CONFIRM_CREATE).text(ctx.t('cancel'), CONFIRM_CANCEL); | ||
await ctx.reply(ctx.t('add_monitor_confirmation', { type: monitorTypeCtx.match, coin: ctx.state.coin, value: `${value}${monitorTypeCtx.match === TYPE_PERCENTAGE ? '%' : '$'}` }), { | ||
parse_mode: 'HTML', | ||
reply_markup: confirmationKeyboard | ||
}); | ||
|
||
const confirmationCtx = await conversation.waitForCallbackQuery([CONFIRM_CREATE, CONFIRM_CANCEL], { | ||
otherwise: ctx => ctx.reply(ctx.t('use_buttons_alert'), { reply_markup: confirmationKeyboard }), | ||
}); | ||
|
||
if (confirmationCtx.match === CONFIRM_CANCEL) | ||
return await ctx.reply('😒'); | ||
|
||
try { | ||
await Monitor.createOrUpdate({ | ||
telegramId: ctx.chat.id, | ||
coinId: ctx.state.id, | ||
coin: ctx.state.coin, | ||
coinName: ctx.state.name, | ||
lastPrice: ctx.state.price, | ||
threshold: { | ||
type: monitorTypeCtx.match, | ||
value: value | ||
} | ||
}); | ||
|
||
await ctx.reply(ctx.t('monitor_created')); | ||
} catch (err) { | ||
logger.debug('addMonitor')(err); | ||
|
||
const { msg: { text } } = await conversation.waitFor("message:text"); | ||
await ctx.reply('Sorry, there was an error on our side. Please try again later or contact administrator.'); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const { InlineKeyboard } = require("grammy"); | ||
const Monitor = require("../../db/schemas/Monitor"); | ||
|
||
const DELETE_CREATE = 'delete'; | ||
const CONFIRM_CANCEL = 'cancel'; | ||
|
||
module.exports = ({ logger }) => _ => { | ||
return async function deleteMonitorConversation(conversation, ctx) { | ||
try { | ||
const confirmationKeyboard = new InlineKeyboard(); | ||
const { id } = ctx.state; | ||
|
||
if (!id) | ||
return await ctx.reply('Something went wrong. No monitor selected for deletion.'); | ||
|
||
confirmationKeyboard.text(ctx.t('delete'), DELETE_CREATE).text(ctx.t('cancel'), CONFIRM_CANCEL); | ||
await ctx.reply(ctx.t('are_you_sure'), { | ||
reply_markup: confirmationKeyboard | ||
}); | ||
|
||
const confirmationCtx = await conversation.waitForCallbackQuery([DELETE_CREATE, CONFIRM_CANCEL], { | ||
otherwise: ctx => ctx.reply(ctx.t('use_buttons_alert'), { reply_markup: confirmationKeyboard }), | ||
}); | ||
|
||
if (confirmationCtx.match === CONFIRM_CANCEL) | ||
return await ctx.reply('👍'); | ||
|
||
await Monitor.findByIdAndDelete(id); | ||
await ctx.reply(ctx.t('monitor_deleted')); | ||
} catch (err) { | ||
logger.debug('deleteMonitor')(err); | ||
|
||
await ctx.reply('Sorry, there was an error on our side. Please try again later or contact administrator.'); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
const searchConversation = require('./search'); | ||
const addMonitorConversation = require('./addMonitor'); | ||
const deleteMonitorConversation = require('./deleteMonitor'); | ||
|
||
module.exports = { | ||
searchConversation, | ||
addMonitorConversation | ||
addMonitorConversation, | ||
deleteMonitorConversation | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.