TelegramAktienBot/documentation/telegram_bot/bot.html

1810 lines
83 KiB
HTML
Raw Normal View History

2022-05-12 11:30:56 +00:00
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1" />
<meta name="generator" content="pdoc 0.10.0" />
<title>telegram_bot.bot API documentation</title>
<meta name="description" content="script for telegram bot and its functions" />
<link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/13.0.0/sanitize.min.css" integrity="sha512-y1dtMcuvtTMJc1yPgEqF0ZjQbhnc/bFhyvIyVNb9Zk5mIGtqVaAB1Ttl28su8AvFMOY0EwRbAe+HCLqj6W7/KA==" crossorigin>
<link rel="preload stylesheet" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/13.0.0/typography.min.css" integrity="sha512-Y1DYSb995BAfxobCkKepB1BqJJTPrOp3zPL74AWFugHHmmdcvO+C48WLrUOlhGMc0QG7AE3f7gmvvcrmX2fDoA==" crossorigin>
<link rel="stylesheet preload" as="style" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/github.min.css" crossorigin>
2022-05-12 11:30:56 +00:00
<style>:root{--highlight-color:#fe9}.flex{display:flex !important}body{line-height:1.5em}#content{padding:20px}#sidebar{padding:30px;overflow:hidden}#sidebar > *:last-child{margin-bottom:2cm}.http-server-breadcrumbs{font-size:130%;margin:0 0 15px 0}#footer{font-size:.75em;padding:5px 30px;border-top:1px solid #ddd;text-align:right}#footer p{margin:0 0 0 1em;display:inline-block}#footer p:last-child{margin-right:30px}h1,h2,h3,h4,h5{font-weight:300}h1{font-size:2.5em;line-height:1.1em}h2{font-size:1.75em;margin:1em 0 .50em 0}h3{font-size:1.4em;margin:25px 0 10px 0}h4{margin:0;font-size:105%}h1:target,h2:target,h3:target,h4:target,h5:target,h6:target{background:var(--highlight-color);padding:.2em 0}a{color:#058;text-decoration:none;transition:color .3s ease-in-out}a:hover{color:#e82}.title code{font-weight:bold}h2[id^="header-"]{margin-top:2em}.ident{color:#900}pre code{background:#f8f8f8;font-size:.8em;line-height:1.4em}code{background:#f2f2f1;padding:1px 4px;overflow-wrap:break-word}h1 code{background:transparent}pre{background:#f8f8f8;border:0;border-top:1px solid #ccc;border-bottom:1px solid #ccc;margin:1em 0;padding:1ex}#http-server-module-list{display:flex;flex-flow:column}#http-server-module-list div{display:flex}#http-server-module-list dt{min-width:10%}#http-server-module-list p{margin-top:0}.toc ul,#index{list-style-type:none;margin:0;padding:0}#index code{background:transparent}#index h3{border-bottom:1px solid #ddd}#index ul{padding:0}#index h4{margin-top:.6em;font-weight:bold}@media (min-width:200ex){#index .two-column{column-count:2}}@media (min-width:300ex){#index .two-column{column-count:3}}dl{margin-bottom:2em}dl dl:last-child{margin-bottom:4em}dd{margin:0 0 1em 3em}#header-classes + dl > dd{margin-bottom:3em}dd dd{margin-left:2em}dd p{margin:10px 0}.name{background:#eee;font-weight:bold;font-size:.85em;padding:5px 10px;display:inline-block;min-width:40%}.name:hover{background:#e0e0e0}dt:target .name{background:var(--highlight-color)}.name > span:first-child{white-space:nowrap}.name.class > span:nth-child(2){margin-left:.4em}.inherited{color:#999;border-left:5px solid #eee;padding-left:1em}.inheritance em{font-style:normal;font-weight:bold}.desc h2{font-weight:400;font-size:1.25em}.desc h3{font-size:1em}.desc dt code{background:inherit}.source summary,.git-link-div{color:#666;text-align:right;font-weight:400;font-size:.8em;text-transform:uppercase}.source summary > *{white-space:nowrap;cursor:pointer}.git-link{color:inherit;margin-left:1em}.source pre{max-height:500px;overflow:auto;margin:0}.source pre code{font-size:12px;overflow:visible}.hlist{list-style:none}.hlist li{display:inline}.hlist li:after{content:',\2002'}.hlist li:last-child:after{content:none}.hlist .hlist{display:inline;padding-left:1em}img{max-width:100%}td{padding:0 .5em}.admonition{padding:.1em .5em;margin-bottom:1em}.admonition-title{font-weight:bold}.admonition.note,.admonition.info,.admonition.important{background:#aef}.admonition.todo,.admonition.versionadded,.admonition.tip,.admonition.hint{background:#dfd}.admonition.warning,.admonition.versionchanged,.admonition.deprecated{background:#fd4}.admonition.error,.admonition.danger,.admonition.caution{background:lightpink}</style>
<style media="screen and (min-width: 700px)">@media screen and (min-width:700px){#sidebar{width:30%;height:100vh;overflow:auto;position:sticky;top:0}#content{width:70%;max-width:100ch;padding:3em 4em;border-left:1px solid #ddd}pre code{font-size:1em}.item .name{font-size:1em}main{display:flex;flex-direction:row-reverse;justify-content:flex-end}.toc ul ul,#index ul{padding-left:1.5em}.toc > ul > li{margin-top:.5em}}</style>
<style media="print">@media print{#sidebar h1{page-break-before:always}.source{display:none}}@media print{*{background:transparent !important;color:#000 !important;box-shadow:none !important;text-shadow:none !important}a[href]:after{content:" (" attr(href) ")";font-size:90%}a[href][title]:after{content:none}abbr[title]:after{content:" (" attr(title) ")"}.ir a:after,a[href^="javascript:"]:after,a[href^="#"]:after{content:""}pre,blockquote{border:1px solid #999;page-break-inside:avoid}thead{display:table-header-group}tr,img{page-break-inside:avoid}img{max-width:100% !important}@page{margin:0.5cm}p,h2,h3{orphans:3;widows:3}h1,h2,h3,h4,h5,h6{page-break-after:avoid}}</style>
<script defer src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js" integrity="sha512-bgHRAiTjGrzHzLyKOnpFvaEpGzJet3z4tZnXGjpsCcqOnAH6VGUx9frc5bcIhKTVLEiCO6vEhNAgx5jtLUYrfA==" crossorigin></script>
2022-05-12 11:30:56 +00:00
<script>window.addEventListener('DOMContentLoaded', () => hljs.initHighlighting())</script>
</head>
<body>
<main>
<article id="content">
<header>
<h1 class="title">Module <code>telegram_bot.bot</code></h1>
</header>
<section id="section-intro">
<p>script for telegram bot and its functions</p>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">&#34;&#34;&#34;
script for telegram bot and its functions
&#34;&#34;&#34;
__author__ = &#34;Florian Kellermann, Linus Eickhoff&#34;
__date__ = &#34;10.05.2022&#34;
__version__ = &#34;1.2.3&#34;
__license__ = &#34;None&#34;
# side-dependencies: none
# Work in Progress
# text bot at t.me/projektaktienbot
# API Documentation https://core.telegram.org/bots/api
# Code examples https://github.com/eternnoir/pyTelegramBotAPI#getting-started
import datetime as dt
import logging
import os
import re
import sys
import telebot
from dotenv import load_dotenv
from telebot import types
import telegram_bot.helper_functions as hf
import telegram_bot.news.news_fetcher as news
import telegram_bot.shares.share_fetcher as share_fetcher
from telegram_bot.api_handling.api_handler import API_Handler
load_dotenv(dotenv_path=&#39;.env&#39;) # load environment variables
bot_version = &#34;2.0.1&#34; # version of bot
# create api handler
api_handler = API_Handler(&#34;https://gruppe1.testsites.info/api&#34;, str(os.getenv(&#34;BOT_EMAIL&#34;)), str(os.getenv(&#34;BOT_PASSWORD&#34;))) # get creds from env vars.
print(&#34;Webserver Token: &#34; + str(api_handler.token))
bot = telebot.TeleBot(os.getenv(&#39;BOT_API_KEY&#39;))
@bot.message_handler(commands=[&#39;start&#39;, &#39;Start&#39;])
def send_start(message):
&#34;&#34;&#34; Sending welcome message to new user
:type message: message object bot
:param message: message that was reacted to, in this case always containing &#39;/start&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
bot.reply_to(message, &#34;Welcome to this share bot project. \
\nType /help to get information on what this bot can do. \
\nAlso see https://gruppe1.testsites.info \
to start configuring your bot&#34;)
@bot.message_handler(commands=[&#39;version&#39;, &#39;Version&#39;])
def send_version(message):
&#34;&#34;&#34; Sending programm version
:type message: message object bot
:param message: message that was reacted to, in this case always containing &#39;/version&#39;
:raises: none
:rtype:none
&#34;&#34;&#34;
bot.reply_to(message, &#34;the current bot version is &#34; + bot_version)
@bot.message_handler(commands=[&#39;help&#39;, &#39;Help&#39;]) # /help -&gt; sending all functions
def send_help(message):
&#34;&#34;&#34; Send all functions
:type message: message object bot
:param message: message that was reacted to, in this case always containing &#39;/help&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
bot.reply_to(message,
&#34;/id or /auth get your user id\n/update get updates on your shares.\n/shares get update on interesting shares\n/setAdmin set admin rights of user (ADMIN)\n/users see all users. (ADMIN)\n/me get my user info\n/news get top article for each keyword.\n/allnews get all news (last 7 days)\n/keywords get all your keywords\n/addkeyword add a keyword\n/removekeyword remove a keyword\n/transactions get all transactions\n/newtransaction create new transaction\n/share get price of specific share\n/portfolio see own portfolio\n/removeshare removes share from portfolio\n/interval get update interval\n/setinterval set update interval\n For further details see https://gruppe1.testsites.info&#34;)
@bot.message_handler(commands=[&#39;users&#39;, &#39;Users&#39;]) # /users -&gt; sending all users
def send_all_users(message):
&#34;&#34;&#34; Send all users, only possible for admins
:type message: message object bot
:param message: message that was reacted to, in this case always containing &#39;/users&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
user_data = api_handler.get_user(user_id)
if (user_data[&#34;admin&#34;] == False): # check if user has admin rights
bot.reply_to(message, &#34;You have to be an admin to use this command&#34;)
return
user_list = api_handler.get_all_users()
user_count = len(user_list)
bot.send_message(chat_id=user_id, text=&#34;There are &#34; + str(user_count) + &#34; users in the database:&#34;)
for user in user_list:
username = user[&#39;username&#39;]
email = user[&#39;email&#39;]
id = user[&#39;telegram_user_id&#39;]
cron = user[&#39;cron&#39;]
admin = user[&#39;admin&#39;]
bot.send_message(chat_id=user_id, text=f&#39;Username: {username}\nEmail: {email}\nID: {id}\nCron: {cron}\nAdmin: {admin}&#39;) # format user data into readable message text
@bot.message_handler(commands=[&#39;setAdmin&#39;, &#39;SetAdmin&#39;, &#39;setadmin&#39;, &#39;Setadmin&#39;]) # set admin rights to user TBD: not working!!
def set_admin(message):
&#34;&#34;&#34; Set admin rights to user
:type message: message object bot
:param message: message that was reacted to, in this case always containing &#39;/setAdmin&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
user_data = api_handler.get_user(user_id)
if (user_data[&#34;admin&#34;] == False): # check if user has admin rights
bot.reply_to(message, &#34;You have to be an admin to use this command&#34;)
return
bot.send_message(chat_id=user_id, text=&#39;send email and true if this account should have admin rights, else false\n in format: &lt;email&gt;,&lt;is_admin&gt;&#39;) # request email and admin rights to change to
bot.register_next_step_handler(message, set_admin_step)
def set_admin_step(message):
str_message = str(message.text)
args_message = str_message.split(&#39;,&#39;) # split message into email and admin rights
if len(args_message) != 2: # make sure 2 args (email,is_admin) are given
bot.reply_to(message, &#34;exactly 2 arguments (&lt;email&gt;,&lt;is_admin&gt;) required, try again&#34;)
return
email = args_message[0]
is_admin = False # default: False
if args_message[1].lower() == &#34;true&#34;: # if user types true, set is_admin to true
is_admin = True
status = api_handler.set_admin(email, is_admin) # set admin in db
if (status == 200):
bot.reply_to(message, &#34;Admin rights set&#34;)
else:
bot.reply_to(message, f&#34;Admin rights could not be set ({status})&#34;)
@bot.message_handler(commands=[&#39;me&#39;, &#39;Me&#39;]) # /me -&gt; sending user info
def send_user(message):
&#34;&#34;&#34; Send user data
:type message: message object bot
:param message: message that was reacted to, in this case always containing &#39;/me&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
user_data = api_handler.get_user(user_id)
if not user_data or user_data == None: # true if user is not registered
bot.reply_to(message, &#34;This didn\&#39;t work. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info&#34;)
return
username = user_data[&#39;username&#39;]
email = user_data[&#39;email&#39;]
user_id = user_data[&#39;telegram_user_id&#39;]
cron = user_data[&#39;cron&#39;]
admin = user_data[&#39;admin&#39;]
bot.reply_to(message, f&#39;Username: {username}\nEmail: {email}\nID: {user_id}\nCron: {cron}\nAdmin: {admin}&#39;) # format user data into readable message text
@bot.message_handler(commands=[&#39;id&#39;, &#39;auth&#39;, &#39;Id&#39;, &#39;Auth&#39;]) # /auth or /id -&gt; Authentication with user_id over web tool
def send_id(message):
&#34;&#34;&#34; Send user id for authentication with browser
:type message: message object bot
:param message: message that was reacted to, in this case always containing &#39;/id&#39; or &#39;/auth&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
answer = &#39;Your ID/Authentication Code is: [&#39; + str(message.from_user.id) + &#39;]. Enter this code in the settings on https://gruppe1.testsites.info to get updates on your shares.&#39;
bot.reply_to(message, answer)
# function that can be used to ensure that the bot is online and running
@bot.message_handler(commands=[&#39;status&#39;, &#39;Status&#39;])
def send_status(message):
&#34;&#34;&#34; Sends status to user
:type message: message object bot
:param message: message that was reacted to, if no other command handler gets called
:raises: none
:rtype: none
&#34;&#34;&#34;
bot.reply_to(message, &#34;bot is running&#34;)
@bot.message_handler(commands=[&#39;update&#39;, &#39;Update&#39;]) # /update -&gt; update shares
def update_for_user(message):
p_user_id = int(message.from_user.id)
p_my_handler = api_handler
share_symbols = []
share_amounts = []
my_portfolio = p_my_handler.get_user_portfolio(p_user_id)
for element in my_portfolio:
if element[&#34;count&#34;] != &#39;&#39; and element[&#34;isin&#34;] != &#39;&#39;:
print(element[&#34;count&#34;], element[&#34;isin&#34;])
share_symbols.append(element[&#34;isin&#34;])
share_amounts.append(element[&#34;count&#34;])
my_user = p_my_handler.get_user(p_user_id)
send_to_user(&#34;Hello %s this is your share update:&#34; % str(my_user[&#34;username&#34;]), pUser_id=p_user_id)
if len(share_symbols) != 0:
for i in range(len(share_symbols)):
my_price = share_fetcher.get_share_price_no_currency(share_symbols[i])
my_update_message = f&#39;{share_fetcher.get_share_information_markdown(share_symbols[i])}\ncount: {share_amounts[i]}\nTotal: {hf.make_markdown_proof(round(float(my_price) * float(share_amounts[i]), 2))} EUR&#39;
bot.send_message(chat_id=p_user_id, text=my_update_message, parse_mode=&#34;MARKDOWNV2&#34;)
else:
send_to_user(&#34;No shares found for your account. Check https://gruppe1.testsites.info to change your settings and add shares.&#34;, pUser_id=p_user_id)
def send_to_user(pText, pUser_id):
&#34;&#34;&#34; 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
:rtype: none
&#34;&#34;&#34;
bot.send_message(chat_id=pUser_id, text=pText)
@bot.message_handler(commands=[&#39;share&#39;, &#39;Share&#39;]) # /share -&gt; get share price
def send_share_update(message):
&#34;&#34;&#34; Send price of a specific share
:type message: message object bot
:param message: message that was reacted to, in this case always containing &#39;/share&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
bot.send_message(chat_id=user_id, text=&#39;Send Symbol/ISIN of share or name of company:&#39;)
bot.register_next_step_handler(message, send_share_price)
def send_share_price(message):
str_share_price = share_fetcher.get_share_information_markdown(str(message.text))
bot.reply_to(message, str_share_price, parse_mode=&#34;MARKDOWNV2&#34;)
@bot.message_handler(commands=[&#39;allnews&#39;, &#39;Allnews&#39;]) # /allnews -&gt; get all news
def send_all_news(message):
&#34;&#34;&#34; Get news for keywords of user
:type message: message object bot
:param message: message that was reacted to, in this case always containing &#39;/allnews&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
keywords = api_handler.get_user_keywords(user_id) # get keywords of user
if keywords == None: # true if user is not registered
bot.send_message(chat_id=user_id, text=&#39;This didn\&#39;t work. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info&#39;)
return
if not keywords: # true if user is registered but does not have any keywords
bot.send_message(chat_id=user_id, text=&#39;You have no keywords. Please add some keywords with /addkeyword&#39;)
return
keywords_search = &#39; OR &#39;.join(keywords) # concat all keywords with OR -&gt; NewsAPI can understand OR, AND, NOT etc.
now = dt.datetime.now().date() # get current date
from_date = now - dt.timedelta(days=7) # get date 7 days ago -&gt; limit age of news to 7 days old max
from_date_formatted = dt.datetime.strftime(from_date, &#39;%Y-%m-%d&#39;)
news_list = news.get_all_news_by_keyword(keywords_search, from_date_formatted)[&#34;articles&#34;] # array of JSON article objects
if news_list: # true if news_list is not empty
for article in news_list:
formatted_article = news.format_article(article)
bot.send_message(chat_id=user_id, text=formatted_article, parse_mode=&#34;MARKDOWNV2&#34;) # Markdown allows to write bold text with * etc.
else:
bot.send_message(chat_id=user_id, text=&#39;No news found for your keywords.&#39;)
@bot.message_handler(commands=[&#39;news&#39;, &#39;News&#39;]) # /news -&gt; get news for specific keyword
def send_news(message):
&#34;&#34;&#34; Get news for keywords of user
:type message: message object bot
:param message: message that was reacted to, in this case always containing &#39;/news&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
keywords = api_handler.get_user_keywords(user_id) # get keywords of user
if keywords == None: # true if user is not registered
bot.send_message(chat_id=user_id, text=&#39;This didn\&#39;t work. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info&#39;)
return
if not keywords: # true if user is registered but does not have any keywords
bot.send_message(chat_id=user_id, text=&#39;You have no keywords. Please add some keywords with /addkeyword&#39;)
return
if keywords:
for keyword in keywords:
top_news = news.get_top_news_by_keyword(keyword)[&#34;articles&#34;]
if top_news == None: # true if request to NewsAPI failed
bot.send_message(chat_id=user_id, text=&#39;News Server did not respond correctly. Try again later.&#39;)
if not top_news: # true if no news found for keyword (empty list)
keyword = hf.make_markdown_proof(keyword)
bot.send_message(chat_id=user_id, text=f&#39;No news found for keyword: *{keyword}*&#39;, parse_mode=&#34;MARKDOWNV2&#34;)
else:
keyword = hf.make_markdown_proof(keyword)
formatted_article = news.format_article(top_news[0]) # only format and send most popular news
bot.send_message(chat_id=user_id, text=f&#34;_keyword: {keyword}_\n\n&#34; + formatted_article, parse_mode=&#34;MARKDOWNV2&#34;) # do not use v2 because of bugs related t &#34;.&#34; in links
@bot.message_handler(commands=[&#39;addkeyword&#39;, &#39;Addkeyword&#39;]) # /addkeyword -&gt; add keyword to user
def add_keyword(message):
&#34;&#34;&#34; Add keyword to user
:type message: message object bot
:param message: message that was reacted to, in this case always &#39;/addkeyword&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
bot.send_message(chat_id=user_id, text=&#39;Type keyword to add:&#39;)
bot.register_next_step_handler(message, store_keyword) # wait for user to send keyword, then call store_keyword function
def store_keyword(message):
user_id = int(message.from_user.id)
keyword = str(message.text).lower() # lower to ensure Bitcoin and bitcoin is not stored as individual keywords
status = api_handler.set_keyword(user_id, keyword) # set keyword in database
if status == 200: # statuscode 200 means keyword was added successfully without errors
bot.send_message(chat_id=user_id, text=f&#39;Keyword &#34;{keyword}&#34; added.&#39;) # duplicate keywords are denied by Database, so no need to check for that here
else:
bot.send_message(chat_id=user_id, text=f&#39;Keyword &#34;{keyword}&#34; could not be stored. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info (statuscode {status})&#39;)
@bot.message_handler(commands=[&#39;removekeyword&#39;, &#39;Removekeyword&#39;]) # /removekeyword -&gt; remove keyword from user
def remove_keyword(message):
&#34;&#34;&#34; Remove keyword from user
:type message: message object bot
:param message: message that was reacted to, in this case always &#39;/removekeyword&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
bot.send_message(chat_id=user_id, text=&#39;Type keyword to remove:&#39;)
bot.register_next_step_handler(message, remove_keyword_step) # wait for user to send keyword to remove, then call remove_keyword_step function
def remove_keyword_step(message):
user_id = int(message.from_user.id)
keyword = str(message.text).lower()
status = api_handler.delete_keyword(user_id, keyword)
if status == 200: # statuscode 200 means keyword was removed successfully without errors
bot.send_message(chat_id=user_id, text=f&#39;Keyword &#34;{keyword}&#34; removed.&#39;) # checking if keyword to remove is in database are handled in database, not here
else:
bot.send_message(chat_id=user_id, text=f&#39;Failed deleting keyword &#34;{keyword}&#34;. (statuscode {status})&#39;)
@bot.message_handler(commands=[&#39;keywords&#39;, &#39;Keywords&#39;]) # /keywords -&gt; get keywords of user
def send_keywords(message):
&#34;&#34;&#34; Send keywords of user
:type message: message object bot
:param message: message that was reacted to, in this case always &#39;/keywords&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
keywords = api_handler.get_user_keywords(user_id) # get keywords of user
if keywords == None: # true if user is not registered
bot.send_message(chat_id=user_id, text=&#39;This didn\&#39;t work. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info&#39;)
return
if not keywords: # true if user is registered but does not have any keywords
bot.send_message(chat_id=user_id, text=&#39;No keywords set for this account. Add keywords by using /addkeyword&#39;)
return
else: # send keyword list
keywords_str = &#39;, &#39;.join(keywords)
keywords_str = hf.make_markdown_proof(keywords_str)
text = f&#39;Your keywords are: _{keywords_str}_&#39;
bot.send_message(chat_id=user_id, text=text, parse_mode=&#34;MARKDOWNV2&#34;)
@bot.message_handler(commands=[&#39;portfolio&#39;, &#39;Portfolio&#39;])
def send_portfolio(message):
&#34;&#34;&#34; Send portfolio of user
:type message: message object bot
:param message: message that was reacted to, in this case always &#39;/portfolio&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
portfolio = api_handler.get_user_portfolio(user_id) # get portfolio of user as json
if portfolio == None: # true if user is not registered
bot.send_message(chat_id=user_id, text=&#39;This didn\&#39;t work. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info&#39;)
return
if not portfolio: # true if user is registered but does not have any stocks in portfolio
bot.send_message(chat_id=user_id, text=&#39;You do not have any stocks in your portfolio.&#39;)
return
else: # send portfolio
for stock in portfolio:
comment = hf.make_markdown_proof(str(stock[&#34;comment&#34;])) # comment may be written name of stock, comment is made by user when adding an stock to portfolio
count = hf.make_markdown_proof(&#34;{:.2f}&#34;.format(float(stock[&#34;count&#34;]))) # round count to 2 decimal places
isin = hf.make_markdown_proof(str(stock[&#34;isin&#34;]))
worth = hf.make_markdown_proof(&#34;{:.2f}&#34;.format(float(stock[&#34;current_price&#34;]) * float(stock[&#34;count&#34;]))) # round current_price to 2 decimal places
bot.send_message(chat_id=user_id, text=f&#39;*{comment}*\n_{isin}_\namount: {count}\nworth: ${worth}&#39;, parse_mode=&#34;MARKDOWNV2&#34;) # formatted message in markdown
@bot.message_handler(commands=[&#39;removeshare&#39;, &#39;Removeshare&#39;])
def remove_share(message):
&#34;&#34;&#34; Remove share from portfolio
:type message: message object bot
:param message: message that was reacted to, in this case always &#39;/removeshare&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
bot.send_message(chat_id=user_id, text=&#39;Type ISIN/Symbol/CompanyName of share to remove (if you are unsure do /shares, find your share and insert the value above amount):&#39;)
bot.register_next_step_handler(message, remove_share_step) # wait for user to send ISIN, then call remove_share_step function
def remove_share_step(message):
user_id = int(message.from_user.id)
isin = str(message.text)
status = api_handler.delete_share(int(user_id), str(isin)) # remove share from portfolio
if status == 200: # statuscode 200 means share was removed successfully without errors
bot.send_message(chat_id=user_id, text=f&#39;Share &#34;{isin}&#34; removed.&#39;) # checking if share to remove is in database are handled in database, not here
else:
bot.send_message(chat_id=user_id, text=f&#39;Failed deleting share &#34;{isin}&#34;. (statuscode {status})\nMake sure that the share is in your portfolio and written exactly like there.&#39;)
@bot.message_handler(commands=[&#39;newtransaction&#39;, &#39;Newtransaction&#39;]) # tbd not working rn may be deleted in future
def set_new_transaction(message):
&#34;&#34;&#34; Set new transaction for user
:type message: message object bot
:param message: message that was reacted to, in this case always &#39;/newtransaction&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
bot.send_message(chat_id=user_id,
text=&#39;Type &#34;&lt;name of stock&gt;,&lt;isin/name/symbol&gt;,&lt;amount&gt;,&lt;price_per_stock_usd&gt;&#34; (time of transaction will be set to now, negative amount is selling, positive is buying):&#39;)
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&#34;[A-Za-z0-9 ]+,[A-Za-z0-9]+,(-)?[0-9]+(.[0-9]+)?,[0-9]+(.[0-9]+)?&#34;, message.text):
bot.send_message(chat_id=user_id, text=&#39;Invalid format \n(e.g. Apple,US0378331005,53.2,120.4).\n Try again with /newtransaction.&#39;)
return
transaction_data = str(message.text).split(&#39;,&#39;)
desc = str(transaction_data[0])
isin = str(transaction_data[1])
amount = float(transaction_data[2])
price = float(transaction_data[3])
time = dt.datetime.now().isoformat()
print(&#34;\n\n\n\n\n&#34;)
print(f&#34;{isin},{amount},{price},{time}&#34;)
status = api_handler.set_transaction(user_id, desc, isin, amount, price, time)
if status == 200:
bot.send_message(chat_id=user_id, text=&#39;Transaction succesfully added.&#39;)
else:
bot.send_message(chat_id=user_id, text=f&#39;Failed adding transaction. (statuscode {status})&#39;)
@bot.message_handler(commands=[&#39;interval&#39;, &#39;Interval&#39;])
def send_interval(message):
&#34;&#34;&#34; send interval for user
:type message: message object bot
:param message: message that was reacted to, in this case always &#39;/interval&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
user_data = api_handler.get_user(user_id) # get cron interval of user (stored in user data)
if user_data == None: # true if user is not registered in DB
bot.send_message(chat_id=user_id, text=&#39;This didn\&#39;t work. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info and set an interval with /setinterval&#39;)
return
else: # send interval
interval = str(user_data[&#39;cron&#39;]) # get cron from user data
if interval == &#39;None&#39;: # true if user has no cron set
bot.send_message(chat_id=user_id, text=&#39;You do not have an interval set. Set one with /setinterval&#39;)
return
formatted_interval = str(interval).replace(&#39; &#39;, &#39;_&#39;) # replace spaces with underscores to add to url of crontab.guru
bot.send_message(chat_id=user_id, text=f&#39;Your update interval: {interval} (https://crontab.guru/#{formatted_interval})&#39;)
@bot.message_handler(commands=[&#39;transactions&#39;, &#39;Transactions&#39;])
def send_transactions(message):
&#34;&#34;&#34; send transactions for user
:type message: message object bot
:param message: message that was reacted to, in this case always &#39;/transactions&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
transactions = api_handler.get_user_transactions(user_id) # get transactions of user
if transactions == None: # true if user does not exist
bot.send_message(chat_id=user_id, text=&#39;This didn\&#39;t work. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info&#39;)
return
if not transactions: # true if user has no transactions
bot.send_message(chat_id=user_id, text=&#39;You do not have any transactions.&#39;)
return
else:
for transaction in transactions:
comment = hf.make_markdown_proof(transaction[&#39;comment&#39;]) or &#34;\(no desc\)&#34; # if comment is empty, make it &#34;no desc&#34;
isin = hf.make_markdown_proof(transaction[&#39;isin&#39;])
amount = hf.make_markdown_proof(transaction[&#39;count&#39;])
price = hf.make_markdown_proof(transaction[&#39;price&#39;])
time = hf.make_markdown_proof(transaction[&#39;time&#39;])
bot.send_message(chat_id=user_id, text=f&#39;_{comment}_\n{isin}\namount: {amount}\nprice: {price}\ntime: {time}&#39;, parse_mode=&#34;MARKDOWNV2&#34;)
@bot.message_handler(commands=[&#39;shares&#39;, &#39;Shares&#39;])
def send_shares(message):
&#34;&#34;&#34; send shares for user
:type message: message object bot
:param message: message that was reacted to, in this case always &#39;/shares&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
shares = api_handler.get_user_shares(user_id) # get shares of user
if shares == None: # true if user does not exist
bot.send_message(chat_id=user_id, text=&#39;This didn\&#39;t work. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info&#39;)
elif not shares: # true if user has no shares
bot.send_message(chat_id=user_id, text=&#39;You do not have any shares. Add shares on https://gruppe1.testsites.info&#39;)
else:
for element in shares:
bot.send_message(chat_id=user_id, text=share_fetcher.get_share_information_markdown(element), parse_mode=&#34;MARKDOWNV2&#34;)
@bot.message_handler(commands=[&#39;setinterval&#39;, &#39;Setinterval&#39;])
def set_new_interval(message):
&#34;&#34;&#34; Set new interval for user
:type message: message object bot
:param message: message that was reacted to, in this case always &#39;/setinterval&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
bot.send_message(chat_id=user_id, text=&#39;Type interval in cron format:\n(https://crontab.guru/)&#39;)
bot.register_next_step_handler(message, set_new_interval_step) # executes function when user sends message
def set_new_interval_step(message):
user_id = int(message.from_user.id)
interval = str(message.text)
status = api_handler.set_cron_interval(user_id, interval) # send cron to db
if status == 200:
bot.send_message(chat_id=user_id, text=&#39;Interval succesfully set.&#39;)
return
if status == -1: # only -1 when interval is invalid, not a real statuscode, but used from api_handler.set_cron_interval to tell the crontab has the wrong format
bot.send_message(chat_id=user_id, text=&#39;Invalid interval format. Try again with\n /setinterval.&#39;)
return
else:
bot.send_message(chat_id=user_id, text=f&#39;Failed setting interval. (statuscode {status})&#39;)
@bot.message_handler(func=lambda message: True) # Returning that command is unknown for any other statement
def echo_all(message):
&#34;&#34;&#34; Tell that command is not known if it is no known command
:type message: message object bot
:param message: message that was reacted to, if no other command handler gets called
:raises: none
:rtype: none
&#34;&#34;&#34;
answer = &#39;Do not know this command or text: &#39; + message.text
bot.reply_to(message, answer)
telebot.logger.setLevel(logging.DEBUG)
@bot.inline_handler(lambda query: query.query == &#39;text&#39;) # inline prints for debugging
def query_text(inline_query):
&#34;&#34;&#34; Output in the console about current user actions and status of bot
:type inline_query:
:param inline_query:
:raises: none
:rtype: none
&#34;&#34;&#34;
try:
r = types.InlineQueryResultArticle(&#39;1&#39;, &#39;Result1&#39;, types.InputTextMessageContent(&#39;hi&#39;))
r2 = types.InlineQueryResultArticle(&#39;2&#39;, &#39;Result2&#39;, types.InputTextMessageContent(&#39;hi&#39;))
bot.answer_inline_query(inline_query.id, [r, r2])
except Exception as e:
print(e)
def main_loop():
&#34;&#34;&#34; Start bot
:raises: none
:rtype: none
&#34;&#34;&#34;
bot.infinity_polling()
if __name__ == &#39;__main__&#39;:
try:
main_loop()
except KeyboardInterrupt:
print(&#39;\nExiting by user request.\n&#39;)
sys.exit(0)</code></pre>
</details>
</section>
<section>
</section>
<section>
</section>
<section>
<h2 class="section-title" id="header-functions">Functions</h2>
<dl>
<dt id="telegram_bot.bot.add_keyword"><code class="name flex">
<span>def <span class="ident">add_keyword</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"><p>Add keyword to user
:type message: message object bot
:param message: message that was reacted to, in this case always '/addkeyword'</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(commands=[&#39;addkeyword&#39;, &#39;Addkeyword&#39;]) # /addkeyword -&gt; add keyword to user
def add_keyword(message):
&#34;&#34;&#34; Add keyword to user
:type message: message object bot
:param message: message that was reacted to, in this case always &#39;/addkeyword&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
bot.send_message(chat_id=user_id, text=&#39;Type keyword to add:&#39;)
bot.register_next_step_handler(message, store_keyword) # wait for user to send keyword, then call store_keyword function</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.echo_all"><code class="name flex">
<span>def <span class="ident">echo_all</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"><p>Tell that command is not known if it is no known command
:type message: message object bot
:param message: message that was reacted to, if no other command handler gets called</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(func=lambda message: True) # Returning that command is unknown for any other statement
def echo_all(message):
&#34;&#34;&#34; Tell that command is not known if it is no known command
:type message: message object bot
:param message: message that was reacted to, if no other command handler gets called
:raises: none
:rtype: none
&#34;&#34;&#34;
answer = &#39;Do not know this command or text: &#39; + message.text
bot.reply_to(message, answer)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.main_loop"><code class="name flex">
<span>def <span class="ident">main_loop</span></span>(<span>)</span>
</code></dt>
<dd>
<div class="desc"><p>Start bot
:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def main_loop():
&#34;&#34;&#34; Start bot
:raises: none
:rtype: none
&#34;&#34;&#34;
bot.infinity_polling()</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.query_text"><code class="name flex">
<span>def <span class="ident">query_text</span></span>(<span>inline_query)</span>
</code></dt>
<dd>
<div class="desc"><p>Output in the console about current user actions and status of bot
:type inline_query:
:param inline_query:</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.inline_handler(lambda query: query.query == &#39;text&#39;) # inline prints for debugging
def query_text(inline_query):
&#34;&#34;&#34; Output in the console about current user actions and status of bot
:type inline_query:
:param inline_query:
:raises: none
:rtype: none
&#34;&#34;&#34;
try:
r = types.InlineQueryResultArticle(&#39;1&#39;, &#39;Result1&#39;, types.InputTextMessageContent(&#39;hi&#39;))
r2 = types.InlineQueryResultArticle(&#39;2&#39;, &#39;Result2&#39;, types.InputTextMessageContent(&#39;hi&#39;))
bot.answer_inline_query(inline_query.id, [r, r2])
except Exception as e:
print(e)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.remove_keyword"><code class="name flex">
<span>def <span class="ident">remove_keyword</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"><p>Remove keyword from user
:type message: message object bot
:param message: message that was reacted to, in this case always '/removekeyword'</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(commands=[&#39;removekeyword&#39;, &#39;Removekeyword&#39;]) # /removekeyword -&gt; remove keyword from user
def remove_keyword(message):
&#34;&#34;&#34; Remove keyword from user
:type message: message object bot
:param message: message that was reacted to, in this case always &#39;/removekeyword&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
bot.send_message(chat_id=user_id, text=&#39;Type keyword to remove:&#39;)
bot.register_next_step_handler(message, remove_keyword_step) # wait for user to send keyword to remove, then call remove_keyword_step function</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.remove_keyword_step"><code class="name flex">
<span>def <span class="ident">remove_keyword_step</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def remove_keyword_step(message):
user_id = int(message.from_user.id)
keyword = str(message.text).lower()
status = api_handler.delete_keyword(user_id, keyword)
if status == 200: # statuscode 200 means keyword was removed successfully without errors
bot.send_message(chat_id=user_id, text=f&#39;Keyword &#34;{keyword}&#34; removed.&#39;) # checking if keyword to remove is in database are handled in database, not here
else:
bot.send_message(chat_id=user_id, text=f&#39;Failed deleting keyword &#34;{keyword}&#34;. (statuscode {status})&#39;)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.remove_share"><code class="name flex">
<span>def <span class="ident">remove_share</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"><p>Remove share from portfolio
:type message: message object bot
:param message: message that was reacted to, in this case always '/removeshare'</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(commands=[&#39;removeshare&#39;, &#39;Removeshare&#39;])
def remove_share(message):
&#34;&#34;&#34; Remove share from portfolio
:type message: message object bot
:param message: message that was reacted to, in this case always &#39;/removeshare&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
bot.send_message(chat_id=user_id, text=&#39;Type ISIN/Symbol/CompanyName of share to remove (if you are unsure do /shares, find your share and insert the value above amount):&#39;)
bot.register_next_step_handler(message, remove_share_step) # wait for user to send ISIN, then call remove_share_step function</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.remove_share_step"><code class="name flex">
<span>def <span class="ident">remove_share_step</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def remove_share_step(message):
user_id = int(message.from_user.id)
isin = str(message.text)
status = api_handler.delete_share(int(user_id), str(isin)) # remove share from portfolio
if status == 200: # statuscode 200 means share was removed successfully without errors
bot.send_message(chat_id=user_id, text=f&#39;Share &#34;{isin}&#34; removed.&#39;) # checking if share to remove is in database are handled in database, not here
else:
bot.send_message(chat_id=user_id, text=f&#39;Failed deleting share &#34;{isin}&#34;. (statuscode {status})\nMake sure that the share is in your portfolio and written exactly like there.&#39;)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.send_all_news"><code class="name flex">
<span>def <span class="ident">send_all_news</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"><p>Get news for keywords of user
:type message: message object bot
:param message: message that was reacted to, in this case always containing '/allnews'</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(commands=[&#39;allnews&#39;, &#39;Allnews&#39;]) # /allnews -&gt; get all news
def send_all_news(message):
&#34;&#34;&#34; Get news for keywords of user
:type message: message object bot
:param message: message that was reacted to, in this case always containing &#39;/allnews&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
keywords = api_handler.get_user_keywords(user_id) # get keywords of user
if keywords == None: # true if user is not registered
bot.send_message(chat_id=user_id, text=&#39;This didn\&#39;t work. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info&#39;)
return
if not keywords: # true if user is registered but does not have any keywords
bot.send_message(chat_id=user_id, text=&#39;You have no keywords. Please add some keywords with /addkeyword&#39;)
return
keywords_search = &#39; OR &#39;.join(keywords) # concat all keywords with OR -&gt; NewsAPI can understand OR, AND, NOT etc.
now = dt.datetime.now().date() # get current date
from_date = now - dt.timedelta(days=7) # get date 7 days ago -&gt; limit age of news to 7 days old max
from_date_formatted = dt.datetime.strftime(from_date, &#39;%Y-%m-%d&#39;)
news_list = news.get_all_news_by_keyword(keywords_search, from_date_formatted)[&#34;articles&#34;] # array of JSON article objects
if news_list: # true if news_list is not empty
for article in news_list:
formatted_article = news.format_article(article)
bot.send_message(chat_id=user_id, text=formatted_article, parse_mode=&#34;MARKDOWNV2&#34;) # Markdown allows to write bold text with * etc.
else:
bot.send_message(chat_id=user_id, text=&#39;No news found for your keywords.&#39;)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.send_all_users"><code class="name flex">
<span>def <span class="ident">send_all_users</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"><p>Send all users, only possible for admins
:type message: message object bot
:param message: message that was reacted to, in this case always containing '/users'</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(commands=[&#39;users&#39;, &#39;Users&#39;]) # /users -&gt; sending all users
def send_all_users(message):
&#34;&#34;&#34; Send all users, only possible for admins
:type message: message object bot
:param message: message that was reacted to, in this case always containing &#39;/users&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
user_data = api_handler.get_user(user_id)
if (user_data[&#34;admin&#34;] == False): # check if user has admin rights
bot.reply_to(message, &#34;You have to be an admin to use this command&#34;)
return
user_list = api_handler.get_all_users()
user_count = len(user_list)
bot.send_message(chat_id=user_id, text=&#34;There are &#34; + str(user_count) + &#34; users in the database:&#34;)
for user in user_list:
username = user[&#39;username&#39;]
email = user[&#39;email&#39;]
id = user[&#39;telegram_user_id&#39;]
cron = user[&#39;cron&#39;]
admin = user[&#39;admin&#39;]
bot.send_message(chat_id=user_id, text=f&#39;Username: {username}\nEmail: {email}\nID: {id}\nCron: {cron}\nAdmin: {admin}&#39;) # format user data into readable message text</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.send_help"><code class="name flex">
<span>def <span class="ident">send_help</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"><p>Send all functions
:type message: message object bot
:param message: message that was reacted to, in this case always containing '/help'</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(commands=[&#39;help&#39;, &#39;Help&#39;]) # /help -&gt; sending all functions
def send_help(message):
&#34;&#34;&#34; Send all functions
:type message: message object bot
:param message: message that was reacted to, in this case always containing &#39;/help&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
bot.reply_to(message,
&#34;/id or /auth get your user id\n/update get updates on your shares.\n/shares get update on interesting shares\n/setAdmin set admin rights of user (ADMIN)\n/users see all users. (ADMIN)\n/me get my user info\n/news get top article for each keyword.\n/allnews get all news (last 7 days)\n/keywords get all your keywords\n/addkeyword add a keyword\n/removekeyword remove a keyword\n/transactions get all transactions\n/newtransaction create new transaction\n/share get price of specific share\n/portfolio see own portfolio\n/removeshare removes share from portfolio\n/interval get update interval\n/setinterval set update interval\n For further details see https://gruppe1.testsites.info&#34;)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.send_id"><code class="name flex">
<span>def <span class="ident">send_id</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"><p>Send user id for authentication with browser
:type message: message object bot
:param message: message that was reacted to, in this case always containing '/id' or '/auth'</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(commands=[&#39;id&#39;, &#39;auth&#39;, &#39;Id&#39;, &#39;Auth&#39;]) # /auth or /id -&gt; Authentication with user_id over web tool
def send_id(message):
&#34;&#34;&#34; Send user id for authentication with browser
:type message: message object bot
:param message: message that was reacted to, in this case always containing &#39;/id&#39; or &#39;/auth&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
answer = &#39;Your ID/Authentication Code is: [&#39; + str(message.from_user.id) + &#39;]. Enter this code in the settings on https://gruppe1.testsites.info to get updates on your shares.&#39;
bot.reply_to(message, answer)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.send_interval"><code class="name flex">
<span>def <span class="ident">send_interval</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"><p>send interval for user
:type message: message object bot
:param message: message that was reacted to, in this case always '/interval'</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(commands=[&#39;interval&#39;, &#39;Interval&#39;])
def send_interval(message):
&#34;&#34;&#34; send interval for user
:type message: message object bot
:param message: message that was reacted to, in this case always &#39;/interval&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
user_data = api_handler.get_user(user_id) # get cron interval of user (stored in user data)
if user_data == None: # true if user is not registered in DB
bot.send_message(chat_id=user_id, text=&#39;This didn\&#39;t work. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info and set an interval with /setinterval&#39;)
return
else: # send interval
interval = str(user_data[&#39;cron&#39;]) # get cron from user data
if interval == &#39;None&#39;: # true if user has no cron set
bot.send_message(chat_id=user_id, text=&#39;You do not have an interval set. Set one with /setinterval&#39;)
return
formatted_interval = str(interval).replace(&#39; &#39;, &#39;_&#39;) # replace spaces with underscores to add to url of crontab.guru
bot.send_message(chat_id=user_id, text=f&#39;Your update interval: {interval} (https://crontab.guru/#{formatted_interval})&#39;)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.send_keywords"><code class="name flex">
<span>def <span class="ident">send_keywords</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"><p>Send keywords of user
:type message: message object bot
:param message: message that was reacted to, in this case always '/keywords'</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(commands=[&#39;keywords&#39;, &#39;Keywords&#39;]) # /keywords -&gt; get keywords of user
def send_keywords(message):
&#34;&#34;&#34; Send keywords of user
:type message: message object bot
:param message: message that was reacted to, in this case always &#39;/keywords&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
keywords = api_handler.get_user_keywords(user_id) # get keywords of user
if keywords == None: # true if user is not registered
bot.send_message(chat_id=user_id, text=&#39;This didn\&#39;t work. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info&#39;)
return
if not keywords: # true if user is registered but does not have any keywords
bot.send_message(chat_id=user_id, text=&#39;No keywords set for this account. Add keywords by using /addkeyword&#39;)
return
else: # send keyword list
keywords_str = &#39;, &#39;.join(keywords)
keywords_str = hf.make_markdown_proof(keywords_str)
text = f&#39;Your keywords are: _{keywords_str}_&#39;
bot.send_message(chat_id=user_id, text=text, parse_mode=&#34;MARKDOWNV2&#34;)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.send_news"><code class="name flex">
<span>def <span class="ident">send_news</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"><p>Get news for keywords of user</p>
<p>:type message: message object bot
:param message: message that was reacted to, in this case always containing '/news'</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(commands=[&#39;news&#39;, &#39;News&#39;]) # /news -&gt; get news for specific keyword
def send_news(message):
&#34;&#34;&#34; Get news for keywords of user
:type message: message object bot
:param message: message that was reacted to, in this case always containing &#39;/news&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
keywords = api_handler.get_user_keywords(user_id) # get keywords of user
if keywords == None: # true if user is not registered
bot.send_message(chat_id=user_id, text=&#39;This didn\&#39;t work. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info&#39;)
return
if not keywords: # true if user is registered but does not have any keywords
bot.send_message(chat_id=user_id, text=&#39;You have no keywords. Please add some keywords with /addkeyword&#39;)
return
if keywords:
for keyword in keywords:
top_news = news.get_top_news_by_keyword(keyword)[&#34;articles&#34;]
if top_news == None: # true if request to NewsAPI failed
bot.send_message(chat_id=user_id, text=&#39;News Server did not respond correctly. Try again later.&#39;)
if not top_news: # true if no news found for keyword (empty list)
keyword = hf.make_markdown_proof(keyword)
bot.send_message(chat_id=user_id, text=f&#39;No news found for keyword: *{keyword}*&#39;, parse_mode=&#34;MARKDOWNV2&#34;)
else:
keyword = hf.make_markdown_proof(keyword)
formatted_article = news.format_article(top_news[0]) # only format and send most popular news
bot.send_message(chat_id=user_id, text=f&#34;_keyword: {keyword}_\n\n&#34; + formatted_article, parse_mode=&#34;MARKDOWNV2&#34;) # do not use v2 because of bugs related t &#34;.&#34; in links</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.send_portfolio"><code class="name flex">
<span>def <span class="ident">send_portfolio</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"><p>Send portfolio of user
:type message: message object bot
:param message: message that was reacted to, in this case always '/portfolio'</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(commands=[&#39;portfolio&#39;, &#39;Portfolio&#39;])
def send_portfolio(message):
&#34;&#34;&#34; Send portfolio of user
:type message: message object bot
:param message: message that was reacted to, in this case always &#39;/portfolio&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
portfolio = api_handler.get_user_portfolio(user_id) # get portfolio of user as json
if portfolio == None: # true if user is not registered
bot.send_message(chat_id=user_id, text=&#39;This didn\&#39;t work. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info&#39;)
return
if not portfolio: # true if user is registered but does not have any stocks in portfolio
bot.send_message(chat_id=user_id, text=&#39;You do not have any stocks in your portfolio.&#39;)
return
else: # send portfolio
for stock in portfolio:
comment = hf.make_markdown_proof(str(stock[&#34;comment&#34;])) # comment may be written name of stock, comment is made by user when adding an stock to portfolio
count = hf.make_markdown_proof(&#34;{:.2f}&#34;.format(float(stock[&#34;count&#34;]))) # round count to 2 decimal places
isin = hf.make_markdown_proof(str(stock[&#34;isin&#34;]))
worth = hf.make_markdown_proof(&#34;{:.2f}&#34;.format(float(stock[&#34;current_price&#34;]) * float(stock[&#34;count&#34;]))) # round current_price to 2 decimal places
bot.send_message(chat_id=user_id, text=f&#39;*{comment}*\n_{isin}_\namount: {count}\nworth: ${worth}&#39;, parse_mode=&#34;MARKDOWNV2&#34;) # formatted message in markdown</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.send_share_price"><code class="name flex">
<span>def <span class="ident">send_share_price</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def send_share_price(message):
str_share_price = share_fetcher.get_share_information_markdown(str(message.text))
bot.reply_to(message, str_share_price, parse_mode=&#34;MARKDOWNV2&#34;)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.send_share_update"><code class="name flex">
<span>def <span class="ident">send_share_update</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"><p>Send price of a specific share
:type message: message object bot
:param message: message that was reacted to, in this case always containing '/share'</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(commands=[&#39;share&#39;, &#39;Share&#39;]) # /share -&gt; get share price
def send_share_update(message):
&#34;&#34;&#34; Send price of a specific share
:type message: message object bot
:param message: message that was reacted to, in this case always containing &#39;/share&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
bot.send_message(chat_id=user_id, text=&#39;Send Symbol/ISIN of share or name of company:&#39;)
bot.register_next_step_handler(message, send_share_price)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.send_shares"><code class="name flex">
<span>def <span class="ident">send_shares</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"><p>send shares for user
:type message: message object bot
:param message: message that was reacted to, in this case always '/shares'</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(commands=[&#39;shares&#39;, &#39;Shares&#39;])
def send_shares(message):
&#34;&#34;&#34; send shares for user
:type message: message object bot
:param message: message that was reacted to, in this case always &#39;/shares&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
shares = api_handler.get_user_shares(user_id) # get shares of user
if shares == None: # true if user does not exist
bot.send_message(chat_id=user_id, text=&#39;This didn\&#39;t work. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info&#39;)
elif not shares: # true if user has no shares
bot.send_message(chat_id=user_id, text=&#39;You do not have any shares. Add shares on https://gruppe1.testsites.info&#39;)
else:
for element in shares:
bot.send_message(chat_id=user_id, text=share_fetcher.get_share_information_markdown(element), parse_mode=&#34;MARKDOWNV2&#34;)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.send_start"><code class="name flex">
<span>def <span class="ident">send_start</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"><p>Sending welcome message to new user
:type message: message object bot
:param message: message that was reacted to, in this case always containing '/start'</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(commands=[&#39;start&#39;, &#39;Start&#39;])
def send_start(message):
&#34;&#34;&#34; Sending welcome message to new user
:type message: message object bot
:param message: message that was reacted to, in this case always containing &#39;/start&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
bot.reply_to(message, &#34;Welcome to this share bot project. \
\nType /help to get information on what this bot can do. \
\nAlso see https://gruppe1.testsites.info \
to start configuring your bot&#34;)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.send_status"><code class="name flex">
<span>def <span class="ident">send_status</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"><p>Sends status to user
:type message: message object bot
:param message: message that was reacted to, if no other command handler gets called</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(commands=[&#39;status&#39;, &#39;Status&#39;])
def send_status(message):
&#34;&#34;&#34; Sends status to user
:type message: message object bot
:param message: message that was reacted to, if no other command handler gets called
:raises: none
:rtype: none
&#34;&#34;&#34;
bot.reply_to(message, &#34;bot is running&#34;)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.send_to_user"><code class="name flex">
<span>def <span class="ident">send_to_user</span></span>(<span>pText, pUser_id)</span>
</code></dt>
<dd>
<div class="desc"><p>Send message to user
:type pText: string
:param pText: Text to send to user</p>
<p>:type pUser_id: int
:param pUser_id: user to send to. per default me (Florian Kellermann)</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def send_to_user(pText, pUser_id):
&#34;&#34;&#34; 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
:rtype: none
&#34;&#34;&#34;
bot.send_message(chat_id=pUser_id, text=pText)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.send_transactions"><code class="name flex">
<span>def <span class="ident">send_transactions</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"><p>send transactions for user
:type message: message object bot
:param message: message that was reacted to, in this case always '/transactions'</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(commands=[&#39;transactions&#39;, &#39;Transactions&#39;])
def send_transactions(message):
&#34;&#34;&#34; send transactions for user
:type message: message object bot
:param message: message that was reacted to, in this case always &#39;/transactions&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
transactions = api_handler.get_user_transactions(user_id) # get transactions of user
if transactions == None: # true if user does not exist
bot.send_message(chat_id=user_id, text=&#39;This didn\&#39;t work. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info&#39;)
return
if not transactions: # true if user has no transactions
bot.send_message(chat_id=user_id, text=&#39;You do not have any transactions.&#39;)
return
else:
for transaction in transactions:
comment = hf.make_markdown_proof(transaction[&#39;comment&#39;]) or &#34;\(no desc\)&#34; # if comment is empty, make it &#34;no desc&#34;
isin = hf.make_markdown_proof(transaction[&#39;isin&#39;])
amount = hf.make_markdown_proof(transaction[&#39;count&#39;])
price = hf.make_markdown_proof(transaction[&#39;price&#39;])
time = hf.make_markdown_proof(transaction[&#39;time&#39;])
bot.send_message(chat_id=user_id, text=f&#39;_{comment}_\n{isin}\namount: {amount}\nprice: {price}\ntime: {time}&#39;, parse_mode=&#34;MARKDOWNV2&#34;)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.send_user"><code class="name flex">
<span>def <span class="ident">send_user</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"><p>Send user data
:type message: message object bot
:param message: message that was reacted to, in this case always containing '/me'</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(commands=[&#39;me&#39;, &#39;Me&#39;]) # /me -&gt; sending user info
def send_user(message):
&#34;&#34;&#34; Send user data
:type message: message object bot
:param message: message that was reacted to, in this case always containing &#39;/me&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
user_data = api_handler.get_user(user_id)
if not user_data or user_data == None: # true if user is not registered
bot.reply_to(message, &#34;This didn\&#39;t work. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info&#34;)
return
username = user_data[&#39;username&#39;]
email = user_data[&#39;email&#39;]
user_id = user_data[&#39;telegram_user_id&#39;]
cron = user_data[&#39;cron&#39;]
admin = user_data[&#39;admin&#39;]
bot.reply_to(message, f&#39;Username: {username}\nEmail: {email}\nID: {user_id}\nCron: {cron}\nAdmin: {admin}&#39;) # format user data into readable message text</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.send_version"><code class="name flex">
<span>def <span class="ident">send_version</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"><p>Sending programm version
:type message: message object bot
:param message: message that was reacted to, in this case always containing '/version'</p>
<p>:raises: none</p>
<p>:rtype:none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(commands=[&#39;version&#39;, &#39;Version&#39;])
def send_version(message):
&#34;&#34;&#34; Sending programm version
:type message: message object bot
:param message: message that was reacted to, in this case always containing &#39;/version&#39;
:raises: none
:rtype:none
&#34;&#34;&#34;
bot.reply_to(message, &#34;the current bot version is &#34; + bot_version)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.set_admin"><code class="name flex">
<span>def <span class="ident">set_admin</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"><p>Set admin rights to user
:type message: message object bot
:param message: message that was reacted to, in this case always containing '/setAdmin'</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(commands=[&#39;setAdmin&#39;, &#39;SetAdmin&#39;, &#39;setadmin&#39;, &#39;Setadmin&#39;]) # set admin rights to user TBD: not working!!
def set_admin(message):
&#34;&#34;&#34; Set admin rights to user
:type message: message object bot
:param message: message that was reacted to, in this case always containing &#39;/setAdmin&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
user_data = api_handler.get_user(user_id)
if (user_data[&#34;admin&#34;] == False): # check if user has admin rights
bot.reply_to(message, &#34;You have to be an admin to use this command&#34;)
return
bot.send_message(chat_id=user_id, text=&#39;send email and true if this account should have admin rights, else false\n in format: &lt;email&gt;,&lt;is_admin&gt;&#39;) # request email and admin rights to change to
bot.register_next_step_handler(message, set_admin_step)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.set_admin_step"><code class="name flex">
<span>def <span class="ident">set_admin_step</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def set_admin_step(message):
str_message = str(message.text)
args_message = str_message.split(&#39;,&#39;) # split message into email and admin rights
if len(args_message) != 2: # make sure 2 args (email,is_admin) are given
bot.reply_to(message, &#34;exactly 2 arguments (&lt;email&gt;,&lt;is_admin&gt;) required, try again&#34;)
return
email = args_message[0]
is_admin = False # default: False
if args_message[1].lower() == &#34;true&#34;: # if user types true, set is_admin to true
is_admin = True
status = api_handler.set_admin(email, is_admin) # set admin in db
if (status == 200):
bot.reply_to(message, &#34;Admin rights set&#34;)
else:
bot.reply_to(message, f&#34;Admin rights could not be set ({status})&#34;)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.set_new_interval"><code class="name flex">
<span>def <span class="ident">set_new_interval</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"><p>Set new interval for user
:type message: message object bot
:param message: message that was reacted to, in this case always '/setinterval'</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(commands=[&#39;setinterval&#39;, &#39;Setinterval&#39;])
def set_new_interval(message):
&#34;&#34;&#34; Set new interval for user
:type message: message object bot
:param message: message that was reacted to, in this case always &#39;/setinterval&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
bot.send_message(chat_id=user_id, text=&#39;Type interval in cron format:\n(https://crontab.guru/)&#39;)
bot.register_next_step_handler(message, set_new_interval_step) # executes function when user sends message</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.set_new_interval_step"><code class="name flex">
<span>def <span class="ident">set_new_interval_step</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def set_new_interval_step(message):
user_id = int(message.from_user.id)
interval = str(message.text)
status = api_handler.set_cron_interval(user_id, interval) # send cron to db
if status == 200:
bot.send_message(chat_id=user_id, text=&#39;Interval succesfully set.&#39;)
return
if status == -1: # only -1 when interval is invalid, not a real statuscode, but used from api_handler.set_cron_interval to tell the crontab has the wrong format
bot.send_message(chat_id=user_id, text=&#39;Invalid interval format. Try again with\n /setinterval.&#39;)
return
else:
bot.send_message(chat_id=user_id, text=f&#39;Failed setting interval. (statuscode {status})&#39;)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.set_new_transaction"><code class="name flex">
<span>def <span class="ident">set_new_transaction</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"><p>Set new transaction for user
:type message: message object bot
:param message: message that was reacted to, in this case always '/newtransaction'</p>
<p>:raises: none</p>
<p>:rtype: none</p></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(commands=[&#39;newtransaction&#39;, &#39;Newtransaction&#39;]) # tbd not working rn may be deleted in future
def set_new_transaction(message):
&#34;&#34;&#34; Set new transaction for user
:type message: message object bot
:param message: message that was reacted to, in this case always &#39;/newtransaction&#39;
:raises: none
:rtype: none
&#34;&#34;&#34;
user_id = int(message.from_user.id)
bot.send_message(chat_id=user_id,
text=&#39;Type &#34;&lt;name of stock&gt;,&lt;isin/name/symbol&gt;,&lt;amount&gt;,&lt;price_per_stock_usd&gt;&#34; (time of transaction will be set to now, negative amount is selling, positive is buying):&#39;)
bot.register_next_step_handler(message, set_new_transaction_step)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.set_new_transaction_step"><code class="name flex">
<span>def <span class="ident">set_new_transaction_step</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def set_new_transaction_step(message):
user_id = int(message.from_user.id)
if not re.match(r&#34;[A-Za-z0-9 ]+,[A-Za-z0-9]+,(-)?[0-9]+(.[0-9]+)?,[0-9]+(.[0-9]+)?&#34;, message.text):
bot.send_message(chat_id=user_id, text=&#39;Invalid format \n(e.g. Apple,US0378331005,53.2,120.4).\n Try again with /newtransaction.&#39;)
return
transaction_data = str(message.text).split(&#39;,&#39;)
desc = str(transaction_data[0])
isin = str(transaction_data[1])
amount = float(transaction_data[2])
price = float(transaction_data[3])
time = dt.datetime.now().isoformat()
print(&#34;\n\n\n\n\n&#34;)
print(f&#34;{isin},{amount},{price},{time}&#34;)
status = api_handler.set_transaction(user_id, desc, isin, amount, price, time)
if status == 200:
bot.send_message(chat_id=user_id, text=&#39;Transaction succesfully added.&#39;)
else:
bot.send_message(chat_id=user_id, text=f&#39;Failed adding transaction. (statuscode {status})&#39;)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.store_keyword"><code class="name flex">
<span>def <span class="ident">store_keyword</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def store_keyword(message):
user_id = int(message.from_user.id)
keyword = str(message.text).lower() # lower to ensure Bitcoin and bitcoin is not stored as individual keywords
status = api_handler.set_keyword(user_id, keyword) # set keyword in database
if status == 200: # statuscode 200 means keyword was added successfully without errors
bot.send_message(chat_id=user_id, text=f&#39;Keyword &#34;{keyword}&#34; added.&#39;) # duplicate keywords are denied by Database, so no need to check for that here
else:
bot.send_message(chat_id=user_id, text=f&#39;Keyword &#34;{keyword}&#34; could not be stored. Make sure to connect your telegram id (/id) on https://gruppe1.testsites.info (statuscode {status})&#39;)</code></pre>
</details>
</dd>
<dt id="telegram_bot.bot.update_for_user"><code class="name flex">
<span>def <span class="ident">update_for_user</span></span>(<span>message)</span>
</code></dt>
<dd>
<div class="desc"></div>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">@bot.message_handler(commands=[&#39;update&#39;, &#39;Update&#39;]) # /update -&gt; update shares
def update_for_user(message):
p_user_id = int(message.from_user.id)
p_my_handler = api_handler
share_symbols = []
share_amounts = []
my_portfolio = p_my_handler.get_user_portfolio(p_user_id)
for element in my_portfolio:
if element[&#34;count&#34;] != &#39;&#39; and element[&#34;isin&#34;] != &#39;&#39;:
print(element[&#34;count&#34;], element[&#34;isin&#34;])
share_symbols.append(element[&#34;isin&#34;])
share_amounts.append(element[&#34;count&#34;])
my_user = p_my_handler.get_user(p_user_id)
send_to_user(&#34;Hello %s this is your share update:&#34; % str(my_user[&#34;username&#34;]), pUser_id=p_user_id)
if len(share_symbols) != 0:
for i in range(len(share_symbols)):
my_price = share_fetcher.get_share_price_no_currency(share_symbols[i])
my_update_message = f&#39;{share_fetcher.get_share_information_markdown(share_symbols[i])}\ncount: {share_amounts[i]}\nTotal: {hf.make_markdown_proof(round(float(my_price) * float(share_amounts[i]), 2))} EUR&#39;
bot.send_message(chat_id=p_user_id, text=my_update_message, parse_mode=&#34;MARKDOWNV2&#34;)
else:
send_to_user(&#34;No shares found for your account. Check https://gruppe1.testsites.info to change your settings and add shares.&#34;, pUser_id=p_user_id)</code></pre>
</details>
</dd>
</dl>
</section>
<section>
</section>
</article>
<nav id="sidebar">
<h1>Index</h1>
<div class="toc">
<ul></ul>
</div>
<ul id="index">
<li><h3>Super-module</h3>
<ul>
<li><code><a title="telegram_bot" href="index.html">telegram_bot</a></code></li>
</ul>
</li>
<li><h3><a href="#header-functions">Functions</a></h3>
<ul class="">
<li><code><a title="telegram_bot.bot.add_keyword" href="#telegram_bot.bot.add_keyword">add_keyword</a></code></li>
<li><code><a title="telegram_bot.bot.echo_all" href="#telegram_bot.bot.echo_all">echo_all</a></code></li>
<li><code><a title="telegram_bot.bot.main_loop" href="#telegram_bot.bot.main_loop">main_loop</a></code></li>
<li><code><a title="telegram_bot.bot.query_text" href="#telegram_bot.bot.query_text">query_text</a></code></li>
<li><code><a title="telegram_bot.bot.remove_keyword" href="#telegram_bot.bot.remove_keyword">remove_keyword</a></code></li>
<li><code><a title="telegram_bot.bot.remove_keyword_step" href="#telegram_bot.bot.remove_keyword_step">remove_keyword_step</a></code></li>
<li><code><a title="telegram_bot.bot.remove_share" href="#telegram_bot.bot.remove_share">remove_share</a></code></li>
<li><code><a title="telegram_bot.bot.remove_share_step" href="#telegram_bot.bot.remove_share_step">remove_share_step</a></code></li>
<li><code><a title="telegram_bot.bot.send_all_news" href="#telegram_bot.bot.send_all_news">send_all_news</a></code></li>
<li><code><a title="telegram_bot.bot.send_all_users" href="#telegram_bot.bot.send_all_users">send_all_users</a></code></li>
<li><code><a title="telegram_bot.bot.send_help" href="#telegram_bot.bot.send_help">send_help</a></code></li>
<li><code><a title="telegram_bot.bot.send_id" href="#telegram_bot.bot.send_id">send_id</a></code></li>
<li><code><a title="telegram_bot.bot.send_interval" href="#telegram_bot.bot.send_interval">send_interval</a></code></li>
<li><code><a title="telegram_bot.bot.send_keywords" href="#telegram_bot.bot.send_keywords">send_keywords</a></code></li>
<li><code><a title="telegram_bot.bot.send_news" href="#telegram_bot.bot.send_news">send_news</a></code></li>
<li><code><a title="telegram_bot.bot.send_portfolio" href="#telegram_bot.bot.send_portfolio">send_portfolio</a></code></li>
<li><code><a title="telegram_bot.bot.send_share_price" href="#telegram_bot.bot.send_share_price">send_share_price</a></code></li>
<li><code><a title="telegram_bot.bot.send_share_update" href="#telegram_bot.bot.send_share_update">send_share_update</a></code></li>
<li><code><a title="telegram_bot.bot.send_shares" href="#telegram_bot.bot.send_shares">send_shares</a></code></li>
<li><code><a title="telegram_bot.bot.send_start" href="#telegram_bot.bot.send_start">send_start</a></code></li>
<li><code><a title="telegram_bot.bot.send_status" href="#telegram_bot.bot.send_status">send_status</a></code></li>
<li><code><a title="telegram_bot.bot.send_to_user" href="#telegram_bot.bot.send_to_user">send_to_user</a></code></li>
<li><code><a title="telegram_bot.bot.send_transactions" href="#telegram_bot.bot.send_transactions">send_transactions</a></code></li>
<li><code><a title="telegram_bot.bot.send_user" href="#telegram_bot.bot.send_user">send_user</a></code></li>
<li><code><a title="telegram_bot.bot.send_version" href="#telegram_bot.bot.send_version">send_version</a></code></li>
<li><code><a title="telegram_bot.bot.set_admin" href="#telegram_bot.bot.set_admin">set_admin</a></code></li>
<li><code><a title="telegram_bot.bot.set_admin_step" href="#telegram_bot.bot.set_admin_step">set_admin_step</a></code></li>
<li><code><a title="telegram_bot.bot.set_new_interval" href="#telegram_bot.bot.set_new_interval">set_new_interval</a></code></li>
<li><code><a title="telegram_bot.bot.set_new_interval_step" href="#telegram_bot.bot.set_new_interval_step">set_new_interval_step</a></code></li>
<li><code><a title="telegram_bot.bot.set_new_transaction" href="#telegram_bot.bot.set_new_transaction">set_new_transaction</a></code></li>
<li><code><a title="telegram_bot.bot.set_new_transaction_step" href="#telegram_bot.bot.set_new_transaction_step">set_new_transaction_step</a></code></li>
<li><code><a title="telegram_bot.bot.store_keyword" href="#telegram_bot.bot.store_keyword">store_keyword</a></code></li>
<li><code><a title="telegram_bot.bot.update_for_user" href="#telegram_bot.bot.update_for_user">update_for_user</a></code></li>
</ul>
</li>
</ul>
</nav>
</main>
<footer id="footer">
<p>Generated by <a href="https://pdoc3.github.io/pdoc" title="pdoc: Python API documentation generator"><cite>pdoc</cite> 0.10.0</a>.</p>
</footer>
</body>
</html>