Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add telegram bot and notification example that checks the total profit #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/telegram_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# bot sending function
def telegram_bot_sendtext(bot_message, bot_token, bot_chatID):


send_text = 'https://api.telegram.org/bot' + bot_token + '/sendMessage?chat_id=' + bot_chatID + '&parse_mode=Markdown&text=' + bot_message

response = requests.get(send_text)

return response.json()

72 changes: 72 additions & 0 deletions examples/telegram_notification.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Check the portfolio total profit and a telegram message


import sys

sys.path.append("../")


import json
import time
from datetime import datetime

from pandas import json_normalize

import requests
import time
import hashlib
from urllib.request import urlopen, Request

from trapi.api import TrBlockingApi
from environment import *

exec(open("telegram_bot.py").read())
# set telegram bot_token, bot_chatID


tr = TrBlockingApi(NUMBER, PIN, locale=LOCALE)
tr.login()

# check portfolio for a specific variable
def check_portfolio(portfolioVariable):

portfolio = tr.portfolio()
portfolioValue = portfolio[portfolioVariable]

return(portfolioValue)

# filter and calculate total for a specific variable
def check_hist(histVariable, histVariableSum):

hist = tr.hist()
hist = json_normalize(hist['data'])
hist = hist[hist['data.title'] == histVariable]

histSum = sum(hist[histVariableSum])

return(histSum)

# alert in telegram chat
def send_telegram_alert(payload, bot_token, bot_chatID):

telegram_bot_sendtext(str(alert), bot_token, bot_chatID)


portfolioVariable = 'unrealisedProfit'
histVariable = 'Cash In'
histVariableSum = 'data.cashChangeAmount'

profit = check_portfolio(portfolioVariable)

cashIn = check_hist(histVariable, histVariableSum)



alert = str(datetime.now()) + ' Profit: ' + str(round(profit - cashIn ,2)) + ' EUR'

send_telegram_alert(alert)