mirror of
https://github.com/williamkray/maubot-ticker.git
synced 2024-09-30 03:45:41 +00:00
include basic crypto exchange price data
This commit is contained in:
parent
fc3c58cc88
commit
71601a2c4c
@ -1,6 +1,6 @@
|
|||||||
# api key to use for https://rapidapi.com/apidojo/api/yahoo-finance1
|
# api key to use for https://rapidapi.com/apidojo/api/yahoo-finance1
|
||||||
rapidapiKey: mYsUp3rs3CretKEY12345
|
rapidapiKey: mYsUp3rs3CretKEY12345
|
||||||
coinmarketkapKey: mYsUp3rs3CretKEY12345
|
coinapiioKey: mYsUp3rs3CretKEY12345
|
||||||
|
|
||||||
stocktrigger: stonks
|
stocktrigger: stonks
|
||||||
cryptotrigger: hodl
|
cryptotrigger: hodl
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
maubot: 0.1.0
|
maubot: 0.1.0
|
||||||
id: org.jobmachine.tickerbot
|
id: org.jobmachine.tickerbot
|
||||||
version: 0.0.3
|
version: 0.0.4
|
||||||
license: MIT
|
license: MIT
|
||||||
modules:
|
modules:
|
||||||
- tickerbot
|
- tickerbot
|
||||||
|
40
tickerbot.py
40
tickerbot.py
@ -12,6 +12,7 @@ import json
|
|||||||
class Config(BaseProxyConfig):
|
class Config(BaseProxyConfig):
|
||||||
def do_update(self, helper: ConfigUpdateHelper) -> None:
|
def do_update(self, helper: ConfigUpdateHelper) -> None:
|
||||||
helper.copy("rapidapiKey")
|
helper.copy("rapidapiKey")
|
||||||
|
helper.copy("coinapiioKey")
|
||||||
helper.copy("stocktrigger")
|
helper.copy("stocktrigger")
|
||||||
helper.copy("cryptotrigger")
|
helper.copy("cryptotrigger")
|
||||||
|
|
||||||
@ -72,3 +73,42 @@ class TickerBot(Plugin):
|
|||||||
)
|
)
|
||||||
|
|
||||||
await evt.respond(prettyMessage, allow_html=True)
|
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)
|
||||||
|
Loading…
Reference in New Issue
Block a user