dev@locazia: implement message_types and clean trash
This commit is contained in:
1 parent
01d56dae42
commit
dae4a6f123
10 files changed
+118
-31
No files matched your search
@@ -6,3 +6,4 @@ from app.core.models.node_storage import StoredContent
|
||||
from app.core.models.transaction import UserBalance, InternalTransaction
|
||||
from app.core.models.user import User
|
||||
from app.core.models.wallet_connection import WalletConnection
|
||||
from app.core.models.messages import KnownTelegramMessage
|
||||
@@ -1,10 +1,12 @@
|
||||
from aiogram import Bot
|
||||
from aiogram import Bot, types
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from app.core.logger import make_log
|
||||
from app.core.models.messages import KnownTelegramMessage
|
||||
|
||||
|
||||
class Wrapped_CBotChat:
|
||||
def __init__(self, api_key: str, chat_id: int = None, **kwargs):
|
||||
def __init__(self, api_key: str, chat_id: int = None, db_session=None, **kwargs):
|
||||
if isinstance(api_key, Bot):
|
||||
self._bot_key = api_key.token
|
||||
self._bot = api_key
|
||||
@@ -15,6 +17,7 @@ class Wrapped_CBotChat:
|
||||
raise TypeError(f'api_key must be Bot or str, not {type(api_key)}')
|
||||
|
||||
self._chat_id = chat_id
|
||||
self.db_session = db_session
|
||||
self.options = kwargs
|
||||
|
||||
def __repr__(self):
|
||||
@@ -23,16 +26,46 @@ class Wrapped_CBotChat:
|
||||
|
||||
return "Bot"
|
||||
|
||||
async def send_message(self, text: str, **kwargs):
|
||||
async def return_result(self, result, message_type='common'):
|
||||
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,
|
||||
).all():
|
||||
await self.delete_message(oc_msg.message_id)
|
||||
ci += 1
|
||||
|
||||
make_log(self, f"Deleted {ci} old messages", level='debug')
|
||||
|
||||
if isinstance(result, types.Message):
|
||||
message_id = getattr(result, 'message_id', None)
|
||||
assert message_id, "No message_id"
|
||||
self.db_session.add(
|
||||
KnownTelegramMessage(
|
||||
type=message_type,
|
||||
chat_id=self._chat_id,
|
||||
message_id=message_id,
|
||||
created=datetime.now()
|
||||
)
|
||||
)
|
||||
self.db_session.commit()
|
||||
else:
|
||||
make_log(self, f"Unknown result type: {type(result)}", level='warning')
|
||||
|
||||
return result
|
||||
|
||||
async def send_message(self, text: str, message_type='common', **kwargs):
|
||||
try:
|
||||
make_log(self, f"Send message to {self._chat_id}. Text len: {len(text)}", level='debug')
|
||||
return await self._bot.send_message(
|
||||
r = await self._bot.send_message(
|
||||
self._chat_id,
|
||||
text,
|
||||
parse_mode='html',
|
||||
disable_web_page_preview=True,
|
||||
**kwargs
|
||||
)
|
||||
return await self.return_result(r, message_type=message_type)
|
||||
except BaseException as e:
|
||||
make_log(self, f"Error sending message to {self._chat_id}. Error: {e}", level='warning')
|
||||
return None
|
||||
@@ -48,6 +81,7 @@ class Wrapped_CBotChat:
|
||||
disable_web_page_preview=True,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
make_log(self, f"Error editing message {self._chat_id}/{message_id}. Error: {e}", level='warning')
|
||||
if 'exactly the same as a current content' in f'{e}':
|
||||
@@ -66,64 +100,69 @@ 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, **kwargs):
|
||||
async def send_photo(self, file_id, message_type='common', **kwargs):
|
||||
try:
|
||||
make_log(self, f"Send photo to {self._chat_id}. File: {file_id}", level='debug')
|
||||
return await self._bot.send_photo(
|
||||
r = await self._bot.send_photo(
|
||||
self._chat_id,
|
||||
file_id,
|
||||
**kwargs
|
||||
)
|
||||
return await self.return_result(r, message_type=message_type)
|
||||
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, **kwargs):
|
||||
async def send_document(self, file_id, message_type='common', **kwargs):
|
||||
try:
|
||||
make_log(self, f"Send document to {self._chat_id}. File: {file_id}", level='debug')
|
||||
return await self._bot.send_document(
|
||||
r = await self._bot.send_document(
|
||||
self._chat_id,
|
||||
file_id,
|
||||
**kwargs
|
||||
)
|
||||
return await self.return_result(r, message_type=message_type)
|
||||
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, **kwargs):
|
||||
async def send_video(self, file_id, message_type='common', **kwargs):
|
||||
try:
|
||||
make_log(self, f"Send video to {self._chat_id}. File: {file_id}", level='debug')
|
||||
return await self._bot.send_video(
|
||||
r = await self._bot.send_video(
|
||||
self._chat_id,
|
||||
file_id,
|
||||
**kwargs
|
||||
)
|
||||
return await self.return_result(r, message_type=message_type)
|
||||
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, **kwargs):
|
||||
async def copy_message(self, from_chat_id, message_id, message_type='common', **kwargs):
|
||||
try:
|
||||
make_log(self, f"Copy message from {from_chat_id}/{message_id} to {self._chat_id}", level='debug')
|
||||
return await self._bot.copy_message(
|
||||
r = await self._bot.copy_message(
|
||||
self._chat_id,
|
||||
from_chat_id,
|
||||
message_id,
|
||||
**kwargs
|
||||
)
|
||||
return await self.return_result(r, message_type=message_type)
|
||||
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, **kwargs):
|
||||
async def forward_message(self, from_chat_id, message_id, message_type='common', **kwargs):
|
||||
try:
|
||||
make_log(self, f"Forward message from {from_chat_id}/{message_id} to {self._chat_id}", level='debug')
|
||||
return await self._bot.forward_message(
|
||||
r = await self._bot.forward_message(
|
||||
self._chat_id,
|
||||
from_chat_id,
|
||||
message_id,
|
||||
**kwargs
|
||||
)
|
||||
return await self.return_result(r, message_type=message_type)
|
||||
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
|
||||
@@ -0,0 +1,17 @@
|
||||
from base58 import b58decode
|
||||
from sqlalchemy import Column, Integer, String, DateTime, JSON, BigInteger
|
||||
|
||||
from .base import AlchemyBase
|
||||
|
||||
|
||||
# Only for clean old messages now
|
||||
|
||||
|
||||
class KnownTelegramMessage(AlchemyBase):
|
||||
__tablename__ = 'known_telegram_messages'
|
||||
|
||||
id = Column(Integer, autoincrement=True, primary_key=True)
|
||||
type = Column(String(64), nullable=True)
|
||||
chat_id = Column(BigInteger, nullable=False)
|
||||
message_id = Column(BigInteger, nullable=False)
|
||||
created = Column(DateTime, nullable=False, default=0)
|
||||
Reference in new issue
Block a user