Util/Webhook

Description:
  • 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:
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.

Methods

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

Description:
  • 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.
Source:
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)

Description:
  • create the payload signature header.
Source:
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)

Description:
  • utility function to send message to bot webhook channel, generating the right message with signature
Source:
Deprecated:
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)

Description:
  • 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.
Source:
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}

Description:
  • utility function to perform webhook signature verification
Source:
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