Skip to content
Permalink
Browse files

Changed axios to request npm package

  • Loading branch information...
charkala committed Feb 16, 2019
1 parent 5a26cdd commit 9ed61efb0457e4dae2fa4471bd5299db73802865
Showing with 111 additions and 98 deletions.
  1. +110 −96 bot.js
  2. +0 −1 package.json
  3. +1 −1 pm2_start.json
206 bot.js
@@ -1,6 +1,7 @@
const Telegraf = require('telegraf')
const rateLimit = require('telegraf-ratelimit')
const axios = require('axios')
const request = require('request')
const moment = require('moment')

const config = require('./config');
@@ -16,8 +17,7 @@ const baseUrl = 'https://api.mbc.wiki/?method='

const msg = {
start: `Hello, I'm your personal MBC explorer bot.\nStart by entering \"/\" for a list of available commands.`,
help:
`*MBC Explorer Bot*
help:`*MBC Explorer Bot*
/latest - last mined block
/supply - circulating/max supply
@@ -37,59 +37,61 @@ bot.command('help', (ctx) => ctx.replyWithMarkdown(msg.help))

// latest block info
bot.command('latest', (ctx) => {
axios.get(`${baseUrl}blockchain.info`)
.then(res => {
const reply =
`
*Latest Block*
request(`${baseUrl}blockchain.info`, (error, response, body) => {
const data = JSON.parse(body)
if (error || (response && response.statusCode !== 200)) {
ctx.reply('There was a problem accessing the server. Please try again later.')
} else {
ctx.replyWithMarkdown(
`*Latest Block*
\`\`\`
Height: ${res.data.result.height}
Difficulty: ${res.data.result.difficulty}
\`\`\`
`
ctx.replyWithMarkdown(reply)
Height: ${data.result.height}
Difficulty: ${data.result.difficulty}
\`\`\``
)
}
})
.catch(error => console.log('Error', error))
})

// supply info
bot.command('supply', (ctx) => {
axios.get(`${baseUrl}blockchain.supply`)
.then(res => {
const reply =
`
*Supply Details*
request(`${baseUrl}blockchain.supply`, (error, response, body) => {
const data = JSON.parse(body)
if (error || (response && response.statusCode !== 200)) {
ctx.reply('There was a problem accessing the server. Please try again later.')
} else {
ctx.replyWithMarkdown(
`*Supply Details*
\`\`\`
Circulating Supply: ${(res.data.result.supply / 10000).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")} MBC
Circulating Supply: ${(data.result.supply / 10000).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")} MBC
Total / Max Supply: 210,000,000,000 MBC
\`\`\`
`
ctx.replyWithMarkdown(reply)
\`\`\``
)
}
})
.catch(error => console.log('Error', error))
})

// get address balance
bot.command('balance', (ctx) => {
const address = ctx.message.text.split(' ')

if (address[1].length === 34) {
axios.get(`${baseUrl}blockchain.address.balance&params[]=${address[1]}`)
.then(res => {
const reply =
`
*Address Balance*
\`\`\`
Confirmed: ${res.data.result.confirmed / 10000} MBC
Unconfirmed: ${res.data.result.unconfirmed / 10000} MBC
if (address[1] && address[1].length === 34) {
request(`${baseUrl}blockchain.address.balance&params[]=${address[1]}`, (error, response, body) => {
const data = JSON.parse(body)
if (error || (response && response.statusCode !== 200)) {
ctx.reply('There was a problem accessing the server. Please try again later.')
} else {
ctx.replyWithMarkdown(
`*Address Balance*
\`\`\`
`
ctx.replyWithMarkdown(reply)
Confirmed: ${data.result.confirmed / 10000} MBC
Unconfirmed: ${data.result.unconfirmed / 10000} MBC
\`\`\``
)
}
})
.catch(error => console.log('Error', error))
} else {
const reply = 'Please enter a valid address.\nE.g. `/balance Man7setiVtcfLySRaygZVLgkDXMLKKK5Rz`'
ctx.replyWithMarkdown(reply)
} else {
ctx.replyWithMarkdown('Please enter a valid address.\nE.g. `/balance Man7setiVtcfLySRaygZVLgkDXMLKKK5Rz`')
}
})

@@ -98,90 +100,102 @@ bot.command('height', (ctx) => {
const height = ctx.message.text.split(' ')

if (height[1] && Number(height[1]) > 0) {
axios.get(`${baseUrl}blockchain.block.header&params[]=${height[1]}`)
.then(res => {
axios.get(`${baseUrl}blockchain.block.info&params[]=${res.data.result.block_hash}`)
.then(res =>{
const reply =
`
*Height Details*
\`\`\`
Height: ${res.data.result.height}
Time: ${moment(res.data.result.time * 1000).format('MMMM Do YYYY, h:mm:ss a')}
Transactions: ${res.data.result.tx_count}
Confirmations: ${res.data.result.confirmations}
Difficulty: ${res.data.result.difficulty}
Nonce: ${res.data.result.nonce}

request(`${baseUrl}blockchain.block.header&params[]=${height[1]}`, (error, response, body) => {
if (error || (response && response.statusCode !== 200)) {
ctx.reply('There was a problem accessing the server. Please try again later.')
} else if (body && body.result) {
request(`${baseUrl}blockchain.block.info&params[]=${JSON.parse(body).result.block_hash}`, (err, res, data) => {
if (error || (response && response.statusCode !== 200)) {
ctx.reply('There was a problem accessing the server. Please try again later.')
} else {
const reply = JSON.parse(data)
ctx.replyWithMarkdown(
`*Height Details*
\`\`\`
`
ctx.replyWithMarkdown(reply)
})
.catch(error => console.log('Error', error))
Height: ${reply.result.height}
Time: ${moment(reply.result.time * 1000).format('MMMM Do YYYY, h:mm:ss a')}
Transactions: ${reply.result.tx_count}
Confirmations: ${reply.result.confirmations}
Difficulty: ${reply.result.difficulty}
Nonce: ${reply.result.nonce}
\`\`\``
)
}
})
} else {
ctx.replyWithMarkdown('Height not found. Please check and try again.')
}
})
} else {
const reply = 'Please enter a valid block height.\nE.g. `/height 525000`'
ctx.replyWithMarkdown(reply)
ctx.replyWithMarkdown('Please enter a valid block height.\nE.g. `/height 525000`')
}
})

// get tx info
bot.command('tx', (ctx) => {
const txid = ctx.message.text.split(' ')

if (txid[1].length === 64) {
axios.get(`${baseUrl}blockchain.transaction.verbose&params[]=${txid[1]}`)
.then(res => {
const reply =
`
*Transaction Details*
\`\`\`
Height: ${res.data.result.height}
Block Time: ${moment(res.data.result.blocktime * 1000).format('MMMM Do YYYY, h:mm:ss a')}
Confirmations: ${res.data.result.confirmations}
if (txid[1] && txid[1].length === 64) {
request(`${baseUrl}blockchain.transaction.verbose&params[]=${txid[1]}`, (error, response, body) => {
const data = JSON.parse(body)
if (error || (response && response.statusCode !== 200)) {
ctx.reply('There was a problem accessing the server. Please try again later.')
} else if (data && data.result) {
ctx.replyWithMarkdown(
`*Transaction Details*
\`\`\`
`
ctx.replyWithMarkdown(reply)
Height: ${data.result.height}
Block Time: ${moment(data.result.blocktime * 1000).format('MMMM Do YYYY, h:mm:ss a')}
Confirmations: ${data.result.confirmations}
\`\`\``
)
} else {
ctx.replyWithMarkdown('Transaction not found. Please check and try again.')
}
})
.catch(error => console.log('Error', error))
} else {
const reply = 'Please enter a valid transaction ID.\nE.g. `/tx 2a1c421c91e26f0b7c241f52ecc8f8b781dcc4b40fc1025e257963745b18a0fc`'
ctx.replyWithMarkdown(reply)
ctx.replyWithMarkdown('Please enter a valid transaction ID.\nE.g. `/tx 2a1c421c91e26f0b7c241f52ecc8f8b781dcc4b40fc1025e257963745b18a0fc`')
}
})

// get estimate fee
bot.command('fee', (ctx) => {
axios.get(`${baseUrl}blockchain.estimatesmartfee`)
.then(res => {
const reply =
`
*Transaction Fees*
request(`${baseUrl}blockchain.estimatesmartfee`, (error, response, body) => {
const data = JSON.parse(body)
if (error || (response && response.statusCode !== 200)) {
ctx.reply('There was a problem accessing the server. Please try again later.')
} else {
ctx.replyWithMarkdown(
`*Transaction Fees*
\`\`\`
Estimate: ${res.data.result.feerate / 10000} MBC
Average Blocks: ${res.data.result.blocks}
\`\`\`
`
ctx.replyWithMarkdown(reply)
Estimate: ${data.result.feerate / 10000} MBC
Average Blocks: ${data.result.blocks}
\`\`\``
)
}
})
.catch(error => console.log('Error', error))
})

// get current CoinGecko price
bot.command('cg', (ctx) => {
axios.get(`https://api.coingecko.com/api/v3/coins/microbitcoin?localization=false&tickers=false&market_data=true&community_data=false&developer_data=false&sparkline=false`)
.then(res => {
const reply =
`
*CoinGecko Price*
\`\`\`
BTC: ${res.data.market_data.current_price.btc.toFixed(8)}
KRW: ${res.data.market_data.current_price.krw < 1 ? res.data.market_data.current_price.krw.toFixed(8) : res.data.market_data.current_price.krw.toFixed(2)}
24h Change: ${Number(res.data.market_data.price_change_percentage_24h).toFixed(2)}%
request(`https://api.coingecko.com/api/v3/coins/microbitcoin?localization=false&tickers=false&market_data=true&community_data=false&developer_data=false&sparkline=false`, (error, response, body) => {
const data = JSON.parse(body)
if (error || (response && response.statusCode !== 200)) {
ctx.reply('There was a problem accessing the server. Please try again later.')
} else if (data && data.id === 'microbitcoin') {
ctx.replyWithMarkdown(
`*CoinGecko Price*
\`\`\`
`
ctx.replyWithMarkdown(reply)
BTC: ${data.market_data.current_price.btc.toFixed(8)}
KRW: ${data.market_data.current_price.krw < 1 ? data.market_data.current_price.krw.toFixed(8) : data.market_data.current_price.krw.toFixed(2)}
24h Change: ${Number(data.market_data.price_change_percentage_24h).toFixed(2)}%
\`\`\``
)
} else {
ctx.reply('There was a problem connecting to CoinGecko.\nPlease try again later.')
}
})
.catch(error => console.log('Error', error))
})

bot.startPolling()
@@ -10,7 +10,6 @@
"author": "charkala",
"license": "MIT",
"dependencies": {
"axios": "^0.18.0",
"moment": "^2.24.0",
"request": "^2.88.0",
"telegraf": "^3.27.1",
@@ -1,5 +1,5 @@
{
"name": "counterBot",
"name": "mbc-explorer-bot-telegram",
"script": "app.js",
"autorestart": true,
"restart_delay": 10000,

0 comments on commit 9ed61ef

Please sign in to comment.
You can’t perform that action at this time.