Skip to content

Commit

Permalink
mega refactor now using coingecko api to fetch and now BAT has come t…
Browse files Browse the repository at this point in the history
…o us
  • Loading branch information
andrecrjr committed May 18, 2020
1 parent 3bc5870 commit dae1139
Show file tree
Hide file tree
Showing 9 changed files with 368 additions and 623 deletions.
26 changes: 13 additions & 13 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}\\bot.js"
}
]
}
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}\\bot.js"
}
]
}
23 changes: 13 additions & 10 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
const TeleBot = require('telebot');
if(process.env.NODE_ENV !== 'prd'){
require('dotenv').config();
bot = new TeleBot(process.env.TOKEN_BTC)
}else{
bot = new TeleBot({
token:process.env.TOKEN_BTC,
usePlugins: ['commandButton'],
})
const TeleBot = require("telebot");
if (process.env.NODE_ENV !== "prd") {
require("dotenv").config();
bot = new TeleBot({
token: process.env.TOKEN_BTC,
usePlugins: ["commandButton"],
});
} else {
bot = new TeleBot({
token: process.env.TOKEN_BTC,
usePlugins: ["commandButton"],
});
}

module.exports = bot
module.exports = bot;
12 changes: 12 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Bot GeckCripto</title>
</head>
<body>
<h1>GeckoCripto Bot Updated!</h1>
<a href="https://brave.com/eel072">Use brave browser</a>
</body>
</html>
18 changes: 17 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
require('./server');
let express = require("express");
let path = require("path");
let app = express();

app.get("/", function (req, res) {
require("./src/routes/bot")();

res.sendFile(path.join(__dirname + "/index.html"));
});

let port = process.env.PORT || 3000;

let server = app.listen(port, "0.0.0.0", function () {
console.log(
`Funcionando ${server.address().address}:${server.address().port}`
);
});
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"axios": "^0.19.0",
"dotenv": "^8.0.0",
"express": "^4.17.1",
"telebot": "^1.2.6"
"telebot": "^1.3.0"
},
"scripts": {
"start": "node index.js",
Expand Down
19 changes: 0 additions & 19 deletions server.js

This file was deleted.

107 changes: 61 additions & 46 deletions src/controllers/daoRequests.js
Original file line number Diff line number Diff line change
@@ -1,57 +1,72 @@
const axios = require('axios')
const {convertDate, float} = require('../utils/timeUtils')

class mercadobitcoinApi{
constructor(coin=undefined){
this._endpoint = 'https://www.mercadobitcoin.net/api/';
this._coin = coin;
this.endpointCoin = this.endpoint+this.coin
this._realNameCoin = this.discoverName()
}
const axios = require("axios");
const { convertDate, float } = require("../utils/timeUtils");

get endpoint(){
return this._endpoint;
}
class criptoApi {
constructor(coin = undefined) {
this._endpoint =
"https://api.coingecko.com/api/v3/coins/markets?vs_currency=brl&ids=";
this._coin = coin;
this.endpointCoin = `${this.endpoint}${this.discoverName()}`;
}

get coin(){
return this._coin.toUpperCase();
}
get endpoint() {
return this._endpoint;
}

set coin(newCoin){
this._coin = newCoin
}
get coin() {
return this._coin.toUpperCase();
}

discoverName(){
switch(this.coin){
case "LTC":
return "Litecoin"
case "BTC":
return "Bitcoin"
case "XRP":
return "Ripple"
}
}
set coin(newCoin) {
this._coin = newCoin;
}

async requestTickerCoin() {
try{
const response = await axios.get(this.endpointCoin+`/ticker`)
return response.data
}catch(err){
return err
console.log('error')
}
discoverName() {
switch (this.coin) {
case "LTC":
return "litecoin";
case "BTC":
return "bitcoin";
case "XRP":
return "ripple";
case "BAT":
return "basic-attention-token";
}
}

async requestTickerCoin() {
try {
const response = await axios.get(this.endpointCoin);
return response.data;
} catch (err) {
console.log(err);

renderCoin(data){
let date = convertDate()
let ultimoValor = float(data.ticker.last).toFixed(2)
let maxValor24 = float(data.ticker.high).toFixed(2)
let highValue = `\nMaior valor nas ultimas 24h: <b>R$${maxValor24}</b>`
let atualValue = `\nValor atual: <b>R$${ultimoValor}</b>`
let textOperation = `$${this.coin} <b>${this.discoverName()} --- ${date}</b>${highValue}${atualValue}`
return textOperation
return err;
}
}

async renderCoin(bot, msg) {
const data = await this.requestTickerCoin();
const { high_24h, current_price, image, low_24h } = data[0];
let date = convertDate();
let highValue = `\nMaior valor nas ultimas 24h: <b>R$${float(
high_24h
).toFixed(2)}</b>`;
let lowValue = `\nMenor valor nas ultimas 24h: <b>R$${float(
low_24h
).toFixed(2)}</b>`;
let currentValue = `\nValor atual: <b>R$${float(current_price).toFixed(
2
)}</b>`;
let nowTime = `Hora: ${date}\n`;
let textOperation = `${nowTime}${currentValue}${highValue}${lowValue}`;

bot.sendPhoto(msg.from.id, image, {
caption: `${this.coin}${textOperation}`,
parseMode: "html",
replyToMessage: true,
});
}
}

module.exports = mercadobitcoinApi
module.exports = criptoApi;
Loading

0 comments on commit dae1139

Please sign in to comment.