backend+ telegram: регистрация переведена на классы

This commit is contained in:
2022-11-18 18:27:53 +03:00
parent 82dfc158e9
commit 210938fd78
10 changed files with 94 additions and 49 deletions

View File

@@ -6,7 +6,7 @@ with open("textsouls/config.json") as config_file:
class Backend:
base_url = config_data["backend_settings"]["base_url"]
base_url = config_data["BACKEND_SETTINGS"]["BASE_URL"]
def post(self, relative_url, data):
try:

View File

@@ -1,8 +1,8 @@
{
"main_settings": {
"bot_token": "token-from-@BotFather"
"MAIN_SETTINGS": {
"BOT_TOKEN": "token-from-@BotFather"
},
"backend_settings": {
"base_url": "http://localhost:5000"
"BACKEND_SETTINGS": {
"BASE_URL": "http://localhost:5000"
}
}

View File

@@ -7,7 +7,7 @@ from common import backend
with open("textsouls/config.json") as config_file:
config_data = json.load(config_file)
API_TOKEN = config_data["main_settings"]["bot_token"]
API_TOKEN = config_data["MAIN_SETTINGS"]["BOT_TOKEN"]
bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot)
@@ -17,18 +17,20 @@ dp = Dispatcher(bot)
async def start(message):
tg_user = message.from_user
ts_user = {
"tg_id": tg_user.id,
"id": tg_user.id,
"first_name": tg_user.first_name,
"last_name": tg_user.last_name,
"username": tg_user.username,
}
result = backend.post("/registration", ts_user)
if not result["error"] and result["response"].ok:
data = json.loads(result["response"].text)
if data["created"]:
result = backend.post("/users", ts_user)
if not result["error"]:
response_code = result["response"].status_code
if response_code == 200:
await message.reply("Добро пожаловать!")
else:
elif response_code == 400:
await message.reply("Добро пожаловать! Снова")
else:
await message.reply("Что-то другое")
else:
await message.reply("Упс! Что-то пошло не так")