diff --git a/package-lock.json b/package-lock.json index 9a6ea5c..a0f9538 100644 --- a/package-lock.json +++ b/package-lock.json @@ -205,6 +205,13 @@ "url": "0.10.3", "uuid": "3.3.2", "xml2js": "0.4.19" + }, + "dependencies": { + "uuid": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz", + "integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==" + } } }, "balanced-match": { @@ -2586,9 +2593,9 @@ } }, "uuid": { - "version": "3.3.2", - "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.3.2.tgz", - "integrity": "sha512-yXJmeNaw3DnnKAOKJE51sL/ZaYfWJRl1pK9dr19YFCu0ObS231AB1/LbqTKRAQ5kw8A90rA6fr4riOUpTZvQZA==" + "version": "7.0.2", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-7.0.2.tgz", + "integrity": "sha512-vy9V/+pKG+5ZTYKf+VcphF5Oc6EFiu3W8Nv3P3zIh0EqVI80ZxOzuPfe9EHjkFNvf8+xuTHVeei4Drydlx4zjw==" }, "v8-compile-cache": { "version": "2.1.0", diff --git a/package.json b/package.json index 3427748..131efd3 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,8 @@ "aws-sdk": "^2.628.0", "dotenv": "^8.2.0", "got": "^10.6.0", - "telegraf": "^3.36.0" + "telegraf": "^3.36.0", + "uuid": "^7.0.2" }, "devDependencies": { "nodemon": "^2.0.2", diff --git a/src/api/FinanceApi.js b/src/api/FinanceApi.js new file mode 100644 index 0000000..1a3220a --- /dev/null +++ b/src/api/FinanceApi.js @@ -0,0 +1,43 @@ +const Http = require('../utils/http'); + +class FinanceApi { + generateMessageTemplate() { + const banks = ['ПриватБанк', 'Укрсиббанк']; + const currencyIgnore = ['RUB']; + + const findedBanks = this.data.organizations.reduce((acc, val) => { + if (banks.includes(val.title)) { + return [...acc, val]; + } + return [...acc]; + }, []); + + return findedBanks + .map(bank => { + const firstLine = `${bank.title}: \n`; + const nextLines = Object.keys(bank.currencies).reduce((acc, key) => { + const currency = bank.currencies[key]; + if (currencyIgnore.includes(key)) { + return [...acc]; + } + return [ + ...acc, + `${key}: ${parseInt(currency.ask).toFixed(2)}/${parseInt( + currency.bid + ).toFixed(2)}` + ]; + }, []); + return firstLine + nextLines.join('\n'); + }) + .join('\n\n'); + } + + async getData() { + this.data = await Http.get( + 'http://resources.finance.ua/ru/public/currency-cash.json' + ); + return this.data; + } +} + +module.exports = FinanceApi; diff --git a/src/api/MinfinApi.js b/src/api/MinfinApi.js new file mode 100644 index 0000000..488278a --- /dev/null +++ b/src/api/MinfinApi.js @@ -0,0 +1,7 @@ +const Http = require('../utils/http'); + +class MinfinApi { + static async get() { + return Http.get('http://resources.finance.ua/ru/public/currency-cash.json'); + } +} diff --git a/src/bot.js b/src/bot.js index dfe0280..881ccc4 100644 --- a/src/bot.js +++ b/src/bot.js @@ -1,62 +1,82 @@ const Telegraf = require('telegraf'); const Telegram = require('telegraf/telegram'); +const session = require('telegraf/session'); +const Extra = require('telegraf/extra'); +const Markup = require('telegraf/markup'); const { TOKEN } = require('./config/env'); const DynamoDatabase = require('./utils/aws/dynamo'); -const FinanceApi = require('./utils/api/FinanceApi'); +const FinanceApi = require('./api/FinanceApi'); //251408559 user //978534005 bot + class TelegramExecutor extends DynamoDatabase { async execute() { const bot = new Telegraf(TOKEN); // console.log(await this.putItem()); + console.log('Started!'); + // bot.use(session()); + // bot.use(Telegraf.log()); + // bot.use((ctx, next) => { + // ctx.state.text = ctx.message.text; + // return next(); + // }); bot.start(async ctx => { - console.log(ctx); - ctx.telegram.setChatPermissions(ctx.botInfo.id, { - can_send_messages: true - }); ctx.reply('Welcome to exchange bot!'); }); - bot.help(ctx => ctx.reply('Send me a sticker')); bot.on('sticker', ctx => ctx.reply('👍')); + bot.command('showratesfinance', async ctx => { + const financeApi = new FinanceApi(); + await financeApi.getData(); + const template = financeApi.generateMessageTemplate(); + // console.log(ctx.state); + ctx.session.counter = ctx.session.counter || 0; + ctx.session.counter++; + console.log(ctx.session.counter); + // return ctx.reply( + // template, + // ); + // return ctx.reply( + // template, + // Markup.keyboard([ + // 'Every 6 hours', + // 'Every 12 hours', + // 'Every day', + // 'Every 2 days' + // ]) + // .oneTime() + // .resize() + // .extra() + // ); + return ctx.reply(template); + }); bot.command('showratesminfin', async ctx => { - const financeData = await FinanceApi.get(); - const banks = ['ПриватБанк', 'Укрсиббанк']; - const currencyIgnore = ['RUB']; + console.log(ctx.message); + const data = { + ...ctx.message.from, + chatId: ctx.message.chat.id, + date: ctx.message.date, + text: ctx.message.text + }; + await this.putItem(data); - const findedBanks = financeData.organizations.reduce((acc, val) => { - if (banks.includes(val.title)) { - return [...acc, val]; - } - return [...acc]; - }, []); + // ctx.state = { A: 2 }; + // console.log(ctx.state); + // console.log(123123); + console.log(ctx.session); - const template = findedBanks - .map(bank => { - console.log(bank); - const firstLine = `${bank.title}: \n`; - const nextLines = Object.keys(bank.currencies).reduce((acc, key) => { - const currency = bank.currencies[key]; - if (currencyIgnore.includes(key)) { - return [...acc]; - } - return [ - ...acc, - `${key}: ${parseInt(currency.ask).toFixed(2)}/${parseInt( - currency.bid - ).toFixed(2)}` - ]; - }, []); - return firstLine + nextLines.join('\n'); - }) - .join('\n\n'); - // return ctx.telegram.sendMessage(251408559, template); - return ctx.reply(template); + return ctx.reply('Rates from minfin'); }); - bot.command('showratesfinance', ctx => { - console.log(123123); - return ctx.reply('Hello'); + bot.on('text', async ctx => { + const data = { + ...ctx.message.from, + chatId: ctx.message.chat.id, + date: ctx.message.date, + text: ctx.message.text + }; + await this.putItem(data); + return ctx.reply(ctx.message.text); }); bot.launch(); } diff --git a/src/utils/api/FinanceApi.js b/src/utils/api/FinanceApi.js deleted file mode 100644 index de10795..0000000 --- a/src/utils/api/FinanceApi.js +++ /dev/null @@ -1,11 +0,0 @@ -const got = require('got'); - -class FinanceApi { - static async get() { - return got - .get('http://resources.finance.ua/ru/public/currency-cash.json') - .json(); - } -} - -module.exports = FinanceApi; diff --git a/src/utils/api/MinfinApi.js b/src/utils/api/MinfinApi.js deleted file mode 100644 index 047253b..0000000 --- a/src/utils/api/MinfinApi.js +++ /dev/null @@ -1,7 +0,0 @@ -class MinfinApi { - static async get() { - await got - .get('http://resources.finance.ua/ru/public/currency-cash.json') - .json(); - } -} diff --git a/src/utils/aws/dynamo.js b/src/utils/aws/dynamo.js index 86d5a46..0a7b6e2 100644 --- a/src/utils/aws/dynamo.js +++ b/src/utils/aws/dynamo.js @@ -1,6 +1,8 @@ const got = require('got'); const AWS = require('aws-sdk'); const tableName = 'exchane-rates-ukraine-bot'; +const uuid = require('uuid/v4'); + class DynamoDatabase { constructor() { this.dynamodb = new AWS.DynamoDB({ @@ -12,6 +14,34 @@ class DynamoDatabase { return this.dynamodb.listTables({}).promise(); } + async scan() { + var params = { + TableName: tableName + }; + this.dynamodb.scan(params, function(err, data) { + if (err) { + console.log('Error', err); + } else { + console.log('Success', data.Items); + } + }); + } + + async get() { + var params = { + TableName: tableName, + KeyConditionExpression: 'CUSTOMER_ID = :i' + // ExpressionAttributeValues: [(':i': ['2'])] + }; + this.dynamodb.query(params, function(err, data) { + if (err) { + console.log('Error', err); + } else { + console.log('Success', data.Items); + } + }); + } + async getItem() { var params = { TableName: tableName, @@ -29,22 +59,43 @@ class DynamoDatabase { }); } - async putItem() { - var params = { - TableName: tableName, - Item: { - CUSTOMER_ID: { N: '001' }, - CUSTOMER_NAME: { S: 'Richard Roe' } + async putItem(data) { + const getType = type => { + // console.log(type); + switch (type) { + case 'string': + return 'S'; + case 'number': + return 'N'; + case 'bool': + return 'B'; + default: + return 'S'; } }; - this.dynamodb.putItem(params, function(err, data) { - if (err) { - console.log('Error', err); - } else { - console.log('Success', data); - } - }); + const preparedData = Object.keys(data).reduce((acc, key) => { + return { + ...acc, + [key]: { [getType(typeof data[key])]: String(data[key]) } + }; + }, {}); + console.log(preparedData); + + const params = { + TableName: tableName, + Item: { + 'exchane-rates-ukraine-bot-primary-key': { S: uuid() }, + ...preparedData + }, + ReturnValues: 'NONE' + }; + try { + const data = await this.dynamodb.putItem(params).promise(); + console.log(data); + } catch (e) { + throw new Error(e); + } } async deleteItem() { diff --git a/src/utils/http.js b/src/utils/http.js new file mode 100644 index 0000000..8cf9bea --- /dev/null +++ b/src/utils/http.js @@ -0,0 +1,12 @@ +const got = require('got'); + +const get = async url => { + try { + const response = await got.get(url).json(); + return response; + } catch (error) { + throw error; + } +}; + +module.exports = { get };