This commit is contained in:
2024-09-27 09:23:55 +03:00
parent c1e481ded8
commit a7b1925e8d
12 changed files with 218 additions and 139 deletions

View File

@@ -1,12 +1,13 @@
from bot.api import telegram_api
from utils.mention import mention, userdata_extract
from utils.store import redis
from utils.mention import userdata_extract
from state.redis import redis
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
def get_newcomer_message(msg):
lang = msg["from"].get("language_code", "ru")
r = "хочет присоединиться к нам здесь" if lang == "ru" else " wants to join us here"
@@ -27,27 +28,36 @@ async def show_announce(msg):
userphotos_response = await telegram_api("getUserphotos", user_id=from_id)
file_id = ""
if isinstance(userphotos_response, dict) and userphotos_response["ok"] and userphotos_response["result"]["total_count"] > 0:
if (
isinstance(userphotos_response, dict)
and userphotos_response["ok"]
and userphotos_response["result"]["total_count"] > 0
):
logger.info("showing button with photo")
file_id = userphotos_response["result"]["photos"][0][0]["file_id"]
r = await telegram_api("sendPhoto",
r = await telegram_api(
"sendPhoto",
chat_id=chat_id,
file_id=file_id,
caption=newcomer_message,
reply_to=mid
reply_to=mid,
)
announce_msg_id = r.get("message_id")
await redis.set(f"announce:{chat_id}:{from_id}", announce_message_id)
await redis.set(f"announce:{chat_id}:{from_id}", announce_msg_id)
async def edit_announce(msg):
logger.info("editing announce")
chat_id = str(msg["chat"]["id"])
from_id = str(msg["from"]["id"])
mid = msg.get("message_id", "")
caption = get_newcomer_message(msg) + msg.get("text").replace("/message ", "")
announce_message_id = await redis.get(f"announce:{chat_id}:{from_id}")
if announce_message_id:
r = await telegram_api("editMessageCaption", chat_id=chat_id, message_id=int(announce_message_id), caption=caption)
r = await telegram_api(
"editMessageCaption",
chat_id=chat_id,
message_id=int(announce_message_id),
caption=caption,
)
await redis.set(f"announce:{chat_id}:{from_id}", r.get("message_id"))

View File

@@ -5,7 +5,7 @@ from bot.config import BOT_TOKEN
import logging
# Create a logger instance
logger = logging.getLogger('bot.api')
logger = logging.getLogger("bot.api")
logging.basicConfig(level=logging.DEBUG)
api_base = f"https://api.telegram.org/bot{BOT_TOKEN}/"
@@ -14,17 +14,20 @@ api_base = f"https://api.telegram.org/bot{BOT_TOKEN}/"
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'}
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:
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}')
logger.info(f" <<< {data}")
return data
except Exception as ex:
except Exception:
import traceback
traceback.print_exc()