added transaction and portfolio commands

This commit is contained in:
Linus E 2022-04-17 11:57:28 +02:00
parent 5078660ce1
commit 543c51397e
2 changed files with 66 additions and 2 deletions

View File

@ -33,6 +33,10 @@ class API_Handler:
get_user_transactions(user_id): gets the transactions of the user get_user_transactions(user_id): gets the transactions of the user
set_transaction(user_id, transaction): sets the transaction of the user set_transaction(user_id, transaction): sets the transaction of the user
delete_transaction(user_id, transaction): deletes the transaction of the user delete_transaction(user_id, transaction): deletes the transaction of the user
get_user_portfolio(user_id): gets the portfolio of the user
set_portfolio(user_id, portfolio): sets the portfolio of the user
delete_portfolio(user_id, portfolio): deletes the portfolio of the user
set_cron_interval(user_id, interval): sets the cron interval of the user
""" """

View File

@ -14,14 +14,13 @@ __license__ = "None"
# API Documentation https://core.telegram.org/bots/api # API Documentation https://core.telegram.org/bots/api
# Code examples https://github.com/eternnoir/pyTelegramBotAPI#getting-started # Code examples https://github.com/eternnoir/pyTelegramBotAPI#getting-started
from ast import keyword
import os import os
import telebot import telebot
import time
import sys import sys
import logging import logging
import json import json
import re
import news.news_fetcher as news import news.news_fetcher as news
import shares.share_fetcher as share_fetcher import shares.share_fetcher as share_fetcher
@ -380,6 +379,67 @@ def send_keywords(message):
bot.send_message(chat_id=user_id, text=f'Your keywords are: _{keywords_str}_', parse_mode="MARKDOWN") bot.send_message(chat_id=user_id, text=f'Your keywords are: _{keywords_str}_', parse_mode="MARKDOWN")
@bot.message_handler(commands=['portfolio'])
def send_portfolio(message):
""" Send portfolio of user
:type message: message object bot
:param message: message that was reacted to, in this case always '/portfolio'
:raises: none
:rtype: none
"""
user_id = int(message.from_user.id)
portfolio = api_handler.get_user_portfolio(user_id)
if portfolio == None:
bot.send_message(chat_id=user_id, text='This didn\'t work. Make sure too connect your telegram id (/id) on https://gruppe1.testsites.info')
return
if not portfolio:
bot.send_message(chat_id=user_id, text='You do not have any stocks in your portfolio.')
return
else:
portfolio_str = ', '.join(portfolio) # tbd: format portfolio
bot.send_message(chat_id=user_id, text=f'Your portfolio is: _{portfolio_str}_', parse_mode="MARKDOWN")
@bot.message_handler(commands=['newtransaction'])
def set_new_transaction(message):
""" Set new transaction for user
:type message: message object bot
:param message: message that was reacted to, in this case always '/newtransaction'
:raises: none
:rtype: none
"""
user_id = int(message.from_user.id)
bot.send_message(chat_id=user_id, text='Type "<symbol>,<amount>,<price_per_stock>" (time of transaction will be set to now):')
bot.register_next_step_handler(message, set_new_transaction_step)
def set_new_transaction_step(message):
user_id = int(message.from_user.id)
if not re.match(r"[A-Za-z0-9]+,[0-9]+[.[0-9]+]?,[0-9]+[.[0-9]+]?", message.text):
bot.send_message(chat_id=user_id, text='Invalid format. Try again with /newtransaction.')
return
transaction_data = str(message.text).split(',')
symbol = str(transaction_data[0])
amount = float(transaction_data[1])
price = float(transaction_data[2])
time = dt.datetime.now()
status = api_handler.set_transaction(user_id, amount, price, symbol, time)
if status == 200:
bot.send_message(chat_id=user_id, text='Transaction succesfully added.')
else:
bot.send_message(chat_id=user_id, text=f'Failed adding transaction. (statuscode {status})')
@bot.message_handler(func=lambda message: True) # Returning that command is unknown for any other statement @bot.message_handler(func=lambda message: True) # Returning that command is unknown for any other statement
def echo_all(message): def echo_all(message):