update /update function
This commit is contained in:
parent
7a4a8aaedc
commit
2b18e02623
@ -190,42 +190,50 @@ def send_status(message):
|
|||||||
bot.reply_to(message, "bot is running")
|
bot.reply_to(message, "bot is running")
|
||||||
|
|
||||||
|
|
||||||
@bot.message_handler(commands=['update', 'Update']) # /update -> update shares
|
@bot.message_handler(commands=['update', 'Update']) # /update -> update shares
|
||||||
def send_update(message):
|
def update_for_user(message):
|
||||||
|
|
||||||
""" Send update on shares
|
p_user_id = int(message.from_user.id)
|
||||||
:type message: message object bot
|
p_my_handler = api_handler
|
||||||
:param message: message that was reacted to, in this case always containing '/help'
|
|
||||||
|
share_symbols = []
|
||||||
|
share_amounts = []
|
||||||
|
share_courses = []
|
||||||
|
|
||||||
|
my_portfolio = p_my_handler.get_user_portfolio(p_user_id)
|
||||||
|
|
||||||
|
for element in my_portfolio:
|
||||||
|
if element["count"] != '' and element["symbol"]!= '':
|
||||||
|
print(element["count"], element["symbol"])
|
||||||
|
share_symbols.append(element["symbol"])
|
||||||
|
share_amounts.append(element["count"])
|
||||||
|
share_courses.append(element["current_price"])
|
||||||
|
|
||||||
|
my_user = p_my_handler.get_user(p_user_id)
|
||||||
|
send_to_user("Hello %s this is your share update:"%str(my_user["username"]), pUser_id=p_user_id)
|
||||||
|
|
||||||
|
if len(share_symbols) != 0:
|
||||||
|
for i in range(len(share_symbols)):
|
||||||
|
my_update_message = f'Symbol: {share_symbols[i]}\nCurrent Price per Share: {share_courses[i]}\nAmount owned: {share_amounts[i]}\nTotal Investment: {float(share_courses[i]) * float(share_amounts[i])}'
|
||||||
|
send_to_user(my_update_message, pUser_id=p_user_id)
|
||||||
|
else:
|
||||||
|
send_to_user("No shares found for your account. Check https://gruppe1.testsites.info to change your settings and add shares.", pUser_id=p_user_id)
|
||||||
|
|
||||||
|
|
||||||
|
def send_to_user(pText, pUser_id = 1770205310):
|
||||||
|
|
||||||
|
""" Send message to user
|
||||||
|
:type pText: string
|
||||||
|
:param pText: Text to send to user
|
||||||
|
|
||||||
|
:type pUser_id: int
|
||||||
|
:param pUser_id: user to send to. per default me (Florian Kellermann)
|
||||||
|
|
||||||
:raises: none
|
:raises: none
|
||||||
|
|
||||||
:rtype: none
|
:rtype: none
|
||||||
"""
|
"""
|
||||||
user_id = int(message.from_user.id)
|
bot.send_message(chat_id=pUser_id, text=pText)
|
||||||
|
|
||||||
#Can be deleted when getting from database
|
|
||||||
dirname = os.path.dirname(__file__)
|
|
||||||
json_path = os.path.join(dirname, 'shares/shares_example.json')
|
|
||||||
|
|
||||||
with open(json_path) as json_file:
|
|
||||||
json_share_data = json.load(json_file)
|
|
||||||
int_share_count = int(json_share_data['share_count'])
|
|
||||||
str_username = str(json_share_data['user'])
|
|
||||||
bot.send_message(chat_id=user_id, text=f'Hello {str_username}. Here is the update on your currently owned shares:')
|
|
||||||
|
|
||||||
|
|
||||||
for i in range(int_share_count):
|
|
||||||
|
|
||||||
my_share = json_share_data['shares'][i]
|
|
||||||
my_share_symbol = str(my_share['symbol'])
|
|
||||||
my_share_amount = float(my_share['amount_bought'])
|
|
||||||
my_share_buy_price = float(my_share['price_bought'])
|
|
||||||
my_share_course = float(share_fetcher.get_share_price(my_share_symbol))
|
|
||||||
|
|
||||||
|
|
||||||
my_update_message = f'Symbol: {my_share_symbol}\nPrice: {my_share_course}\nBought for: {my_share_buy_price}\n\
|
|
||||||
Amount owned: {my_share_amount}\nWin/Lose: {(my_share_amount*my_share_course) - (my_share_amount*my_share_buy_price)}'
|
|
||||||
bot.send_message(chat_id=user_id, text=my_update_message)
|
|
||||||
|
|
||||||
|
|
||||||
@bot.message_handler(commands=['share', 'Share']) # /share -> get share price
|
@bot.message_handler(commands=['share', 'Share']) # /share -> get share price
|
||||||
|
Loading…
Reference in New Issue
Block a user