2022-03-12 16:20:33 +00:00
# Work in Progress
# Api-Key: 5228016873:AAGFrh0P6brag7oD3gxXjCh5gnLLE8JMvMs
2022-03-12 17:31:10 +00:00
# text bot at t.me/projektaktienbot
# API Documentation https://core.telegram.org/bots/api
# Code examples https://github.com/eternnoir/pyTelegramBotAPI#getting-started
2022-03-12 18:44:58 +00:00
import os
2022-03-12 17:31:10 +00:00
import telebot
2022-03-12 18:24:25 +00:00
import time
import sys
import logging
from telebot import types
2022-03-12 17:31:10 +00:00
2022-03-13 09:49:51 +00:00
user_list = [ ]
class User : # Currently saving users in this class to test functionality -> later database
def __init__ ( self , p_user_id , p_chat_id ) :
self . user_id = int ( p_user_id )
self . chat_id = int ( p_chat_id )
2022-03-12 18:44:58 +00:00
bot = telebot . TeleBot ( os . getenv ( ' BOT_API_KEY ' ) )
2022-03-13 09:49:51 +00:00
@bot.message_handler ( commands = [ ' start ' ] ) # /start -> saving as new user and sending welcome
def send_start ( message ) :
new_user = User ( int ( message . from_user . id ) , int ( message . chat . id ) )
existing_already = False
for known_user in user_list :
if known_user . user_id == new_user . user_id :
existing_already = True
if existing_already == False :
user_list . add ( new_user )
bot . reply_to ( message , " Welcome to this share bot project. Type /help to get information on what this bot can do " )
2022-03-12 17:31:10 +00:00
2022-03-13 09:49:51 +00:00
@bot.message_handler ( commands = [ ' help ' ] ) # /help -> sending all functions
2022-03-12 17:31:10 +00:00
def send_welcome ( message ) :
2022-03-13 09:49:51 +00:00
bot . reply_to ( message , " /id or /auth for authentication. /update to get updates on your shares. For further details see aktienbot.flokaiser.com " )
2022-03-13 08:05:44 +00:00
2022-03-13 09:49:51 +00:00
@bot.message_handler ( commands = [ ' id ' , ' auth ' ] ) # /auth or /id -> Authentication with user_id over web tool
2022-03-13 08:05:44 +00:00
def send_id ( message ) :
2022-03-13 09:49:51 +00:00
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. '
2022-03-13 08:05:44 +00:00
bot . reply_to ( message , answer )
2022-03-12 17:31:10 +00:00
2022-03-13 09:49:51 +00:00
@bot.message_handler ( commands = [ ' update ' ] ) # /update -> send static update via user_id to this user, later fetch from database
def send_update ( message ) :
user_id = int ( message . from_user . id )
#Get Information for user with this id
bot . send_message ( chat_id = user_id , text = ' This is your update ' )
@bot.message_handler ( func = lambda message : True ) # Returning that command is unkown for any other statement
2022-03-12 17:31:10 +00:00
def echo_all ( message ) :
2022-03-13 08:05:44 +00:00
answer = ' Do not know this command or text: ' + message . text
2022-03-12 18:44:58 +00:00
bot . reply_to ( message , answer )
2022-03-12 18:24:25 +00:00
telebot . logger . setLevel ( logging . DEBUG )
2022-03-13 09:49:51 +00:00
@bot.inline_handler ( lambda query : query . query == ' text ' ) # inline prints for debugging
2022-03-12 18:24:25 +00:00
def query_text ( inline_query ) :
try :
r = types . InlineQueryResultArticle ( ' 1 ' , ' Result1 ' , types . InputTextMessageContent ( ' hi ' ) )
r2 = types . InlineQueryResultArticle ( ' 2 ' , ' Result2 ' , types . InputTextMessageContent ( ' hi ' ) )
bot . answer_inline_query ( inline_query . id , [ r , r2 ] )
except Exception as e :
print ( e )
2022-03-12 18:44:58 +00:00
2022-03-12 18:24:25 +00:00
def main_loop ( ) :
bot . infinity_polling ( )
while 1 :
time . sleep ( 3 )
2022-03-12 18:05:40 +00:00
2022-03-12 17:31:10 +00:00
2022-03-12 18:24:25 +00:00
if __name__ == ' __main__ ' :
try :
main_loop ( )
except KeyboardInterrupt :
print ( ' \n Exiting by user request. \n ' )
sys . exit ( 0 )