Filtering and Transforming Messages with Metadata

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

A common example where sending metadata can provide information that you would not otherwise have is to send the current URL at which the user is currently browsing.

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
})

Alternatively, you can send a redirect request in the metadata to move the user to a new page based on their bot interaction.