diff --git a/bot.py b/bot.py index e801efb..40677aa 100644 --- a/bot.py +++ b/bot.py @@ -188,6 +188,26 @@ def stat(update, context): ) +def crypto(update, context): + """ + https://iexcloud.io/docs/api/#cryptocurrency-quote + """ + context.bot.send_chat_action( + chat_id=update.message.chat_id, action=telegram.ChatAction.TYPING + ) + message = update.message.text + + reply = s.crypto(message) + + if reply: + update.message.reply_text(text=reply, parse_mode=telegram.ParseMode.MARKDOWN) + else: + update.message.reply_text( + text=f"Pair: f{message} returned an error.", + parse_mode=telegram.ParseMode.MARKDOWN, + ) + + def inline_query(update, context): """ Handles inline query. @@ -256,6 +276,7 @@ def main(): dp.add_handler(CommandHandler("search", search)) dp.add_handler(CommandHandler("intraday", intra)) dp.add_handler(CommandHandler("intra", intra)) + dp.add_handler(CommandHandler("crypto", crypto)) dp.add_handler(CommandHandler("random", rand_pick)) # on noncommand i.e message - echo the message on Telegram diff --git a/functions.py b/functions.py index 0b3aaba..e89a761 100644 --- a/functions.py +++ b/functions.py @@ -261,3 +261,33 @@ Market data is provided by [IEX Cloud](https://iexcloud.io) ] = f"No information found for: {symbol}\nEither today is boring or the symbol does not exist." return infoMessages + + def crypto(self, pair): + """Get quote for a cryptocurrency pair. + + Args: + pair (string): Cryptocurrency + """ + + pair = pair.split(" ")[-1].replace("/", "").upper() + IEXurl = f"https://cloud.iexapis.com/stable/crypto/{pair}/quote?token={self.IEX_TOKEN}" + + response = r.get(IEXurl) + + if response.status_code == 200: + data = response.json() + + quote = f"Symbol: {data['symbol']}\n" + quote += f"Price: {data['latestPrice']}\n" + quote += f"Volume: {data['latestVolume']}\n" + + new, old = data["latestPrice"], data["previousClose"] + if old is not None: + change = (float(new) - float(old)) / float(old) + quote += f"Change: {change}\n" + + return quote + + else: + return False +