From 74e63bb6977b37e567f94980e91a519d407a93d1 Mon Sep 17 00:00:00 2001 From: Anson Date: Sat, 11 May 2019 19:44:25 -0700 Subject: [PATCH 1/2] fix #16 --- bot.py | 5 ++--- functions.py | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/bot.py b/bot.py index adcb580..16af4ed 100644 --- a/bot.py +++ b/bot.py @@ -1,6 +1,5 @@ # Work with Python 3.7 import logging -import re import os import telegram @@ -40,7 +39,7 @@ def tickerDetect(bot, update): message = update.message.text chat_id = update.message.chat_id - tickers = re.findall(TICKER_REGEX, message) + tickers = getTickers(message) data = tickerData(tickers) if tickers else {} @@ -69,7 +68,7 @@ def news(bot, update): message = update.message.text chat_id = update.message.chat_id - tickers = re.findall(TICKER_REGEX, message) + tickers = getTickers(message) news = tickerNews(tickers) if tickers else {} diff --git a/functions.py b/functions.py index 01e9ba3..99fa467 100644 --- a/functions.py +++ b/functions.py @@ -2,6 +2,17 @@ import urllib.request import json from datetime import datetime 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): @@ -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) """ 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"]: reply = reply + f"\n\t[{title}]({url})" return reply + # Below Functions are incomplete + def tickerInfo(ticker): infoURL = f"https://api.iextrading.com/1.0/stock/{ticker}/stats" @@ -152,6 +166,7 @@ def tickerInfo(ticker): return info + def tickerDividend(ticker): data = tickerInfo(ticker) 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." - return dividendInfo + countdownMessage \ No newline at end of file + return dividendInfo + countdownMessage From 9f87a86bdef8508a000b4285c0bb675cb67db828 Mon Sep 17 00:00:00 2001 From: Anson Date: Sat, 11 May 2019 19:45:28 -0700 Subject: [PATCH 2/2] bot now types in chat while its working. --- bot.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bot.py b/bot.py index 16af4ed..7ab7238 100644 --- a/bot.py +++ b/bot.py @@ -39,6 +39,9 @@ def tickerDetect(bot, update): message = update.message.text chat_id = update.message.chat_id + # 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 {} @@ -68,6 +71,9 @@ def news(bot, update): message = update.message.text chat_id = update.message.chat_id + # 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 {}