mirror of
https://gitlab.com/simple-stock-bots/simple-discord-stock-bot.git
synced 2025-07-28 00:51:22 +00:00
ported news and info commands from telegram bot
This commit is contained in:
18
bot.py
18
bot.py
@@ -22,10 +22,28 @@ async def on_message(message):
|
|||||||
await message.channel.send(reply)
|
await message.channel.send(reply)
|
||||||
else:
|
else:
|
||||||
await message.channel.send("No tickers found.")
|
await message.channel.send("No tickers found.")
|
||||||
|
|
||||||
|
elif message.content.startswith("/news"):
|
||||||
|
replies = tickerNews(getTickers(message.content))
|
||||||
|
if replies:
|
||||||
|
for tick, reply in replies.items():
|
||||||
|
await message.channel.send(reply)
|
||||||
|
else:
|
||||||
|
await message.channel.send("No tickers found.")
|
||||||
|
|
||||||
|
elif message.content.startswith("/info"):
|
||||||
|
replies = tickerInfo(getTickers(message.content))
|
||||||
|
if replies:
|
||||||
|
for tick, reply in replies.items():
|
||||||
|
await message.channel.send(reply)
|
||||||
|
else:
|
||||||
|
await message.channel.send("No tickers found.")
|
||||||
|
|
||||||
elif message.content.startswith('/help'):
|
elif message.content.startswith('/help'):
|
||||||
"""Send link to docs when the command /help is issued."""
|
"""Send link to docs when the command /help is issued."""
|
||||||
message = "[Please see the docs for Bot information](https://simple-stock-bots.gitlab.io/site/discord/)"
|
message = "[Please see the docs for Bot information](https://simple-stock-bots.gitlab.io/site/discord/)"
|
||||||
await message.channel.send(message)
|
await message.channel.send(message)
|
||||||
|
|
||||||
# If no commands, check for any tickers.
|
# If no commands, check for any tickers.
|
||||||
else:
|
else:
|
||||||
replies = tickerDataReply(getTickers(message.content))
|
replies = tickerDataReply(getTickers(message.content))
|
||||||
|
43
functions.py
43
functions.py
@@ -78,3 +78,46 @@ def tickerDividend(tickers: list):
|
|||||||
messages[ticker] = f"{ticker} either doesn't exist or pays no dividend."
|
messages[ticker] = f"{ticker} either doesn't exist or pays no dividend."
|
||||||
|
|
||||||
return messages
|
return messages
|
||||||
|
|
||||||
|
|
||||||
|
def tickerNews(tickers: list):
|
||||||
|
messages = {}
|
||||||
|
|
||||||
|
for ticker in tickers:
|
||||||
|
IEXurl = f"https://cloud.iexapis.com/stable/stock/{ticker}/news/last/3?token={IEX_TOKEN}"
|
||||||
|
with urllib.request.urlopen(IEXurl) as url:
|
||||||
|
data = json.loads(url.read().decode())
|
||||||
|
if data:
|
||||||
|
messages[ticker] = f"News for **{ticker.upper()}**:\n"
|
||||||
|
for news in data:
|
||||||
|
message = f"\t[{news['headline']}]({news['url']})\n\n"
|
||||||
|
messages[ticker] = messages[ticker] + message
|
||||||
|
else:
|
||||||
|
messages[
|
||||||
|
ticker
|
||||||
|
] = f"No news found for: {ticker}\nEither today is boring or the ticker does not exist."
|
||||||
|
|
||||||
|
return messages
|
||||||
|
|
||||||
|
|
||||||
|
def tickerInfo(tickers: list):
|
||||||
|
messages = {}
|
||||||
|
|
||||||
|
for ticker in tickers:
|
||||||
|
IEXurl = (
|
||||||
|
f"https://cloud.iexapis.com/stable/stock/{ticker}/company?token={IEX_TOKEN}"
|
||||||
|
)
|
||||||
|
with urllib.request.urlopen(IEXurl) as url:
|
||||||
|
data = json.loads(url.read().decode())
|
||||||
|
if data:
|
||||||
|
messages[
|
||||||
|
ticker
|
||||||
|
] = f"Company Name: [{data['companyName']}]({data['website']})\nIndustry: {data['industry']}\nSector: {data['sector']}\nCEO: {data['CEO']}\nDescription: {data['description']}\n"
|
||||||
|
|
||||||
|
else:
|
||||||
|
messages[
|
||||||
|
ticker
|
||||||
|
] = f"No information found for: {ticker}\nEither today is boring or the ticker does not exist."
|
||||||
|
|
||||||
|
return messages
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user