TelegramAktienBot/telegram_bot/api_handler.py
2022-03-28 18:07:39 +02:00

54 lines
1.6 KiB
Python

"""
script for communicating with webservice to get data from database
"""
__author__ = "Florian Kellermann, Linus Eickhoff"
__date__ = "16.03.2022"
__version__ = "0.0.1"
__license__ = "None"
#side-dependencies: none
#Work in Progress
import sys
import os
import requests as r
class API_Handler:
#class for communicating with webservice to get data from database
def __init__(self, db_adress):
self.db_adress = db_adress
return
def get_auth_token(self, email, password):
payload = {'email': email, 'password': password}
with r.Session() as s:
p = s.post(self.db_adress + "/user/login", json=payload)
if p.status_code == 200:
self.token = p.json()["data"]['token']
return p.json()["data"]['token']
else:
#print server response
print(p.text)
return None
def get_user_keywords(self, token, user_id):
with r.Session() as s:
headers = {'Authorization': 'Bearer ' + token + ":" + user_id}
p = s.get(self.db_adress + "/api/keywords", headers=headers)
keywords_json = p.json()["data"]
keywords = []
for keyword in keywords_json:
keywords.append(keyword["keyword"])
return keywords
if __name__ == "__main__":
print("This is a module for the telegram bot. It is not intended to be run directly.")
handler = API_Handler("https://aktienbot.flokaiser.com/api")
try:
handler.get_auth_token("bot@example.com", "bot")
except None:
print("Could not connect to server.")
sys.exit(1)