big update
This commit is contained in:
1 parent
7d05a3f2c0
commit
f45d593a84
32 files changed
+726
-320
No files matched your search
@@ -0,0 +1,70 @@
|
||||
from app.core.models.node_storage import StoredContent
|
||||
from app.core.models.content.user_content import UserContent
|
||||
from app.core.logger import make_log
|
||||
from app.core._utils.tg_process_template import tg_process_template
|
||||
from app.core._config import PROJECT_HOST
|
||||
from app.core._keyboards import get_inline_keyboard
|
||||
|
||||
|
||||
class PlayerTemplates:
|
||||
async def send_content(self, content: StoredContent, extra_buttons=None, message_id=None):
|
||||
assert content.type.startswith('onchain/content'), "Invalid nodeStorage content type"
|
||||
inline_keyboard_array = []
|
||||
cd_log = f"Content (SHA256: {content.hash}), Encrypted: {content.encrypted}, TelegramCID: {content.telegram_cid}. "
|
||||
if not content.encrypted:
|
||||
local_content = content
|
||||
else:
|
||||
local_content = content.decrypted_content
|
||||
# TODO: add check decrypted_content by .format_json()['content_cid']
|
||||
if local_content:
|
||||
cd_log += f"Decrypted: {local_content.hash}. "
|
||||
else:
|
||||
cd_log += "Can't decrypt content. "
|
||||
|
||||
if local_content:
|
||||
content_meta = content.json_format()
|
||||
local_content_meta = local_content.json_format()
|
||||
try:
|
||||
content_type, content_encoding = local_content_meta["content_type"].split('/')
|
||||
except:
|
||||
content_type, content_encoding = 'application', 'x-binary'
|
||||
|
||||
try:
|
||||
cover_content = StoredContent.from_cid(self.db_session, content_meta.get('cover_cid') or None)
|
||||
except BaseException as e:
|
||||
cd_log += f"Can't get cover content: {e}. "
|
||||
cover_content = None
|
||||
|
||||
local_content_cid = local_content.cid
|
||||
local_content_cid.content_type = 'audio/mpeg'
|
||||
local_content_url = f"{PROJECT_HOST}/api/v1/storage/{local_content_cid.serialize_v2(include_accept_type=True)}"
|
||||
|
||||
template_kwargs = {}
|
||||
if content_type[0] == 'audio':
|
||||
template_kwargs['title'] = 'title'
|
||||
template_kwargs['performer'] = 'performer'
|
||||
template_kwargs['protect_content'] = True
|
||||
template_kwargs['audio'] = local_content_url
|
||||
if cover_content:
|
||||
template_kwargs['thumbnail'] = cover_content.web_url
|
||||
|
||||
|
||||
else:
|
||||
local_content = None
|
||||
|
||||
if not local_content:
|
||||
text = self.user.translated('p_playerContext_unsupportedContent').format(
|
||||
content_type=content_type,
|
||||
content_encoding=content_encoding
|
||||
)
|
||||
inline_keyboard_array = []
|
||||
extra_buttons = []
|
||||
|
||||
make_log("TG-Player", f"Send content {content_type} ({content_encoding}) to chat {self._chat_id}. {cd_log}")
|
||||
return await tg_process_template(
|
||||
self, text, message_id=message_id, **template_kwargs,
|
||||
keyboard=get_inline_keyboard([*inline_keyboard_array, *extra_buttons]) if inline_keyboard_array else None,
|
||||
message_type=f'content/{content_type}', message_meta={'content_sha256': content_meta['hash']}
|
||||
)
|
||||
|
||||
|
||||
@@ -3,10 +3,16 @@ from datetime import datetime, timedelta
|
||||
|
||||
from app.core.logger import make_log
|
||||
from app.core.models.messages import KnownTelegramMessage
|
||||
from app.core._config import TELEGRAM_API_KEY, CLIENT_TELEGRAM_API_KEY
|
||||
|
||||
from app.core.models._telegram.templates.player import PlayerTemplates
|
||||
|
||||
|
||||
class Wrapped_CBotChat:
|
||||
def __init__(self, api_key: str, chat_id: int = None, db_session=None, **kwargs):
|
||||
class T: pass
|
||||
|
||||
|
||||
class Wrapped_CBotChat(T, PlayerTemplates):
|
||||
def __init__(self, api_key: str, chat_id: int = None, db_session=None, user=None, **kwargs):
|
||||
if isinstance(api_key, Bot):
|
||||
self._bot_key = api_key.token
|
||||
self._bot = api_key
|
||||
@@ -18,6 +24,7 @@ class Wrapped_CBotChat:
|
||||
|
||||
self._chat_id = chat_id
|
||||
self.db_session = db_session
|
||||
self.user = user
|
||||
self.options = kwargs
|
||||
|
||||
def __repr__(self):
|
||||
@@ -26,13 +33,21 @@ class Wrapped_CBotChat:
|
||||
|
||||
return "Bot"
|
||||
|
||||
async def return_result(self, result, message_type='common'):
|
||||
@property
|
||||
def bot_id(self):
|
||||
return {
|
||||
TELEGRAM_API_KEY: 0,
|
||||
CLIENT_TELEGRAM_API_KEY: 1
|
||||
}[self._bot_key]
|
||||
|
||||
async def return_result(self, result, message_type='common', message_meta={}):
|
||||
if self.db_session:
|
||||
if message_type == 'common':
|
||||
ci = 0
|
||||
for oc_msg in self.db_session.query(KnownTelegramMessage).filter(
|
||||
KnownTelegramMessage.type == 'common',
|
||||
KnownTelegramMessage.chat_id == self._chat_id,
|
||||
KnownTelegramMessage.deleted == False
|
||||
).all():
|
||||
await self.delete_message(oc_msg.message_id)
|
||||
ci += 1
|
||||
@@ -45,9 +60,12 @@ class Wrapped_CBotChat:
|
||||
self.db_session.add(
|
||||
KnownTelegramMessage(
|
||||
type=message_type,
|
||||
bot_id=self.bot_id,
|
||||
chat_id=self._chat_id,
|
||||
message_id=message_id,
|
||||
created=datetime.now()
|
||||
from_user=False,
|
||||
created=datetime.now(),
|
||||
meta=message_meta or {}
|
||||
)
|
||||
)
|
||||
self.db_session.commit()
|
||||
@@ -56,7 +74,7 @@ class Wrapped_CBotChat:
|
||||
|
||||
return result
|
||||
|
||||
async def send_message(self, text: str, message_type='common', **kwargs):
|
||||
async def send_message(self, text: str, message_type='common', message_meta={}, **kwargs):
|
||||
assert self._chat_id, "No chat_id"
|
||||
try:
|
||||
make_log(self, f"Send message to {self._chat_id}. Text len: {len(text)}", level='debug')
|
||||
@@ -67,7 +85,7 @@ class Wrapped_CBotChat:
|
||||
disable_web_page_preview=True,
|
||||
**kwargs
|
||||
)
|
||||
return await self.return_result(r, message_type=message_type)
|
||||
return await self.return_result(r, message_type=message_type, message_meta=message_meta)
|
||||
except BaseException as e:
|
||||
make_log(self, f"Error sending message to {self._chat_id}. Error: {e}", level='warning')
|
||||
return None
|
||||
@@ -104,7 +122,7 @@ class Wrapped_CBotChat:
|
||||
make_log(self, f"Error deleting message {self._chat_id}/{message_id}. Error: {e}", level='warning')
|
||||
return None
|
||||
|
||||
async def send_photo(self, file_id, message_type='common', **kwargs):
|
||||
async def send_photo(self, file_id, message_type='common', message_meta={}, **kwargs):
|
||||
assert self._chat_id, "No chat_id"
|
||||
try:
|
||||
make_log(self, f"Send photo to {self._chat_id}. File: {file_id}", level='debug')
|
||||
@@ -113,12 +131,12 @@ class Wrapped_CBotChat:
|
||||
file_id,
|
||||
**kwargs
|
||||
)
|
||||
return await self.return_result(r, message_type=message_type)
|
||||
return await self.return_result(r, message_type=message_type, message_meta=message_meta)
|
||||
except Exception as e:
|
||||
make_log(self, f"Error sending photo to {self._chat_id}. Error: {e}", level='warning')
|
||||
return None
|
||||
|
||||
async def send_document(self, file_id, message_type='common', **kwargs):
|
||||
async def send_document(self, file_id, message_type='common', message_meta={}, **kwargs):
|
||||
assert self._chat_id, "No chat_id"
|
||||
try:
|
||||
make_log(self, f"Send document to {self._chat_id}. File: {file_id}", level='debug')
|
||||
@@ -127,12 +145,12 @@ class Wrapped_CBotChat:
|
||||
file_id,
|
||||
**kwargs
|
||||
)
|
||||
return await self.return_result(r, message_type=message_type)
|
||||
return await self.return_result(r, message_type=message_type, message_meta=message_meta)
|
||||
except Exception as e:
|
||||
make_log(self, f"Error sending document to {self._chat_id}. Error: {e}", level='warning')
|
||||
return None
|
||||
|
||||
async def send_video(self, file_id, message_type='common', **kwargs):
|
||||
async def send_video(self, file_id, message_type='common', message_meta={}, **kwargs):
|
||||
assert self._chat_id, "No chat_id"
|
||||
try:
|
||||
make_log(self, f"Send video to {self._chat_id}. File: {file_id}", level='debug')
|
||||
@@ -141,12 +159,26 @@ class Wrapped_CBotChat:
|
||||
file_id,
|
||||
**kwargs
|
||||
)
|
||||
return await self.return_result(r, message_type=message_type)
|
||||
return await self.return_result(r, message_type=message_type, message_meta=message_meta)
|
||||
except Exception as e:
|
||||
make_log(self, f"Error sending video to {self._chat_id}. Error: {e}", level='warning')
|
||||
return None
|
||||
|
||||
async def copy_message(self, from_chat_id, message_id, message_type='common', **kwargs):
|
||||
async def send_audio(self, file_id, message_type='common', message_meta={}, **kwargs):
|
||||
assert self._chat_id, "No chat_id"
|
||||
try:
|
||||
make_log(self, f"Send audio to {self._chat_id}. File: {file_id}", level='debug')
|
||||
r = await self._bot.send_audio(
|
||||
self._chat_id,
|
||||
file_id,
|
||||
**kwargs
|
||||
)
|
||||
return await self.return_result(r, message_type=message_type, message_meta=message_meta)
|
||||
except Exception as e:
|
||||
make_log(self, f"Error sending audio to {self._chat_id}. Error: {e}", level='warning')
|
||||
return None
|
||||
|
||||
async def copy_message(self, from_chat_id, message_id, message_type='common', message_meta={}, **kwargs):
|
||||
assert self._chat_id, "No chat_id"
|
||||
try:
|
||||
make_log(self, f"Copy message from {from_chat_id}/{message_id} to {self._chat_id}", level='debug')
|
||||
@@ -156,12 +188,12 @@ class Wrapped_CBotChat:
|
||||
message_id,
|
||||
**kwargs
|
||||
)
|
||||
return await self.return_result(r, message_type=message_type)
|
||||
return await self.return_result(r, message_type=message_type, message_meta=message_meta)
|
||||
except Exception as e:
|
||||
make_log(self, f"Error copying message from {from_chat_id}/{message_id} to {self._chat_id}. Error: {e}", level='warning')
|
||||
return None
|
||||
|
||||
async def forward_message(self, from_chat_id, message_id, message_type='common', **kwargs):
|
||||
async def forward_message(self, from_chat_id, message_id, message_type='common', message_meta={}, **kwargs):
|
||||
assert self._chat_id, "No chat_id"
|
||||
try:
|
||||
make_log(self, f"Forward message from {from_chat_id}/{message_id} to {self._chat_id}", level='debug')
|
||||
@@ -171,7 +203,7 @@ class Wrapped_CBotChat:
|
||||
message_id,
|
||||
**kwargs
|
||||
)
|
||||
return await self.return_result(r, message_type=message_type)
|
||||
return await self.return_result(r, message_type=message_type, message_meta=message_meta)
|
||||
except Exception as e:
|
||||
make_log(self, f"Error forwarding message from {from_chat_id}/{message_id} to {self._chat_id}. Error: {e}", level='warning')
|
||||
return None
|
||||
Reference in new issue
Block a user