56 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			56 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
import aiohttp
 | 
						|
import jwt
 | 
						|
from fastapi import UploadFile
 | 
						|
 | 
						|
from backend.config import CHATS_SYNC_URL, CHAT_CONNECTOR_API_KEY
 | 
						|
from external.chat.schemas import *
 | 
						|
from services.auth import algorithm
 | 
						|
 | 
						|
 | 
						|
class ChatClient:
 | 
						|
    def __init__(self, api_key: str):
 | 
						|
        self.api_key = api_key
 | 
						|
        self.headers = {
 | 
						|
            'Authorization': 'Bearer ' + self.create_jwt_token()
 | 
						|
        }
 | 
						|
        self.base_url = CHATS_SYNC_URL
 | 
						|
        self.chats_sync_endpoint = '/chats-sync'
 | 
						|
        self.groups_endpoint = '/group'
 | 
						|
 | 
						|
    def create_jwt_token(self):
 | 
						|
        return jwt.encode({'sub': self.api_key}, CHAT_CONNECTOR_API_KEY, algorithm=algorithm)
 | 
						|
 | 
						|
    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:
 | 
						|
                return await response.json()
 | 
						|
 | 
						|
    async def create_group(self, request: ExternalCreateGroupRequest) -> ExternalCreateGroupResponse:
 | 
						|
        json_data = request.model_dump()
 | 
						|
        response = await self._method('POST', self.groups_endpoint + '/create', json=json_data)
 | 
						|
        return ExternalCreateGroupResponse.model_validate(response)
 | 
						|
 | 
						|
    async def create_topic(self, request: ExternalCreateTopicRequest) -> ExternalCreateTopicResponse:
 | 
						|
        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)
 |