File size: 1,190 Bytes
f169c98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# message_parser.py

from app.models.message_types import Message, MediaContent, MediaType

class MessageParser:
    @staticmethod
    def parse(raw_message: dict) -> Message:
        media = {}
        
        if image := raw_message.get("image"):
            media[MediaType.IMAGE] = MediaContent(
                id=image["id"],
                file_path=f"{image['id']}.jpg"
            )
            
        if document := raw_message.get("document"):
            media[MediaType.DOCUMENT] = MediaContent(
                id=document["id"],
                file_path=document.get("filename"),
                filename=document.get("filename")
            )
            
        if video := raw_message.get("video"):
            mime_type = video.get("mime_type")
            media[MediaType.VIDEO] = MediaContent(
                id=video["id"],
                file_path=f"{video['id']}.{mime_type.split('/')[-1]}",
                mime_type=mime_type
            )

        return Message(
            id=raw_message["id"],
            sender_id=raw_message["from"],
            content=raw_message.get("text", {}).get("body"),
            media=media if media else None
        )