include basic crypto exchange price data

This commit is contained in:
wreck 2021-01-07 17:11:09 -08:00
parent fc3c58cc88
commit 71601a2c4c
3 changed files with 42 additions and 2 deletions

View File

@ -1,6 +1,6 @@
# api key to use for https://rapidapi.com/apidojo/api/yahoo-finance1
rapidapiKey: mYsUp3rs3CretKEY12345
coinmarketkapKey: mYsUp3rs3CretKEY12345
coinapiioKey: mYsUp3rs3CretKEY12345
stocktrigger: stonks
cryptotrigger: hodl

View File

@ -1,6 +1,6 @@
maubot: 0.1.0
id: org.jobmachine.tickerbot
version: 0.0.3
version: 0.0.4
license: MIT
modules:
- tickerbot

View File

@ -12,6 +12,7 @@ import json
class Config(BaseProxyConfig):
def do_update(self, helper: ConfigUpdateHelper) -> None:
helper.copy("rapidapiKey")
helper.copy("coinapiioKey")
helper.copy("stocktrigger")
helper.copy("cryptotrigger")
@ -72,3 +73,42 @@ class TickerBot(Plugin):
)
await evt.respond(prettyMessage, allow_html=True)
@command.new(name=lambda self: self.config["cryptotrigger"],
help="Look up exchange rate of a currency pair. You must supply a pair, e.g. BTC/USD or ADA/ETH, otherwise USD will be used by default.")
@command.argument("exch_pair", pass_raw=True, required=True)
async def handler(self, evt: MessageEvent, exch_pair: str) -> None:
await evt.mark_read()
exch_pair = exch_pair.upper()
if '/' not in exch_pair:
exch_pair = exch_pair + '/USD'
url = f"https://rest.coinapi.io/v1/exchangerate/{exch_pair}"
headers = {
'X-CoinAPI-Key': self.config["coinapiioKey"],
}
try:
response = await self.http.get(url, headers=headers)
resp_json = await response.json()
except Exception as e:
await evt.respond(f"request failed: {e.message}")
return None
try:
exch_rate = resp_json["rate"]
except Exception as e:
await evt.respond("No results, double check that you've chosen a real currency pair")
self.log.exception(e)
return None
prettyMessage = "<br />".join(
[
f"<b>Current exchange rate for {exch_pair}:</b> {exch_rate}"
]
)
await evt.respond(prettyMessage, allow_html=True)