2023-04-23 16:54:58 +00:00
|
|
|
import aiohttp
|
2023-04-16 14:58:53 +00:00
|
|
|
import json
|
2024-01-07 09:19:46 +00:00
|
|
|
from urllib.parse import urlencode
|
|
|
|
from bot.config import BOT_TOKEN
|
2023-09-11 20:04:53 +00:00
|
|
|
import logging
|
2023-09-06 10:20:50 +00:00
|
|
|
|
2023-09-11 20:04:53 +00:00
|
|
|
# Create a logger instance
|
2024-06-03 10:27:42 +00:00
|
|
|
logger = logging.getLogger('bot.api')
|
2024-02-12 12:50:35 +00:00
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
2023-04-16 14:58:53 +00:00
|
|
|
|
2024-02-12 12:50:35 +00:00
|
|
|
api_base = f"https://api.telegram.org/bot{BOT_TOKEN}/"
|
2023-04-16 14:58:53 +00:00
|
|
|
|
|
|
|
|
2024-02-12 12:50:35 +00:00
|
|
|
async def telegram_api(endpoint: str, json_data=None, **kwargs):
|
|
|
|
try:
|
|
|
|
url = api_base + f"{endpoint}?{urlencode(kwargs)}"
|
|
|
|
is_polling = endpoint == 'getUpdates'
|
|
|
|
headers = {'Content-Type': 'application/json'}
|
|
|
|
async with aiohttp.ClientSession() as session:
|
|
|
|
url = api_base + f"{endpoint}?{urlencode(kwargs)}"
|
|
|
|
if not is_polling:
|
|
|
|
logger.info(f' >>> {url} {json_data if json_data else ""}')
|
|
|
|
async with session.get(url, data=json.dumps(json_data), headers=headers) as response:
|
|
|
|
data = await response.json()
|
|
|
|
if not is_polling:
|
|
|
|
logger.info(f' <<< {data}')
|
|
|
|
return data
|
|
|
|
except Exception as ex:
|
|
|
|
import traceback
|
|
|
|
traceback.print_exc()
|