Receiving Messages and User Events

When your webhook receives inbound messages and user events from Pinnacle, the raw request body lacks type information. The process method will validate incoming events by checking the signing secret and transforms the request into a fully typed MessageEvent or UserEvent objects.

Parameters

ParameterTypeDescription
reqRequest | ExpressLikeRequestStandard Fetch API Request object or Express-like request that must contain both a headers and body field.
secretstring (optional)Signing secret for webhook validation. If not provided, uses PINNACLE_SIGNING_SECRET environment variable. Throws an error if neither is provided.

Returns

One of the following objects:

  • MessageEvent: Inbound message or message status update.
  • UserEvent: User event such as when a user started typing.

Errors

ErrorDescription
UnauthorizedErrorWebhook secret validation failed
BadRequestErrorRequest body validation failed

Example Implementation

1import express from 'express';
2import { PinnacleClient, Pinnacle } from 'rcs-js';
3
4const app = express();
5const client = new PinnacleClient({ apiKey: PINNACLE_API_KEY });
6
7app.post('/webhook', express.json(), async (req, res) => {
8 try {
9 // Process and validate the webhook
10 // Returns a fully typed MessageEvent or UserEvent object
11 const messageEvent: Pinnacle.MessageEvent | Pinnacle.UserEvent = await client.messages.process(req);
12
13 // messageEvent is now typed as Pinnacle.MessageEvent or Pinnacle.UserEvent
14 console.log(messageEvent);
15
16 res.status(200).json({ status: 'processed' });
17 } catch (error) {
18 throw error
19 }
20});