From previous post, we created utility methods to retrieve Bitcoin price from Gemini public API. Today we will implement new Telegram bot commands to use them.

Register commands

We need to register 2 more commands for our bot:

  • Check latest Bitcoin price
  • Check Bitcoin booking prices
checkbcprice - Check Bitcoin price
checkbcbooking - Check maxBid and MinAsk Bitcoin price

chat.py

Import gemini module from utils package.

from app.utils import gemini

Update __get_command_reply method to handle 2 new commands

@__encode_reply_decorator
    @__get_reply_decorator
    def __get_command_reply(self):
        commant_text = self.message['text']
        if '/checkbcprice' in commant_text:
            last_price = gemini.get_btc_price()
            if last_price is not None:
                msg = '1 btcUSD = $' + last_price
            else:
                msg = 'Failed to retrieve price data.'
            reply_dict = {'text': msg.encode('utf-8')}
        elif '/checkbcbooking' in commant_text:
            max_bid, min_ask = gemini.get_btc_booking()
            if max_bid is not None and min_ask is not None:
                msg = 'maxBid= $' + str(max_bid) + '  |  minAsk=$' + str(min_ask)
            else:
                msg = 'Failed to retrieve booking data.'
            reply_dict = {'text': msg.encode('utf-8')}
        elif '/sayhello' in commant_text:
            msg = 'Hi, nice to meet you'
            reply_dict = {
                'text': msg.encode('utf-8')
            }
        else:
            msg = 'I know this command. Let me think...'
            reply_dict = {'text': msg.encode('utf-8')}
        return 'sendMessage', reply_dict

Result

Settings Page