WebhookClient

The WebhookClient is the entrypoint into the SDK. Here we can setup handlers that will respond to various events from the bot.

A WebhookClient is instantiated using the JWT Secret found in logicdialog. This is used to call the handler function.

export const whClient: WebhookClient = new WebhookClient('secret');

There are two main methods on the Webhook client - handleRequest and addHandler

handleRequest

The handleRequest function is called from within a code that has been setup to handle HTTP requests made to your application. This might be part of an express route, or some other process. The function is passed two arguments. The first of these arguments is the body of the HTTP request which has originated from logicdialog. The second argument provides the authorization token used on the incoming request. This token is used to verify the payload that has been sent has not been modified in transit.

Examples of these function calls are provided below.

// This should match the endpoint set in IntelAgent
app.post('/', async (req: Request, res: Response) => {
  // handleRequest returns a JSON object and a statusCode
  const { status, body } = await whClient.handleRequest(
    req.body,
    getToken(req)
  );
  res.status(status).send(body);
});
function getToken(request: Request): string {
  const token = request.headers.authorization?.split('Bearer ')[1];
  if (!token) {
    throw Error('No token found');
  }
  return token;
}

addHandler

The addHandler function allows you to specify a function that can be run to process the data coming in from logicdialog. Much like event subscriptions, this handler is associated with a specific name and as such you can add multiple handlers to a webhook client so that they can handle different types of events from your bot.

The handler that you create is an asynchronous function that accepts two parameters - a WebhookRequest object and a link to a ResponseBuilder.

A new WebhookHandler is initiated as follows :

export const divisionHandler: WebhookHandler = async (
  webhookReq: WebhookRequest,
  responseBuilder: ResponseBuilder
  ): Promise<void> => {
    // Logic goes here
}

Once you have created a handler, you can add it to your webhook client using the addHandler method.

whClient.addHandler('division-handler', divisionHandler);

In the example above, only forms in logicdialog that have a handler specified as division-handler will trigger the function.

Inside the function you can find the parameters that have been set in the conversation within the webhookReq object. Each form will differ depending on the number and type of questions that are asked to the user however the following example shows how you might access two values of a form.

const {value: baseNumber} = webhookReq.formValue.baseNumber;
const {value: divisionNumber} = webhookReq.formValue.divisionNumber;

The formValues in the example are structured as the following interface:

{
  baseNumber: {
    title: string,
    value: string
  },
  divisionNumber: {
    title: string,
    value: string
  }
}

For more information about constructing a reply to the user, please see the ResponseBuilder section.

ResponseBuilder

Last updated