welcomecenterbot/bot/announce.py

63 lines
2.0 KiB
Python
Raw Permalink Normal View History

2024-01-07 09:19:46 +00:00
from bot.api import telegram_api
2024-09-27 06:23:55 +00:00
from utils.mention import userdata_extract
from state.redis import redis
2024-01-07 09:19:46 +00:00
import logging
logger = logging.getLogger(__name__)
2024-09-27 08:28:41 +00:00
2024-01-07 09:19:46 +00:00
def get_newcomer_message(msg):
lang = msg["from"].get("language_code", "ru")
r = "хочет присоединиться к нам здесь" if lang == "ru" else " wants to join us here"
_uid, identity, username = userdata_extract(msg["from"])
if username:
r = "@" + username + " " + r
r = identity + " " + r
return r
async def show_announce(msg):
logger.info("showing announce with photo")
chat_id = str(msg["chat"]["id"])
from_id = str(msg["from"]["id"])
mid = msg.get("message_id", "")
newcomer_message = get_newcomer_message(msg)
userphotos_response = await telegram_api("getUserphotos", user_id=from_id)
file_id = ""
2024-09-27 06:23:55 +00:00
if (
isinstance(userphotos_response, dict)
and userphotos_response["ok"]
and userphotos_response["result"]["total_count"] > 0
):
2024-01-07 09:19:46 +00:00
logger.info("showing button with photo")
file_id = userphotos_response["result"]["photos"][0][0]["file_id"]
2024-09-27 06:23:55 +00:00
r = await telegram_api(
"sendPhoto",
2024-01-07 09:19:46 +00:00
chat_id=chat_id,
file_id=file_id,
caption=newcomer_message,
2024-09-27 06:23:55 +00:00
reply_to=mid,
2024-01-07 09:19:46 +00:00
)
2024-09-26 16:59:24 +00:00
announce_msg_id = r.get("message_id")
2024-09-27 06:23:55 +00:00
await redis.set(f"announce:{chat_id}:{from_id}", announce_msg_id)
2024-01-07 09:19:46 +00:00
async def edit_announce(msg):
logger.info("editing announce")
chat_id = str(msg["chat"]["id"])
from_id = str(msg["from"]["id"])
2024-09-26 16:59:24 +00:00
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:
2024-09-27 06:23:55 +00:00
r = await telegram_api(
"editMessageCaption",
chat_id=chat_id,
message_id=int(announce_message_id),
caption=caption,
)
2024-09-26 16:59:24 +00:00
await redis.set(f"announce:{chat_id}:{from_id}", r.get("message_id"))