Util/Webhook

The webhookUtil is a set of utility functions for bot integration via webhook channel. While most use cases are accommodated through the WebhookClient instance methods and options, direct use of these methods is also possible.
Source:

Methods

(static) bodyParserRawMessageVerify(req, res, buf, encoding)

Source:
utility function for use with expressjs route in handling the raw message body of the webhook message received from bot. Instead of just letting bodyParser.json to parse the raw message to JSON, the rawMessage and its encoding is saved as properties 'rawBody' and 'encoding' for use in signature verification in method verifyMessageFormat.
Example
app.post('/webhook/messages', 
   bodyParser.json({
     verify: webhookUtil.bodyParserRawMessageVerify
   }), 
   function (req, res) {
     // request body is now available in req.rawBody, req.encoding is also set
   }
 );
Parameters:
Name Type Description
req object expressjs req for the POST route.
res object expressjs res for the POST route.
buf Buffer the raw message body.
encoding string encoding of the raw message body.

(static) buildSignatureHeader(buf, secret, encodingopt)

Source:
create the payload signature header.
Parameters:
Name Type Attributes Description
buf Buffer Raw payload as a Buffer, such as `Buffer.from(JSON.stringify(payload), 'utf8')`
secret string secret key of the channel for computing signature
encoding string <optional>
secret key of the channel for computing signature

(static) messageToBot(channelUrl, channelSecretKey, userId, inMsg, callback)

Source:
Deprecated:
utility function to send message to bot webhook channel, generating the right message with signature
Parameters:
Name Type Description
channelUrl string send the message to this channel url
channelSecretKey string secret key of the channel for computing message signature.
userId string userId is the sender of the message.
inMsg object | string message to be sent to bot
callback function callback function to be invoked after message is sent

(static) messageToBotWithProperties(channelUrl, channelSecretKey, userId, inMsg, additionalPropertiesopt, callback)

Source:
utility function to send message to bot webhook channel, generating the right message with signature. This function also allows additional properties to be sent along to the bot. A common use case is to add a profile property.
Example
webhookUtil.messageToBotWithProperties(
  channelUrl, 
  channelSecretKey, 
  userId, 
  messagePayload, 
  {
    "profile": {
      "firstName": 'John',
      "lastName": 'Smith'
      "age": 22,
      "clientType": 'Alexa'
    }
  },
  function (err) {
    if (err) {
      logger.warn("Failed sending message to Bot");
    }
  }
);
Parameters:
Name Type Attributes Description
channelUrl string send the message to this channel url
channelSecretKey string secret key of the channel for computing message signature.
userId string userId is the sender of the message.
inMsg object | string message to be sent to bot
additionalProperties object <optional>
additional properties like profile can be added
callback function callback function to be invoked after message is sent

(static) verifyMessageFormat(signature, msgBody, encoding, secretKey) → {boolean}

Source:
utility function to perform webhook signature verification
Example
if (webhookUtil.verifyMessageFromBot(req.get('X-Hub-Signature'), req.rawBody, req.encoding, channelSecretKey)) {
  res.sendStatus(200);
} else {
  res.sendStatus(403);
}
Parameters:
Name Type Description
signature string signature included in the bot message, to be compared to calculated signature.
msgBody Buffer raw message body of the bot message.
encoding string encoding of the raw message body.
secretKey string secretKey used to calculate message signature
Returns:
true if the webhook message received from Bots is verified successfully.
Type
boolean