mirror of
https://github.com/MrSedan/neuro-reply-bot-reworked.git
synced 2026-01-14 21:49:42 +03:00
Added message entities to posts and info
This commit is contained in:
@@ -1,4 +1,8 @@
|
||||
import json
|
||||
from typing import List, Optional
|
||||
|
||||
import requests
|
||||
from aiogram.types import MessageEntity
|
||||
from aiohttp import ClientSession
|
||||
|
||||
import neuroapi.types as neuroTypes
|
||||
@@ -9,10 +13,18 @@ from .enums import EGetAll
|
||||
|
||||
class Post(ApiMethod):
|
||||
|
||||
async def new(self, text: str, from_user_id: str, media_group_id: str = "None"):
|
||||
async def new(self, text: str, from_user_id: str, media_group_id: str = "None", message_entities: Optional[List[MessageEntity]] = None):
|
||||
payload = {'text': text, 'from_user_id': from_user_id}
|
||||
if media_group_id != 'None':
|
||||
payload['media_group_id'] = media_group_id
|
||||
if message_entities is not None:
|
||||
mes_ent = list(map(lambda x: x.model_dump(), message_entities))
|
||||
arr =[]
|
||||
for item in mes_ent:
|
||||
if item['type'] == 'bot_command': continue
|
||||
item['offset'] -= 9
|
||||
arr.append(item)
|
||||
payload['message_entities'] = json.dumps(arr)
|
||||
response = requests.post(self.api_url+'/post/new', data=payload)
|
||||
data = response.json()
|
||||
if 'statusCode' in data:
|
||||
|
||||
@@ -1,9 +1,18 @@
|
||||
import json
|
||||
from dataclasses import dataclass
|
||||
from uuid import UUID
|
||||
from datetime import datetime
|
||||
from typing import Any, List, Optional
|
||||
from ._image import Image
|
||||
from uuid import UUID
|
||||
|
||||
from aiogram.types import MessageEntity
|
||||
|
||||
from ._helpers import *
|
||||
from ._image import Image
|
||||
|
||||
|
||||
def to_message_dict_class(x: Any) -> dict:
|
||||
assert isinstance(x, MessageEntity)
|
||||
return cast(MessageEntity, x).model_dump()
|
||||
|
||||
|
||||
@dataclass
|
||||
@@ -15,6 +24,7 @@ class Post:
|
||||
timestamp: datetime
|
||||
from_user_id: int
|
||||
images: Optional[List[Image]] = None
|
||||
message_entities: Optional[List[MessageEntity]] = None
|
||||
|
||||
@staticmethod
|
||||
def from_dict(obj: Any) -> 'Post':
|
||||
@@ -28,7 +38,9 @@ class Post:
|
||||
from_user_id = int(from_str(obj.get("from_user_id")))
|
||||
images = from_union([lambda x: from_list(
|
||||
Image.from_dict, x), from_none], obj.get("images"))
|
||||
return Post(uuid, posted, text, media_group_id, timestamp, from_user_id, images)
|
||||
mes_ent = json.loads(obj.get('message_entities','[]'))
|
||||
message_entities = from_list(MessageEntity.model_validate, mes_ent)
|
||||
return Post(uuid, posted, text, media_group_id, timestamp, from_user_id, images, message_entities)
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
result: dict = {}
|
||||
@@ -41,4 +53,7 @@ class Post:
|
||||
if self.images is not None:
|
||||
result["images"] = from_union([lambda x: from_list(
|
||||
lambda x: to_class(Image, x), x), from_none], self.images)
|
||||
if self.message_entities is not None:
|
||||
result['message_entities'] = from_union([lambda x: from_list(
|
||||
lambda x: to_message_dict_class(x), x), from_none], self.message_entities)
|
||||
return result
|
||||
|
||||
Reference in New Issue
Block a user