Skip to content
Permalink
Browse files

Bot v0.0.1 Setup

  • Loading branch information...
charkala committed Feb 16, 2019
0 parents commit 768a95bdbad2f279ab534ba2238b21092aa1066a
Showing with 2,317 additions and 0 deletions.
  1. +26 −0 .gitignore
  2. +1 −0 README.md
  3. +1 −0 app.js
  4. +187 −0 bot.js
  5. +13 −0 botfather_settings.md
  6. +22 −0 package.json
  7. +7 −0 pm2_start.json
  8. +2,060 −0 yarn.lock
@@ -0,0 +1,26 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*

# roject files
config.js
@@ -0,0 +1 @@
# MBC Explorer Bot for Telegram
1 app.js
@@ -0,0 +1 @@
const bot = require('./bot')
187 bot.js
@@ -0,0 +1,187 @@
const Telegraf = require('telegraf')
const rateLimit = require('telegraf-ratelimit')
const axios = require('axios')
const moment = require('moment')

const config = require('./config');

// Set limit to 1 message per 5 seconds
const limitConfig = {
window: 5000,
limit: 1,
onLimitExceeded: (ctx, next) => ctx.reply('You\'re doing that too much.\nPlease wait and try again.')
}

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*
/latest - last mined block
/supply - circulating/max supply
/fee - estimated transaction fee
/balance \`ADDRESS\` - get \`ADDRESS\` balance
/height \`HEIGHT\` - get block \`HEIGHT\` details
/tx \`TRANSACTION\` - get \`TRANSACTION\` details
/cg - current CoinGecko price
/help - list of available bot commands`
}

const bot = new Telegraf(config.BOT_TOKEN, config.BOT_OPTIONS)
bot.use(rateLimit(limitConfig))
bot.start((ctx) => ctx.replyWithMarkdown(msg.start))

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*
\`\`\`
Height: ${res.data.result.height}
Difficulty: ${res.data.result.difficulty}
\`\`\`
`
ctx.replyWithMarkdown(reply)
})
.catch(error => console.log('Error', error))
})

// supply info
bot.command('supply', (ctx) => {
axios.get(`${baseUrl}blockchain.supply`)
.then(res => {
const reply =
`
*Supply Details*
\`\`\`
Circulating Supply: ${(res.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
\`\`\`
`
ctx.replyWithMarkdown(reply)
})
.catch(error => console.log('Error', error))
} else {
const reply = 'Please enter a valid address.\nE.g. `/balance Man7setiVtcfLySRaygZVLgkDXMLKKK5Rz`'
ctx.replyWithMarkdown(reply)
}
})

// get block height info
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}
\`\`\`
`
ctx.replyWithMarkdown(reply)
})
.catch(error => console.log('Error', error))
})
} else {
const reply = 'Please enter a valid block height.\nE.g. `/height 525000`'
ctx.replyWithMarkdown(reply)
}
})

// 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}
\`\`\`
`
ctx.replyWithMarkdown(reply)
})
.catch(error => console.log('Error', error))
} else {
const reply = 'Please enter a valid transaction ID.\nE.g. `/tx 2a1c421c91e26f0b7c241f52ecc8f8b781dcc4b40fc1025e257963745b18a0fc`'
ctx.replyWithMarkdown(reply)
}
})

// get estimate fee
bot.command('fee', (ctx) => {
axios.get(`${baseUrl}blockchain.estimatesmartfee`)
.then(res => {
const reply =
`
*Transaction Fees*
\`\`\`
Estimate: ${res.data.result.feerate / 10000} MBC
Average Blocks: ${res.data.result.blocks}
\`\`\`
`
ctx.replyWithMarkdown(reply)
})
.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)}%
\`\`\`
`
ctx.replyWithMarkdown(reply)
})
.catch(error => console.log('Error', error))
})

bot.startPolling()
@@ -0,0 +1,13 @@
# BotFather Command List

```
start - start MBC Explorer Bot
latest - last mined block
supply - circulating/max supply
fee - estimated transaction fee
balance - get ADDRESS balance
height - get block HEIGHT details
tx - get TRANSACTION details
cg - current CoinGecko price
help - list of bot commands
```
@@ -0,0 +1,22 @@
{
"name": "mbc-explorer-bot",
"version": "0.0.1",
"description": "Simple bot to explore the MicroBitcoin (MBC) blockchain.",
"main": "app.js",
"scripts": {
"start": "nodemon app.js"
},
"repository": "https://github.com/charkala/mbc-explorer-bot",
"author": "charkala",
"license": "MIT",
"dependencies": {
"axios": "^0.18.0",
"moment": "^2.24.0",
"request": "^2.88.0",
"telegraf": "^3.27.1",
"telegraf-ratelimit": "^2.0.0"
},
"devDependencies": {
"nodemon": "^1.18.10"
}
}
@@ -0,0 +1,7 @@
{
"name": "counterBot",
"script": "app.js",
"autorestart": true,
"restart_delay": 10000,
"log_date_format": "YYYY-MM-DD HH:mm:ss Z"
}
Oops, something went wrong.

0 comments on commit 768a95b

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