Filtering and Transforming Messages with Metadata

If you're using the Web Messenger, you can attach metadata to messages and access this data in your Cloud Functions or on the website hosting the web messenger.

Metadata can be used to extend the functionality of your chatbot by providing a two-way communication method to transport and manipulate data that cannot easily be captured through other methods.

For example, you might want to add the URL of the current webpage as metadata to messages sent by the user which could then be used for conditional logic in a Cloud Function. Or you might attach a redirect URL as metadata to messages sent by a Cloud Function, which can be used by the hosting website to automatically redirect the user when it receives these messages from your chatbot.

In the example below, the currentUrl attribute has been added to the metadata.

const delegate = {
    beforeSend(message) {
        if (message.role === 'appUser') {
            message.metadata = {
                ...message.metadata,
                currentUrl: window.location.href
            };
        }

        return message;
    }
};

Stackchat.init({
    appId: <app-id>,
    delegate
})

This action can also be done accompanying a postback as seen below.

const delegate = {
    beforePostbackSend(postback) {
        postback.metadata = {
            ...postback.metadata,
            currentUrl: window.location.href
        };

        return postback;
    }
};

Stackchat.init({
    appId: <app-id>,
    delegate
})