welcomecenterbot/bot/announce.py

56 lines
1.8 KiB
Python
Raw Normal View History

2024-01-07 09:19:46 +00:00
from bot.api import telegram_api
from utils.mention import mention, userdata_extract
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)
announces = {}
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)
logger.debug(userphotos_response)
file_id = ""
if 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",
chat_id=chat_id,
file_id=file_id,
caption=newcomer_message,
reply_to=mid
)
logger.debug(r)
announces[from_id] = r.get("message_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")
announce_message_id = announces.get(from_id)
r = await telegram_api("editMessageCaption", chat_id=chat_id, message_id=announce_message_id, caption=caption)
announces[from_id] = r.get("message_id")