From b340937a820858a8d96867b2ee2a21aa8947b9fa Mon Sep 17 00:00:00 2001 From: Anson Date: Mon, 4 Feb 2019 09:18:36 -0700 Subject: [PATCH 1/7] Change % is now only 2 decimal places --- tickerInfo.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tickerInfo.py b/tickerInfo.py index 8822152..e4d09f3 100644 --- a/tickerInfo.py +++ b/tickerInfo.py @@ -17,7 +17,9 @@ def tickerQuote(tickers): ticker = ticker.upper() stockData[ticker + "Name"] = IEXData[ticker]["quote"]["companyName"] stockData[ticker + "Price"] = IEXData[ticker]["quote"]["latestPrice"] - stockData[ticker + "Change"] = IEXData[ticker]["quote"]["changePercent"] * 100 + stockData[ticker + "Change"] = round( + (IEXData[ticker]["quote"]["changePercent"] * 100), 2 + ) stockData[ticker + "Image"] = stockLogo(ticker) print("Quote Gathered") return stockData From 6247bc2fb312dc6d49591497167bd20f3ef782ae Mon Sep 17 00:00:00 2001 From: Anson Date: Mon, 4 Feb 2019 13:16:22 -0700 Subject: [PATCH 2/7] fixed message for if stock hasnt shown movement --- stockBot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stockBot.py b/stockBot.py index 02571fe..e72c6e6 100644 --- a/stockBot.py +++ b/stockBot.py @@ -96,7 +96,7 @@ def stockInfo(bot, update): + "%**" ) else: - move = ", the stock hasn't shown any movement today." + message = message + ", the stock hasn't shown any movement today." update.message.reply_text( text=message, parse_mode=telegram.ParseMode.MARKDOWN ) From 5440f3b601806fe313697413f644b983f5b7f989 Mon Sep 17 00:00:00 2001 From: Anson Date: Mon, 4 Feb 2019 13:19:32 -0700 Subject: [PATCH 3/7] Bot now says typing in chat when its working on an answer --- stockBot.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/stockBot.py b/stockBot.py index e72c6e6..e4d76b4 100644 --- a/stockBot.py +++ b/stockBot.py @@ -34,9 +34,12 @@ def help(bot, update): def stockInfo(bot, update): message = update.message.text + chat_id = update.message.chat_id + try: # regex to find tickers in messages, looks for up to 4 word characters following a dollar sign and captures the 4 word characters tickers = re.findall("[$](\w{1,4})", message) + bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) # Checks if !news is called, and prints news embed if it is if message.startswith("!news"): From 10cd96236d5f43ab9d85cf76f5ddd16096204bcb Mon Sep 17 00:00:00 2001 From: Anson Date: Mon, 4 Feb 2019 13:19:48 -0700 Subject: [PATCH 4/7] Bot prints to console when its online --- stockBot.py | 1 + 1 file changed, 1 insertion(+) diff --git a/stockBot.py b/stockBot.py index e4d76b4..9594ca5 100644 --- a/stockBot.py +++ b/stockBot.py @@ -18,6 +18,7 @@ logging.basicConfig( ) logger = logging.getLogger(__name__) +print("Bot Online") # Define a few command handlers. These usually take the two arguments bot and From 7ad7d17e70006dd235ecf0dd5628c1bc78859b8d Mon Sep 17 00:00:00 2001 From: Anson Date: Mon, 4 Feb 2019 14:37:05 -0700 Subject: [PATCH 5/7] /news is now used and bot now says in chat if it cant find a ticker --- stockBot.py | 109 ++++++++++++++++++++++++++++++++------------------ tickerInfo.py | 19 +++++---- 2 files changed, 81 insertions(+), 47 deletions(-) diff --git a/stockBot.py b/stockBot.py index 9594ca5..f84ce4b 100644 --- a/stockBot.py +++ b/stockBot.py @@ -33,6 +33,68 @@ def help(bot, update): update.message.reply_text("I don't know how to help yet!") +def news(bot, update): + """Send a message when the /news command is issued.""" + message = update.message.text + chat_id = update.message.chat_id + + try: + # regex to find tickers in messages, looks for up to 4 word characters following a dollar sign and captures the 4 word characters + tickers = re.findall("[$](\w{1,4})", message) + bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) + + if tickers == []: + update.message.reply_text( + "Please type a ticker after your command: /news $tsla" + ) + else: + tickerData = tickerInfo.tickerQuote(tickers) + for ticker in tickers: + ticker = ticker.upper() + if tickerData[ticker] == 1: + name = tickerData[ticker + "Name"] + price = tickerData[ticker + "Price"] + change = tickerData[ticker + "Change"] + + message = ( + "The current stock price of " + + name + + " is $**" + + str(price) + + "**" + ) + if change > 0: + message = ( + message + + ", the stock is currently **up " + + str(change) + + "%**" + ) + elif change < 0: + message = ( + message + + ", the stock is currently **down" + + str(change) + + "%**" + ) + else: + message = ( + message + ", the stock hasn't shown any movement today." + ) + + news = tickerInfo.stockNewsList(ticker) + for source in news: + message = message + "\n[" + source + "](" + news[source] + ")" + + update.message.reply_text( + text=message, parse_mode=telegram.ParseMode.MARKDOWN + ) + else: + update.message.reply_text(ticker + " Does not exist.") + except: + pass + + def stockInfo(bot, update): message = update.message.text chat_id = update.message.chat_id @@ -42,46 +104,10 @@ def stockInfo(bot, update): tickers = re.findall("[$](\w{1,4})", message) bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) - # Checks if !news is called, and prints news embed if it is - if message.startswith("!news"): - tickerData = tickerInfo.tickerQuote(tickers) - for ticker in tickers: - ticker = ticker.upper() - # bot.send_photo(bot.get_updates() - # [-1].message.chat_id, tickerData[ticker + 'Image']) - name = tickerData[ticker + "Name"] - price = tickerData[ticker + "Price"] - change = tickerData[ticker + "Change"] - - message = ( - "The current stock price of " + name + " is $**" + str(price) + "**" - ) - if change > 0: - message = ( - message + ", the stock is currently **up " + str(change) + "%**" - ) - elif change < 0: - message = ( - message - + ", the stock is currently **down" - + str(change) - + "%**" - ) - else: - message = message + ", the stock hasn't shown any movement today." - - news = tickerInfo.stockNewsList(ticker) - for source in news: - message = message + "\n[" + source + "](" + news[source] + ")" - - update.message.reply_text( - text=message, parse_mode=telegram.ParseMode.MARKDOWN - ) - - else: # If news isnt called, print normal stock price - tickerData = tickerInfo.tickerQuote(tickers) - for ticker in tickers: - ticker = ticker.upper() + tickerData = tickerInfo.tickerQuote(tickers) + for ticker in tickers: + ticker = ticker.upper() + if tickerData[ticker] == 1: name = tickerData[ticker + "Name"] price = tickerData[ticker + "Price"] change = tickerData[ticker + "Change"] @@ -104,6 +130,8 @@ def stockInfo(bot, update): update.message.reply_text( text=message, parse_mode=telegram.ParseMode.MARKDOWN ) + else: + update.message.reply_text(ticker + " Does not exist.") except: pass @@ -124,6 +152,7 @@ def main(): # on different commands - answer in Telegram dp.add_handler(CommandHandler("start", start)) dp.add_handler(CommandHandler("help", help)) + dp.add_handler(CommandHandler("news", news)) # on noncommand i.e message - echo the message on Telegram dp.add_handler(MessageHandler(Filters.text, stockInfo)) diff --git a/tickerInfo.py b/tickerInfo.py index e4d09f3..c53143b 100644 --- a/tickerInfo.py +++ b/tickerInfo.py @@ -15,13 +15,18 @@ def tickerQuote(tickers): for ticker in tickers: ticker = ticker.upper() - stockData[ticker + "Name"] = IEXData[ticker]["quote"]["companyName"] - stockData[ticker + "Price"] = IEXData[ticker]["quote"]["latestPrice"] - stockData[ticker + "Change"] = round( - (IEXData[ticker]["quote"]["changePercent"] * 100), 2 - ) - stockData[ticker + "Image"] = stockLogo(ticker) - print("Quote Gathered") + if ticker in IEXData: + stockData[ticker] = 1 + stockData[ticker + "Name"] = IEXData[ticker]["quote"]["companyName"] + stockData[ticker + "Price"] = IEXData[ticker]["quote"]["latestPrice"] + stockData[ticker + "Change"] = round( + (IEXData[ticker]["quote"]["changePercent"] * 100), 2 + ) + stockData[ticker + "Image"] = stockLogo(ticker) + print(ticker + " Quote Gathered") + else: + stockData[ticker] = 0 + print(stockData) return stockData From aa31b0cfa73ba05256e21d03c28ebc7eab7ce575 Mon Sep 17 00:00:00 2001 From: Anson Date: Mon, 4 Feb 2019 14:42:06 -0700 Subject: [PATCH 6/7] added comments for new features --- stockBot.py | 5 ++++- tickerInfo.py | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/stockBot.py b/stockBot.py index f84ce4b..16363d1 100644 --- a/stockBot.py +++ b/stockBot.py @@ -43,14 +43,16 @@ def news(bot, update): tickers = re.findall("[$](\w{1,4})", message) bot.send_chat_action(chat_id=chat_id, action=telegram.ChatAction.TYPING) + ## Checks if a ticker was passed in if tickers == []: update.message.reply_text( - "Please type a ticker after your command: /news $tsla" + "Please type a ticker after your command with a dollar sign: /news $tsla" ) else: tickerData = tickerInfo.tickerQuote(tickers) for ticker in tickers: ticker = ticker.upper() + #Makes sure ticker exists if tickerData[ticker] == 1: name = tickerData[ticker + "Name"] price = tickerData[ticker + "Price"] @@ -107,6 +109,7 @@ def stockInfo(bot, update): tickerData = tickerInfo.tickerQuote(tickers) for ticker in tickers: ticker = ticker.upper() + #Makes sure ticker exists if tickerData[ticker] == 1: name = tickerData[ticker + "Name"] price = tickerData[ticker + "Price"] diff --git a/tickerInfo.py b/tickerInfo.py index c53143b..5eb9659 100644 --- a/tickerInfo.py +++ b/tickerInfo.py @@ -3,6 +3,7 @@ import json def tickerQuote(tickers): + """Gathers information from IEX api on stock""" stockData = {} IEXURL = ( "https://api.iextrading.com/1.0/stock/market/batch?symbols=" @@ -15,6 +16,8 @@ def tickerQuote(tickers): for ticker in tickers: ticker = ticker.upper() + + # Makes sure ticker exists before populating a dictionary if ticker in IEXData: stockData[ticker] = 1 stockData[ticker + "Name"] = IEXData[ticker]["quote"]["companyName"] @@ -31,6 +34,7 @@ def tickerQuote(tickers): def stockNewsList(ticker): + """Makes a bunch of strings that are links to news websites for an input ticker""" print("Gather News on " + ticker) news = { "Bravos": "https://bravos.co/" + ticker, @@ -46,5 +50,6 @@ def stockNewsList(ticker): def stockLogo(ticker): + """returns a png of an input ticker""" logoURL = "https://g.foolcdn.com/art/companylogos/mark/" + ticker + ".png" return logoURL From a48c21ad3e889c3a0d83d4d02daa1e807e5728cd Mon Sep 17 00:00:00 2001 From: Anson Biggs Date: Mon, 4 Feb 2019 21:54:50 +0000 Subject: [PATCH 7/7] removed debugging print --- tickerInfo.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tickerInfo.py b/tickerInfo.py index 5eb9659..be36a50 100644 --- a/tickerInfo.py +++ b/tickerInfo.py @@ -29,7 +29,6 @@ def tickerQuote(tickers): print(ticker + " Quote Gathered") else: stockData[ticker] = 0 - print(stockData) return stockData