WebhookClient

Middleware~ WebhookClient

Webhook class for custom messaging implementations.

Constructor

new WebhookClient(optionsopt)

Source:
See:
Example

Simple client for sending and receivinng messages.

const OracleBot = require('@oracle/bots-node-sdk');
const express = require('express');
const app = express();
OracleBot.init(app); // init main application layer

// define webhook channel configuration.
// can also be function (req => WebhookChannel | Promise<WebhookChannel>)
const channel = {
  url: process.env.BOT_WEBHOOK_URL,
  secret: process.env.BOT_WEBHOOK_SECRET
};
const webhook = new OracleBot.Middleware.WebhookClient({ channel });

// receive messages from bot and forward to user
app.post('/webhook/receiver', webhook.receiver((req, res) => {
  const { userId, messagePayload } = req.body;
  // Message was received and validated from bot. Forward to user accordingly...
}))

// receive messages from a client webhook and forward to bot
app.post('/webhook/:client/message', (req, res) => {
  let message = {};
  // assign userId, messagePayload, profile, etc... on message and send
  webhook.send(message)
   .then(() => res.send('ok'), e => res.status(400).send())
});
Parameters:
Name Type Attributes Description
options WebhookClientOptions <optional>

Methods

MessageModel() → {MessageModel}

Source:
Returns the MessageModel class for creating or validating messages to or from bots.
Returns:
The MessageModel class
Type
MessageModel

on(event, handler) → {WebhookClient}

Source:
Subscribe to WebhookClient events
Parameters:
Name Type Description
event WebhookEvent Event type to subscribe
handler WebhookEventHandler Event handler
Returns:
Type
WebhookClient

receiver(callbackopt) → {WebhookReceiverMiddleware}

Source:
Receiver middleware to handle messages incoming from bot. If used without callback, messages will be dispatched to any subscribers to the WebhookEvent.MESSAGE_RECEIVED event.
Parameters:
Name Type Attributes Description
callback WebhookReceiverCallback <optional>
Optional callback for received messages from bot.
Returns:
Type
WebhookReceiverMiddleware

send(message, channelopt)

Source:
See:
Send client message to bot
Parameters:
Name Type Attributes Description
message object Complete payload to send
channel WebhookChannel <optional>
Webhook channel configuration to use (if different than that in the instance options)

Type Definitions

WebhookChannel

Source:
Properties:
Name Type Description
url string Webhook url issued by bots platform channel
secret string Message signature secret key used to create X-Hub-Signature
Configuration details for sending messages to bots on a webhook channel.
Type:
  • Object

WebhookChannelCallback(reqopt) → {WebhookChannel|Promise.<WebhookChannel>}

Source:
Callback used by webhook client to obtain channel configuration information for a given request.
Example
const { WebhookClient, WebhookEvent } = require('@oracle/bots-node-sdk').Middleware;

function getChannelForReq(req) {
  const client = req.params.client;
  return {
    url: 'https://...',  // Oracle bot webhook url specific to client
    secret: '...',       // webhook channel secret key
  }
}

const webhook = new WebhookClient({
  channel: getChannelForReq,
});

app.post('/bot/messages', webhook.receiver());
webhook.on(WebhookEvent.MESSAGE_RECEIVED, message => {
  const { userId, messagePayload } = message;
  // format and send to user.
});
Parameters:
Name Type Attributes Description
req external:ExpressRequest <optional>
The request object originally sent to the endpoint
Returns:
Type
WebhookChannel | Promise.<WebhookChannel>

WebhookClientOptions

Source:
Properties:
Name Type Attributes Description
channel WebhookChannel | WebhookChannelCallback <optional>
Webhook channel configuration or callback.
Options to configure a webhook client endpoint where messages are forwarded to the bot on a webhook channel.

WebhookEvent

Source:
WebhookEvent enum for WebhookClient event subscriptions
Example
const { WebhookClient, WebhookEvent } = require('@oracle/bots-node-sdk').Middleware;

const webhook = new WebhookClient({
  channel: // ...
});
//... 
webhook.on(WebhookEvent.ERROR, error => {
  // webhook error
  console.error(error);
});
webhook.on(WebhookEvent.MESSAGE_RECEIVED, message => {
  // message received from bot. 
  // Format and send to user...
});
webhook.on(WebhookEvent.MESSAGE_SENT, message => {
  // message was sent to bot.
});

WebhookEventHandler(detail) → {void}

Source:
Callback handler for WebhookClient event emitter.
Parameters:
Name Type Description
detail * Event detail payload.
Returns:
Type
void

WebhookReceiverMiddleware(req, res, next) → {void}

Source:
Callback function upon successful webhook validation. Further validations may be performed, and it is required to send the response for the webhook request. as res.send, res.json, etc. Note that this response is NOT a message back to the bot.
Parameters:
Name Type Description
req external:ExpressRequest Request with validated req.body
res external:ExpressResponse Response to bots webhook request
next function Express NextFunction
Returns:
Type
void