Skip to content
This repository was archived by the owner on Jun 8, 2022. It is now read-only.

Commit b9548aa

Browse files
michjnichmattrasband
authored andcommitted
Allow setting of currency using c$<ticker>-(USD|GBP|EUR) when checking crypto price
1 parent 854faa3 commit b9548aa

File tree

3 files changed

+38
-9
lines changed

3 files changed

+38
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.env
22
.env/
33
.idea/
4+
.vscode/
45
__pycache__
56
docker/sirbot.env

sirbot_pyslackers/endpoints/slack/messages.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@
1414

1515
LOG = logging.getLogger(__name__)
1616
STOCK_REGEX = re.compile(r"\b(?P<asset_class>[cs])\$(?P<symbol>\^?[A-Z.]{1,5})\b")
17+
STOCK_REGEX = re.compile(
18+
r"\b(?P<asset_class>[cs])\$(?P<symbol>\^?[A-Z.]{1,5})-?(?P<currency>[A-Z]{3})?\b"
19+
)
1720
TELL_REGEX = re.compile("tell (<(#|@)(?P<to_id>[A-Z0-9]*)(|.*)?>) (?P<msg>.*)")
21+
CRYPTO_CURRENCIES = {
22+
"USD": "$",
23+
"GBP": "£",
24+
"EUR": "€",
25+
}
1826

1927

2028
def create_endpoints(plugin):
@@ -42,14 +50,21 @@ async def stock_quote(message, app):
4250
if not match:
4351
return
4452

45-
asset_class, symbol = match.group("asset_class"), match.group("symbol")
53+
asset_class, symbol, currency = (
54+
match.group("asset_class"),
55+
match.group("symbol"),
56+
match.group("currency"),
57+
)
58+
if not currency or not CRYPTO_CURRENCIES.get(currency, False):
59+
currency = "USD"
60+
currency_symbol = CRYPTO_CURRENCIES[currency]
4661
LOG.debug(
4762
"Fetching stock quotes for symbol %s in asset class %s", symbol, asset_class
4863
)
4964

5065
if asset_class == "c":
51-
LOG.debug("Fetching a crypto quote, appending USD as the pair's quote price.")
52-
symbol += "-USD"
66+
LOG.debug(f"Fetching a crypto quote, setting {currency} as the pair's quote price.")
67+
symbol = f"{symbol}-{currency}"
5368

5469
response = message.response()
5570
try:
@@ -80,7 +95,7 @@ async def stock_quote(message, app):
8095
"fields": [
8196
{
8297
"title": "Change",
83-
"value": f"${quote.change:,.4f} ({quote.change_percent:,.4f}%)",
98+
"value": f"{currency_symbol}{quote.change:,.4f} ({quote.change_percent:,.4f}%)",
8499
"short": True,
85100
},
86101
{
@@ -90,7 +105,7 @@ async def stock_quote(message, app):
90105
},
91106
{
92107
"title": "Open",
93-
"value": f"${quote.market_open:,.4f}",
108+
"value": f"{currency_symbol}{quote.market_open:,.4f}",
94109
"short": True,
95110
},
96111
{

tests/endpoints/slack/test_messages.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,23 @@
55
@pytest.mark.parametrize(
66
["text", "result"],
77
[
8-
("check out s$TSLA", {"symbol": "TSLA", "asset_class": "s"}),
9-
("what do you think about s$GOOG?", {"symbol": "GOOG", "asset_class": "s"}),
10-
("Hey, the s$^DJI is up today!", {"symbol": "^DJI", "asset_class": "s"}),
11-
("Another wild day for c$BTC, huh?", {"symbol": "BTC", "asset_class": "c"}),
8+
("check out s$TSLA", {"symbol": "TSLA", "asset_class": "s", "currency": None}),
9+
(
10+
"what do you think about s$GOOG?",
11+
{"symbol": "GOOG", "asset_class": "s", "currency": None},
12+
),
13+
(
14+
"Hey, the s$^DJI is up today!",
15+
{"symbol": "^DJI", "asset_class": "s", "currency": None},
16+
),
17+
(
18+
"Another wild day for c$BTC, huh?",
19+
{"symbol": "BTC", "asset_class": "c", "currency": None},
20+
),
21+
(
22+
"Show me c$BTC-EUR in Euros!!",
23+
{"symbol": "BTC", "asset_class": "c", "currency": "EUR"},
24+
),
1225
],
1326
)
1427
def test_stock_regex(text, result):

0 commit comments

Comments
 (0)