openhive.chat bot for daily hive price

If you have logged in to openhive.chat recently ( you probably didn't ) you would have seen many now ideas for bots churning up!! And a user named 'thevil' had this awesome idea to build a bot that would put in the daily price of hive ... And that's EXACTLY what happened...


img_20210410_161154.jpg


And with this post I would like to share with you on how I made it...

Interacting with Rocket.chat's API

To send an message using nodejs there were mainly 3 ways: webhooks , js library or REST API ...


The problem with the first two was that they needed my username and private posting key ( no deal ) ... using REST API allowed me to use Auth token and User Id.

Why did I use use auth tokens?
Even if your Auth Token got compromised you could still manually login and change those in just one click ... If it was your private posting key the scammers can use that at any hive platform and do a lot more damage!!


So yeah I decided that REST API was my safest bet ... And did I mention that I had NEVER used REST API in my life! ( except maybe curl ) ... I had to deal with a LOT of "moo"s "hello world"s to finally get a hang of it ...


img_20210410_160911.jpg




But once I managed to get a hang of it ... It seemed simple enough!!

Here is the code ...

axios.post( 'https://openhive.chat/api/v1/chat.postMessage/' , { channel: 'general', text: ' Psychic Potato' }, { 
    headers: {
        'X-Auth-Token': ' ',
        'X-User-Id': ' '
    }
});


Getting the hive price

I decided to the coingecko-api library for this ( the only other option was to use coinmarketcap ,which was ... let's say "not so real values" at times ) ...

const CoinGeckoClient = new CoinGecko();
    let data = await CoinGeckoClient.exchanges.fetchTickers('bittrex', {
        coin_ids: ['hive']
    });
    var _coinList = {};
    var _datacc = data.data.tickers.filter(t => t.target == 'USD');
    [
        'HIVE'
    ].forEach((i) => {
        var _temp = _datacc.filter(t => t.base == i);
        var _res = _temp.length == 0 ? [] : _temp[0];
        _coinList[i] = _res.last;
    })
console.log(_coinList);



The problem

  1. Only bittrex price worked, trying the rest gave me an undeffined.
  2. When I tried to message the _coinList I got this ...


img_20210410_195921.jpg




Turns out ... I had to extract only the price using var x = _coinList.hive.


Where do I host this !!??






I thought about hosting this on glitch.com but the sad part was that my server died when I wasn't around ... So I decided to use parse-server's cloud code!! I had previously used back4app ( hosted version of parse-server ) so this part was easy to do.
I put the entire thing inside a Parse.Cloud.job called go and scheduled a background job to execute my code at 10am ( UTC time ). You can do this at your Dashboard!!


The code is up on GitHub => appukuttan66/Expert-Chainsaw.
So you can make your your own bot to send the daily hive price!!!


Future Plans

  • Adding HBD, Bitcoin, Ether prices too.
  • I have to work a little more on the message format.
H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Ecency