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