mirror of
https://gitlab.com/simple-stock-bots/simple-stock-bot.git
synced 2025-06-16 15:17:28 +00:00
Merge branch 'issues' into 'master'
Issues Closes #16 See merge request simple-stock-bots/simple-telegram-bot!11
This commit is contained in:
commit
d5533909a0
11
bot.py
11
bot.py
@ -1,6 +1,5 @@
|
|||||||
# Work with Python 3.7
|
# Work with Python 3.7
|
||||||
import logging
|
import logging
|
||||||
import re
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
import telegram
|
import telegram
|
||||||
@ -40,7 +39,10 @@ def tickerDetect(bot, update):
|
|||||||
message = update.message.text
|
message = update.message.text
|
||||||
chat_id = update.message.chat_id
|
chat_id = update.message.chat_id
|
||||||
|
|
||||||
tickers = re.findall(TICKER_REGEX, message)
|
# Let user know bot is working
|
||||||
|
bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
|
||||||
|
|
||||||
|
tickers = getTickers(message)
|
||||||
|
|
||||||
data = tickerData(tickers) if tickers else {}
|
data = tickerData(tickers) if tickers else {}
|
||||||
|
|
||||||
@ -69,7 +71,10 @@ def news(bot, update):
|
|||||||
message = update.message.text
|
message = update.message.text
|
||||||
chat_id = update.message.chat_id
|
chat_id = update.message.chat_id
|
||||||
|
|
||||||
tickers = re.findall(TICKER_REGEX, message)
|
# Let user know bot is working
|
||||||
|
bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING)
|
||||||
|
|
||||||
|
tickers = getTickers(message)
|
||||||
|
|
||||||
news = tickerNews(tickers) if tickers else {}
|
news = tickerNews(tickers) if tickers else {}
|
||||||
|
|
||||||
|
19
functions.py
19
functions.py
@ -2,6 +2,17 @@ import urllib.request
|
|||||||
import json
|
import json
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import time
|
import time
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
|
def getTickers(text: str):
|
||||||
|
"""
|
||||||
|
Takes a blob of text and returns any stock tickers found.
|
||||||
|
"""
|
||||||
|
|
||||||
|
TICKER_REGEX = "[$]([a-zA-Z]{1,4})"
|
||||||
|
|
||||||
|
return list(set(re.findall(TICKER_REGEX, text)))
|
||||||
|
|
||||||
|
|
||||||
def tickerData(tickers: list):
|
def tickerData(tickers: list):
|
||||||
@ -127,13 +138,16 @@ def tickerNewsReply(ticker: dict):
|
|||||||
[Is Vanguard's VIG Better Than Its WisdomTree Counterpart?](https://api.iextrading.com/1.0/stock/aapl/article/7238581261167527)
|
[Is Vanguard's VIG Better Than Its WisdomTree Counterpart?](https://api.iextrading.com/1.0/stock/aapl/article/7238581261167527)
|
||||||
"""
|
"""
|
||||||
reply = tickerDataReply(ticker)
|
reply = tickerDataReply(ticker)
|
||||||
if len(ticker["news"]) == 0 : return reply + f"\n\tNo News was found for {ticker['name']}"
|
if len(ticker["news"]) == 0:
|
||||||
|
return reply + f"\n\tNo News was found for {ticker['name']}"
|
||||||
for title, url in ticker["news"]:
|
for title, url in ticker["news"]:
|
||||||
reply = reply + f"\n\t[{title}]({url})"
|
reply = reply + f"\n\t[{title}]({url})"
|
||||||
return reply
|
return reply
|
||||||
|
|
||||||
|
|
||||||
# Below Functions are incomplete
|
# Below Functions are incomplete
|
||||||
|
|
||||||
|
|
||||||
def tickerInfo(ticker):
|
def tickerInfo(ticker):
|
||||||
infoURL = f"https://api.iextrading.com/1.0/stock/{ticker}/stats"
|
infoURL = f"https://api.iextrading.com/1.0/stock/{ticker}/stats"
|
||||||
|
|
||||||
@ -152,6 +166,7 @@ def tickerInfo(ticker):
|
|||||||
|
|
||||||
return info
|
return info
|
||||||
|
|
||||||
|
|
||||||
def tickerDividend(ticker):
|
def tickerDividend(ticker):
|
||||||
data = tickerInfo(ticker)
|
data = tickerInfo(ticker)
|
||||||
if data["divDate"] == 0:
|
if data["divDate"] == 0:
|
||||||
@ -177,4 +192,4 @@ def tickerDividend(ticker):
|
|||||||
|
|
||||||
countdownMessage = f"\n\nThe dividend is in: {d:.0f} Days {h:.0f} Hours {m:.0f} Minutes {s:.0f} Seconds."
|
countdownMessage = f"\n\nThe dividend is in: {d:.0f} Days {h:.0f} Hours {m:.0f} Minutes {s:.0f} Seconds."
|
||||||
|
|
||||||
return dividendInfo + countdownMessage
|
return dividendInfo + countdownMessage
|
||||||
|
Loading…
x
Reference in New Issue
Block a user