Перейти к содержанию

Types, Models and Exceptions

pullkin.models.Message

You can create a child model that describes the fields you use in the notification.

Example
class MyMessage(Message):
    my_field: str

class MyMessageWithGeneric(Message[MyNotificationData]):

to_another_model(another_model)

You can use this method to convert a Message-model to your model. A Message will be converted into the another_model using another_model.model_validate(self.raw)

If you use a handler with type hinting, the message model will be converted automatically.

Example: Manually convert
message: Message = ...

my_message: Message[MyNotificationData] = message.to_another_model(Message[MyNotificationData])
my_message: MyMessage[MyNotificationData] = message.to_another_model(MyMessage[MyNotificationData])
my_message: MyMessage[NotificationData] = message.to_another_model(MyMessage[NotificationData])
my_message: MyMessage = message.to_another_model(MyMessage)

# or use simple
my_message: MyMessage[MyNotificationData] = MyMessage[MyNotificationData].model_validate(message.raw)
Example: Auto convert with handler-style
@pullkin.on_notification()
async def on_notification(notification: MyMessage[MyNotificationData], data_message: DataMessageStanza):
    print(notification)