| 1 | import express from "express"; |
| 2 | import { Pinnacle, PinnacleClient } from "rcs-js"; |
| 3 | import { config } from "dotenv"; |
| 4 | |
| 5 | config(); |
| 6 | |
| 7 | const port = 3000; |
| 8 | const app = express(); |
| 9 | const client = new PinnacleClient({ apiKey: process.env.PINNACLE_API_KEY }); |
| 10 | |
| 11 | app.get("/send-rcs/:phoneNumber", async (req, res) => { |
| 12 | const { phoneNumber } = req.params; |
| 13 | |
| 14 | const result = await client.messages.rcs.send({ |
| 15 | options: { |
| 16 | validate: true, // Will let you know if your RCS message will fail to be sent ahead of making the request to carriers |
| 17 | }, |
| 18 | from: process.env.AGENT_ID || "", |
| 19 | to: phoneNumber, |
| 20 | cards: [ |
| 21 | { |
| 22 | media: |
| 23 | "https://server.trypinnacle.app/storage/v1/object/sign/vault/3/67330f56-8fc4-4d9e-882d-161b84fc8e31/Your_image_here.png?token=eyJraWQiOiJzdG9yYWdlLXVybC1zaWduaW5nLWtleV9hOGI0YTI0NC00NzY4LTRhOTktYWI4MS1iNmZhNTZhNGQyZWYiLCJhbGciOiJIUzI1NiJ9.eyJ1cmwiOiJ2YXVsdC8zLzY3MzMwZjU2LThmYzQtNGQ5ZS04ODJkLTE2MWI4NGZjOGUzMS9Zb3VyX2ltYWdlX2hlcmUucG5nIiwiaWF0IjoxNzYwOTg0OTEwLCJleHAiOjMxNzEyMDk4NDkxMH0.bcIMtiBAvV8C7Gw7uYaR5TMGsCep7w1TvRMjoRlP_-g", |
| 24 | title: "Hello, world!", |
| 25 | subtitle: "This is an example RCS message with rich content.", |
| 26 | buttons: [ |
| 27 | { |
| 28 | type: "trigger", |
| 29 | metadata: "", |
| 30 | payload: "HELLO", |
| 31 | title: "Say hello back", |
| 32 | }, |
| 33 | { |
| 34 | type: "sendLocation", |
| 35 | latLong: { |
| 36 | lat: 36.7749, |
| 37 | lng: -122.4194, |
| 38 | }, |
| 39 | metadata: "", |
| 40 | title: "View example location", |
| 41 | }, |
| 42 | ], |
| 43 | }, |
| 44 | { |
| 45 | media: |
| 46 | "https://server.trypinnacle.app/storage/v1/object/sign/vault/3/67330f56-8fc4-4d9e-882d-161b84fc8e31/Your_image_here.png?token=eyJraWQiOiJzdG9yYWdlLXVybC1zaWduaW5nLWtleV9hOGI0YTI0NC00NzY4LTRhOTktYWI4MS1iNmZhNTZhNGQyZWYiLCJhbGciOiJIUzI1NiJ9.eyJ1cmwiOiJ2YXVsdC8zLzY3MzMwZjU2LThmYzQtNGQ5ZS04ODJkLTE2MWI4NGZjOGUzMS9Zb3VyX2ltYWdlX2hlcmUucG5nIiwiaWF0IjoxNzYwOTg0OTEwLCJleHAiOjMxNzEyMDk4NDkxMH0.bcIMtiBAvV8C7Gw7uYaR5TMGsCep7w1TvRMjoRlP_-g", |
| 47 | title: "Your second card", |
| 48 | subtitle: |
| 49 | "This subtitle is optional. Each card can have different buttons, like this one allowing you to share your location instead of view a location.", |
| 50 | buttons: [ |
| 51 | { |
| 52 | type: "requestUserLocation", |
| 53 | metadata: "", |
| 54 | title: "Share your location", |
| 55 | }, |
| 56 | ], |
| 57 | }, |
| 58 | ], |
| 59 | // Quick replies are available across all cards while buttons are specific to certain cards, and only show when a user swipes to that particular card |
| 60 | quickReplies: [ |
| 61 | { |
| 62 | type: "scheduleEvent", |
| 63 | title: "Add example event to cal", |
| 64 | metadata: "", |
| 65 | eventTitle: "Sample Event", |
| 66 | eventStartTime: "2025-12-11T00:00:00Z", |
| 67 | eventEndTime: "2025-12-11T23:59:59Z", |
| 68 | eventDescription: "This is an example calendar event.", |
| 69 | }, |
| 70 | { |
| 71 | type: "call", |
| 72 | metadata: "", |
| 73 | payload: "+14152321234", |
| 74 | title: "Call example number", |
| 75 | }, |
| 76 | { |
| 77 | type: "openUrl", |
| 78 | metadata: "", |
| 79 | payload: "https://docs.pinnacle.sh/api-reference/messages/send-rcs", |
| 80 | title: "View RCS docs", |
| 81 | }, |
| 82 | ], |
| 83 | }); |
| 84 | |
| 85 | res.json(result); |
| 86 | }); |
| 87 | |
| 88 | app.post("/inbound-rcs", express.json(), async (req, res) => { |
| 89 | try { |
| 90 | // Process and validate the webhook |
| 91 | // Returns a fully typed MessageEvent object |
| 92 | const messageEvent: Pinnacle.MessageEvent | Pinnacle.UserEvent = |
| 93 | await client.messages.process(req); |
| 94 | |
| 95 | console.log(messageEvent); |
| 96 | |
| 97 | // messageEvent is now typed as Pinnacle.MessageEvent |
| 98 | // Your business logic here |
| 99 | await handleInboundMessage(messageEvent); |
| 100 | |
| 101 | res.status(200).json({ message: "You received a message" }); |
| 102 | } catch (error) { |
| 103 | console.error("Error occurred processing webhook message", error); |
| 104 | res.status(200).json({ message: "You received a message" }); |
| 105 | } |
| 106 | }); |
| 107 | |
| 108 | async function handleInboundMessage( |
| 109 | event: Pinnacle.MessageEvent | Pinnacle.UserEvent |
| 110 | ) { |
| 111 | // event is fully typed with autocomplete support |
| 112 | // Your message handling logic |
| 113 | // Check if the message contains a button click with HELLO payload |
| 114 | // View full docs on processing events here: https://docs.pinnacle.sh/methods/process |
| 115 | if (event.type === "MESSAGE.RECEIVED") { |
| 116 | if (event.direction === "INBOUND") { |
| 117 | console.log("event:", event.message); |
| 118 | |
| 119 | const message = event.message; |
| 120 | // Check if this is RCS button data (discriminated by type field) |
| 121 | if ( |
| 122 | message.type === "RCS_BUTTON_DATA" && |
| 123 | message.button.payload === "HELLO" |
| 124 | ) { |
| 125 | await sendHello(event.conversation.from); |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | async function sendHello(to: string) { |
| 132 | const result = await client.messages.rcs.send({ |
| 133 | text: "Hello! Button clicked successfully.", |
| 134 | from: process.env.AGENT_ID || "", |
| 135 | to: to, |
| 136 | quickReplies: [], |
| 137 | }); |
| 138 | console.log(result); |
| 139 | } |
| 140 | |
| 141 | app.listen(port, () => { |
| 142 | console.log(`Server is listening on port ${port}`); |
| 143 | }); |