Subscribing to a messaging channel
Chatlayer uses webhooks to allow external services to send messages to the platform.
In the app integrations world, this is performed by two functions: performSubscribe
and performUnsubscribe
.
The main goal of these two functions is to register and deregister the webhook which Chatlayer provides to the external service.
This webhook URL is available inside these functions.
Perform subscribe
performSubscribe
is responsible for registering a webhook to the external service. App integrations provide a value
inside bundle.targetUrl
with the correct URL.
The following sample demonstrates what that could look like:
module.exports = async (sdk, bundle) => {
const body = {
callback_url: bundle.targetUrl,
bot_id: botId,
bot_version: botVersion,
};
await sdk
.request({
url: 'https://myservice.com/webhooks',
method: "POST",
body,
})
.catch((err) => {
throw new Error(`failed to register webhook: ${err.message}`);
});
};
Perform unsubscribe
performUnsubscribe
is responsible for deregistering the previously registered webhook from the external service.
The following sample demonstrates what that could look like:
module.exports = async (sdk, bundle) => {
const body = {
bot_id: botId,
bot_version: botVersion,
};
await sdk
.request({
url: 'https://myservice.com/webhooks',
method: "DELETE",
body,
})
.catch((err) => {
throw new Error(`failed to unregister webhook: ${err.message}`);
});
};