From 01a4c2e2d6be1fec17af6ee720890b4e642de346 Mon Sep 17 00:00:00 2001 From: justuser Date: Wed, 24 Jul 2024 16:37:08 +0300 Subject: [PATCH] =?UTF-8?q?API=20=D0=B4=D0=BB=D1=8F=20=D0=BF=D0=B5=D1=80?= =?UTF-8?q?=D0=B5=D0=B2=D0=BE=D0=B4=D0=BE=D0=B2=20TG->DS/...?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- call2api.py | 5 +++++ tg.py | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/call2api.py b/call2api.py index 15c3ef1..1bfba0f 100644 --- a/call2api.py +++ b/call2api.py @@ -108,4 +108,9 @@ def get_passwd(token, id): data = {'token': token, 'id': id} return call('api/get_passwd/', data) +def transfer_callbak(addr, token, src_id, amount): + amount = str(amount) + data = {'token': token, 'src_id': src_id, 'amount': amount} + return call(addr + 'api/get_passwd/', data) + #print( user_in_db('ee77b9d8-44f3-4e01-a702-69d5524ee50b', '1234') ) diff --git a/tg.py b/tg.py index 38521a3..a30cbe9 100644 --- a/tg.py +++ b/tg.py @@ -3,6 +3,15 @@ from telebot.formatting import hcode from telebot.types import ReplyKeyboardMarkup, KeyboardButton from datetime import datetime +# API для отправки сообщения о переводе +# TG -> DS / DS -> Mine / ... +from fastapi import FastAPI, HTTPException +from pydantic import BaseModel +import uvicorn +app = FastAPI() +# Для запуска API в потоке +from threading import Thread + from call2api import * from db import * @@ -174,4 +183,34 @@ def checks(message): help(message) +# API для переводов TG->DS / ... +class Transfer_callback(BaseModel): + token: str + src_nick: str + dst_nick: str + amount: str +@app.post('/api/transfer_callback/') +def transfer_callback(it: Transfer_callback): + token, src_nick, dst_nick, amount = it.token, it.src_nick, it.dst_nick, it.amount + db = read() + if token in db['tokens']: + dst_id = user_in_db(API_TOKEN, nick=dst_nick) + tg_dst = int(get_tg(API_TOKEN, id=dst_id)) + print(tg_dst) + bot.send_message(tg_dst, f'''Вам перевели {hcode(amount)} CDM. + +Отправитель: {hcode(src_nick)}''', parse_mode='HTML') + return 200 + else: + return 'Error' + +def run_api(): + uvicorn.run(app, host='127.0.0.1', port=2222) + +# Запускаем API для переводов +api = Thread(target=run_api) +api.Daemon = True +api.start() + +# Запускаем бота bot.infinity_polling()