Message is built by Pydantic now
This commit is contained in:
@@ -4,17 +4,10 @@ from .. import ContextVars, ContextVarsPopulator
|
||||
from . import *
|
||||
|
||||
@ContextVarsPopulator.autobuilder
|
||||
def _build_message(context: EventContext, vars: ContextVars) -> None:
|
||||
source = context.event.source
|
||||
if "content" not in source:
|
||||
def _build_message(ctx: EventContext, vars: ContextVars) -> None:
|
||||
"""This builder creates a CTX_MESSAGE variable for any message."""
|
||||
source = ctx.event.source
|
||||
if source["type"] != "m.room.message":
|
||||
return
|
||||
content: dict = source["content"]
|
||||
if "body" not in content or "msgtype" not in content:
|
||||
return
|
||||
res = Message(
|
||||
body=content.get("body", "Missing `body`"),
|
||||
format=content.get("format"),
|
||||
formatted_body=content.get("formatted_body"),
|
||||
msgtype=content.get("msgtype", "Missing `msgtype`")
|
||||
)
|
||||
vars[CTX_MESSAGE] = res
|
||||
vars[CTX_MESSAGE] = Message.model_validate(content)
|
||||
@@ -1,8 +1,10 @@
|
||||
from typing import Literal
|
||||
from pydantic import BaseModel
|
||||
|
||||
class Message(BaseModel):
|
||||
"""This model defines the most important fields of the message. Custom
|
||||
fields must be accessed using raw matrix-nio event."""
|
||||
"""This model describes message of any kind and is being used as base
|
||||
model for other types of message. Unknown message types are interpreted
|
||||
as this model only."""
|
||||
|
||||
msgtype: str
|
||||
"""It identifies the type of message being sent"""
|
||||
@@ -10,8 +12,8 @@ class Message(BaseModel):
|
||||
body: str
|
||||
"""Unformatted (or fallback) body of the message"""
|
||||
|
||||
format: str | None
|
||||
format: str | None = None
|
||||
"""Format of the formatted_body content"""
|
||||
|
||||
formatted_body: str | None
|
||||
formatted_body: str | None = None
|
||||
"""Formatted body of the message, present if `format` is not None"""
|
||||
Reference in New Issue
Block a user