Inserting correct comments
This commit is contained in:
parent
d3a99997b8
commit
d12916abef
@ -4,7 +4,7 @@ script for telegram bot and its functions
|
|||||||
"""
|
"""
|
||||||
__author__ = "Florian Kellermann, Linus Eickhoff"
|
__author__ = "Florian Kellermann, Linus Eickhoff"
|
||||||
__date__ = "11.03.2022"
|
__date__ = "11.03.2022"
|
||||||
__version__ = "0.0.3"
|
__version__ = "0.0.4"
|
||||||
__license__ = "None"
|
__license__ = "None"
|
||||||
|
|
||||||
# side-dependencies: none
|
# side-dependencies: none
|
||||||
@ -22,9 +22,11 @@ import sys
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
import news_fetcher
|
import news_fetcher
|
||||||
|
import share_fetcher
|
||||||
|
|
||||||
from telebot import types
|
from telebot import types
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
from telegram_bot.share_fetcher import Share_Handler
|
||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
@ -33,6 +35,25 @@ user_list = []
|
|||||||
|
|
||||||
class User: # Currently saving users in this class to test functionality -> later database
|
class User: # Currently saving users in this class to test functionality -> later database
|
||||||
def __init__(self, p_user_id, p_user_name, p_chat_id):
|
def __init__(self, p_user_id, p_user_name, p_chat_id):
|
||||||
|
|
||||||
|
""" Initialize a new user
|
||||||
|
:type self:
|
||||||
|
:param self: for class
|
||||||
|
|
||||||
|
:type p_user_id: int
|
||||||
|
:param p_user_id: telegram user id
|
||||||
|
|
||||||
|
:type p_user_name: str
|
||||||
|
:param p_user_name: first name of user
|
||||||
|
|
||||||
|
:type p_chat_id: int
|
||||||
|
:param p_chat_id: telegram chat id
|
||||||
|
|
||||||
|
:raises:
|
||||||
|
|
||||||
|
:rtype:
|
||||||
|
"""
|
||||||
|
|
||||||
self.user_id = int(p_user_id)
|
self.user_id = int(p_user_id)
|
||||||
self.chat_id = int(p_chat_id)
|
self.chat_id = int(p_chat_id)
|
||||||
self.user_name = str(p_user_name)
|
self.user_name = str(p_user_name)
|
||||||
@ -41,6 +62,15 @@ bot = telebot.TeleBot(os.getenv('BOT_API_KEY'))
|
|||||||
|
|
||||||
@bot.message_handler(commands=['start']) # /start -> saving as new user and sending welcome
|
@bot.message_handler(commands=['start']) # /start -> saving as new user and sending welcome
|
||||||
def send_start(message):
|
def send_start(message):
|
||||||
|
|
||||||
|
""" Description
|
||||||
|
:type message: message object bot
|
||||||
|
:param message: message that was reacted to, in this case always containing '/start'
|
||||||
|
|
||||||
|
:raises: none
|
||||||
|
|
||||||
|
:rtype: none
|
||||||
|
"""
|
||||||
new_user = User(int(message.from_user.id), message.from_user.first_name, int(message.chat.id))
|
new_user = User(int(message.from_user.id), message.from_user.first_name, int(message.chat.id))
|
||||||
existing_already = False
|
existing_already = False
|
||||||
for known_user in user_list:
|
for known_user in user_list:
|
||||||
@ -54,18 +84,48 @@ def send_start(message):
|
|||||||
|
|
||||||
@bot.message_handler(commands=['version'])
|
@bot.message_handler(commands=['version'])
|
||||||
def send_version(message):
|
def send_version(message):
|
||||||
|
|
||||||
|
""" Sending programm version
|
||||||
|
:type message: message object bot
|
||||||
|
:param message: message that was reacted to, in this case always containing '/version'
|
||||||
|
|
||||||
|
:raises: none
|
||||||
|
|
||||||
|
:rtype:none
|
||||||
|
"""
|
||||||
bot.reply_to(message, bot_version)
|
bot.reply_to(message, bot_version)
|
||||||
|
|
||||||
|
|
||||||
@bot.message_handler(commands=['help']) # /help -> sending all functions
|
@bot.message_handler(commands=['help']) # /help -> sending all functions
|
||||||
def send_welcome(message):
|
def send_welcome(message):
|
||||||
bot.reply_to(message, "/id or /auth for authentication. /update to get updates on your shares. /users to see all users. For further details see aktienbot.flokaiser.com")
|
|
||||||
|
""" Send all functions
|
||||||
|
:type message: message object bot
|
||||||
|
:param message: message that was reacted to, in this case always containing '/help'
|
||||||
|
|
||||||
|
:raises: none
|
||||||
|
|
||||||
|
:rtype: none
|
||||||
|
"""
|
||||||
|
bot.reply_to(message, "/id or /auth for authentication. /update to get updates on your shares. /users to see all users. /news to get current use for your keywords. For further details see aktienbot.flokaiser.com")
|
||||||
|
|
||||||
|
|
||||||
@bot.message_handler(commands=['users'])
|
@bot.message_handler(commands=['users'])
|
||||||
def send_all_users(message):
|
def send_all_users(message):
|
||||||
|
|
||||||
|
""" 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'
|
||||||
|
|
||||||
|
:raises: none
|
||||||
|
|
||||||
|
:rtype: none
|
||||||
|
"""
|
||||||
print('Debug: users command')
|
print('Debug: users command')
|
||||||
user_id = int(message.from_user.id)
|
user_id = int(message.from_user.id)
|
||||||
|
|
||||||
|
# tbd check if user is admin
|
||||||
|
|
||||||
answer = 'Current number of users: ' + str(len(user_list))
|
answer = 'Current number of users: ' + str(len(user_list))
|
||||||
bot.send_message(chat_id = user_id, text=answer)
|
bot.send_message(chat_id = user_id, text=answer)
|
||||||
for known_user in user_list:
|
for known_user in user_list:
|
||||||
@ -75,19 +135,51 @@ def send_all_users(message):
|
|||||||
|
|
||||||
@bot.message_handler(commands=['id', 'auth']) # /auth or /id -> Authentication with user_id over web tool
|
@bot.message_handler(commands=['id', 'auth']) # /auth or /id -> Authentication with user_id over web tool
|
||||||
def send_id(message):
|
def send_id(message):
|
||||||
|
|
||||||
|
""" 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'
|
||||||
|
|
||||||
|
:raises: none
|
||||||
|
|
||||||
|
:rtype: none
|
||||||
|
"""
|
||||||
answer = 'Your ID/Authentication Code is: [' + str(message.from_user.id) + ']. Enter this code in the settings on aktienbot.flokaiser.com to get updates on your shares.'
|
answer = 'Your ID/Authentication Code is: [' + str(message.from_user.id) + ']. Enter this code in the settings on aktienbot.flokaiser.com to get updates on your shares.'
|
||||||
bot.reply_to(message, answer)
|
bot.reply_to(message, answer)
|
||||||
|
|
||||||
|
|
||||||
@bot.message_handler(commands=['update']) # /update -> send static update via user_id to this user, later fetch from database
|
@bot.message_handler(commands=['update']) # /update -> send static update via user_id to this user, later fetch from database
|
||||||
def send_update(message):
|
def send_update(message):
|
||||||
|
|
||||||
|
""" Send update on shares
|
||||||
|
:type message: message object bot
|
||||||
|
:param message: message that was reacted to, in this case always containing '/help'
|
||||||
|
|
||||||
|
:raises: none
|
||||||
|
|
||||||
|
:rtype: none
|
||||||
|
"""
|
||||||
user_id = int(message.from_user.id)
|
user_id = int(message.from_user.id)
|
||||||
|
|
||||||
|
share_fetcher = Share_Handler()
|
||||||
|
|
||||||
#Get Information for user with this id
|
#Get Information for user with this id
|
||||||
|
#call Share_Handler
|
||||||
bot.send_message(chat_id=user_id, text='This is your update')
|
bot.send_message(chat_id=user_id, text='This is your update')
|
||||||
|
|
||||||
|
|
||||||
@bot.message_handler(commands=['news']) # /news -> send news by keyword
|
@bot.message_handler(commands=['news'])
|
||||||
def send_news(message):
|
def send_news(message):
|
||||||
|
|
||||||
|
""" Get news for keywords of user
|
||||||
|
:type message: message object bot
|
||||||
|
:param message: message that was reacted to, in this case always containing '/news'
|
||||||
|
|
||||||
|
:raises: none
|
||||||
|
|
||||||
|
:rtype: none
|
||||||
|
"""
|
||||||
|
|
||||||
keyword = "business"
|
keyword = "business"
|
||||||
user_id = int(message.from_user.id)
|
user_id = int(message.from_user.id)
|
||||||
#Get Information for user with this id
|
#Get Information for user with this id
|
||||||
@ -103,6 +195,15 @@ def send_news(message):
|
|||||||
|
|
||||||
@bot.message_handler(func=lambda message: True) # Returning that command is unkown for any other statement
|
@bot.message_handler(func=lambda message: True) # Returning that command is unkown for any other statement
|
||||||
def echo_all(message):
|
def echo_all(message):
|
||||||
|
|
||||||
|
""" 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
|
||||||
|
"""
|
||||||
answer = 'Do not know this command or text: ' + message.text
|
answer = 'Do not know this command or text: ' + message.text
|
||||||
bot.reply_to(message, answer)
|
bot.reply_to(message, answer)
|
||||||
|
|
||||||
@ -112,6 +213,15 @@ telebot.logger.setLevel(logging.DEBUG)
|
|||||||
|
|
||||||
@bot.inline_handler(lambda query: query.query == 'text') # inline prints for debugging
|
@bot.inline_handler(lambda query: query.query == 'text') # inline prints for debugging
|
||||||
def query_text(inline_query):
|
def query_text(inline_query):
|
||||||
|
|
||||||
|
""" Output in the console about current user actions and status of bot
|
||||||
|
:type inline_query:
|
||||||
|
:param inline_query:
|
||||||
|
|
||||||
|
:raises: none
|
||||||
|
|
||||||
|
:rtype: none
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
r = types.InlineQueryResultArticle('1', 'Result1', types.InputTextMessageContent('hi'))
|
r = types.InlineQueryResultArticle('1', 'Result1', types.InputTextMessageContent('hi'))
|
||||||
r2 = types.InlineQueryResultArticle('2', 'Result2', types.InputTextMessageContent('hi'))
|
r2 = types.InlineQueryResultArticle('2', 'Result2', types.InputTextMessageContent('hi'))
|
||||||
@ -121,6 +231,12 @@ def query_text(inline_query):
|
|||||||
|
|
||||||
|
|
||||||
def main_loop():
|
def main_loop():
|
||||||
|
|
||||||
|
""" Get Information about bot status every 3 seconds
|
||||||
|
:raises: none
|
||||||
|
|
||||||
|
:rtype: none
|
||||||
|
"""
|
||||||
bot.infinity_polling()
|
bot.infinity_polling()
|
||||||
while 1:
|
while 1:
|
||||||
time.sleep(3)
|
time.sleep(3)
|
||||||
|
Loading…
Reference in New Issue
Block a user