Skip to main content

Customize Payload

onBeforeSendMessage lets you customize the payload before a message is sent — attach the current page, user state, feature flags, or any other context metadata so the bot can respond based on the full picture, not just the user's text.

Select Context

Choose a category to inject into every outgoing message.

Send via ref

Payload Logs

Send a message to see the injected payload.
Loading chatbot...

Code Example

const [category, setCategory] = useState<Category | null>(null);

<Chatbot
onBeforeSendMessage={(params) => ({
...params,
payload: {
categoryId: category?.id,
categoryName: category?.name,
injectedAt: new Date().toISOString(),
},
})}
{...rest}
/>;

SendMessageParams

The shape received and returned by onBeforeSendMessage:

FieldTypeDescription
textstringThe user's input text
payloadRecord<string, unknown> | (() => Record<string, unknown>)Metadata sent alongside the message to the bot provider
blobIdsstring[]Uploaded file blob IDs (optional)
filePreviewUrlsstring[]Image preview URLs (optional)
documentNamesstring[]Document names (optional)

The most common use is injecting payload. Other fields (e.g. prefixing text) can also be modified — the returned object replaces the original params.

Further Reading

For how payload is received and used by the bot provider, see the Asgard developer docs: