Custom Components

This section describes how you can pass in your own message types to be rendered in the WebChat application.

WebChat will allow you to pass in your own components to render as message types in the message list. This means that you could, for example, write your own Location message using a maps plugin of your choice and use it to override the default OpenDialog Location message, which requires a Google Maps API key.

Or you could use custom components to render message types outside of the current types supplied by OpenDialog. However, you will also need to set up a backend data source that provides the correct message data for WebChat to render in your components.

For more info about the current message types, response types and their data structure, explore our ChatAPI documentation.

How to create & use your custom components

Any component passed into the WebChat application must be written as a Vue 3 single file component. How you create and import your components into your application beyond that is entirely up to you.

Every message-type component that you create can receive the following properties, and emit the following events:

<your-message-component
   :msg="" // Message object - the current message object. See the API docs for more details
   :theme="" // Theme object - taken from the theme/colours that you passed into your settings or set in the interface settings section of OpenDialog
   :latest="" // boolean - derived dynamically from the current message history. Indicates whether the message is the latest one in the list
   :errorTemplates="" // object - uses settings.messages from the config endpoint
   :language="" // string - the currently selected language code
   @submit="(data: any) => onComponentSubmit(data.type, data.data)"
   @cancel="onCancel(message)"
/>

A working example

This is an example of a very basic message component that renders some text and emits a submit event when the button is clicked:

Now that we have a component, we need to provide WebChat with a name for it and some optional other values. At it's simplest, this will look as follows:

The name property must be the same as the message type property in the data. This is how WebChat maps message components to the data received from the API.

Finally, we can pass in our custom components when we initialise our WebChat widget:

When we put all these elements together, your Javascript code will look like the following:

At this point, when WebChat receives a message from ChatApi with a type of location , it will render the custom component rather than the default one provided by OpenDialog.

Last updated