feat: sending and receiving messages with files, editing text messages
This commit is contained in:
21
external/chat/chat_client.py
vendored
21
external/chat/chat_client.py
vendored
@@ -1,5 +1,6 @@
|
||||
import aiohttp
|
||||
import jwt
|
||||
from fastapi import UploadFile
|
||||
|
||||
from backend.config import CHATS_SYNC_URL, CHAT_CONNECTOR_API_KEY
|
||||
from external.chat.schemas import *
|
||||
@@ -22,7 +23,6 @@ class ChatClient:
|
||||
async def _method(self, http_method, method, **kwargs):
|
||||
async with aiohttp.ClientSession(headers=self.headers) as session:
|
||||
async with session.request(http_method, self.base_url + method, **kwargs) as response:
|
||||
print(response)
|
||||
return await response.json()
|
||||
|
||||
async def create_group(self, request: ExternalCreateGroupRequest) -> ExternalCreateGroupResponse:
|
||||
@@ -34,3 +34,22 @@ class ChatClient:
|
||||
json_data = request.model_dump()
|
||||
response = await self._method('POST', self.groups_endpoint + '/topic/create', json=json_data)
|
||||
return ExternalCreateTopicResponse.model_validate(response)
|
||||
|
||||
async def send_messages_with_files(
|
||||
self,
|
||||
tg_group_id: str,
|
||||
tg_topic_id: int,
|
||||
caption: str,
|
||||
files: list[UploadFile],
|
||||
) -> ExternalSendMessagesWithFilesResponse:
|
||||
query_params = f'?tg_group_id={tg_group_id}&tg_topic_id={tg_topic_id}&caption={caption}'
|
||||
|
||||
data = aiohttp.FormData(default_to_multipart=True)
|
||||
|
||||
for file in files:
|
||||
content = await file.read()
|
||||
data.add_field('files', content, filename=file.filename, content_type=file.content_type)
|
||||
|
||||
response = await self._method('POST', self.chats_sync_endpoint + '/send' + query_params, data=data)
|
||||
|
||||
return ExternalSendMessagesWithFilesResponse.model_validate(response)
|
||||
|
||||
22
external/chat/schemas.py
vendored
22
external/chat/schemas.py
vendored
@@ -1,9 +1,25 @@
|
||||
from typing import Optional
|
||||
from uuid import UUID
|
||||
|
||||
from schemas.base import BaseSchema
|
||||
from schemas.base import BaseSchema, OkMessageSchema
|
||||
|
||||
|
||||
# region Entities
|
||||
|
||||
class ExternalSendFileSchema(BaseSchema):
|
||||
buffer: bytes
|
||||
file_name: str
|
||||
file_size: int
|
||||
|
||||
|
||||
class ExternalMessageFileSchema(BaseSchema):
|
||||
file_path: str
|
||||
type: str
|
||||
file_name: str
|
||||
file_size: int
|
||||
|
||||
# endregion
|
||||
|
||||
# region Requests
|
||||
|
||||
class ExternalCreateGroupRequest(BaseSchema):
|
||||
@@ -29,4 +45,8 @@ class ExternalCreateGroupResponse(BaseSchema):
|
||||
class ExternalCreateTopicResponse(BaseSchema):
|
||||
tg_topic_id: int
|
||||
|
||||
|
||||
class ExternalSendMessagesWithFilesResponse(OkMessageSchema):
|
||||
files: list[ExternalMessageFileSchema]
|
||||
|
||||
# endregion
|
||||
|
||||
Reference in New Issue
Block a user