-
Notifications
You must be signed in to change notification settings - Fork 251
feat(twilio): add RCS support for interactive inbound and rich outbound #590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
oss-polar-bear
wants to merge
3
commits into
vercel:main
Choose a base branch
from
oss-polar-bear:feat/twilio-rcs-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "@chat-adapter/twilio": minor | ||
| --- | ||
|
|
||
| feat(twilio): add RCS support with rich cards, button actions, and location sharing | ||
|
|
||
| Extends the Twilio adapter with full RCS support: inbound button tap routing via `processAction`, location share parsing, Content API integration for rich outbound cards with SMS fallback, and channel metadata detection. Cards sent to RCS-capable senders (Messaging Service or `rcs:` address) are automatically rendered as Twilio Content templates with embedded SMS fallback variants. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| # message log | ||
|
|
||
| ## SMS inbound text | ||
|
|
||
| ``` | ||
| AccountSid=AC000000000000000000000000000&Body=Hello+bot&From=%2B15551234567&MessageSid=SM00000000000000000000000000000&NumMedia=0&To=%2B15559876543 | ||
| ``` | ||
|
|
||
| ## RCS inbound text with ChannelMetadata | ||
|
|
||
| ``` | ||
| AccountSid=AC000000000000000000000000000&Body=Hello+from+RCS&ChannelMetadata=%7B%22type%22%3A%22rcs%22%7D&From=%2B15551234567&MessageSid=SM11111111111111111111111111111&NumMedia=0&To=%2B15559876543 | ||
| ``` | ||
|
|
||
| ## RCS button tap (ButtonPayload) | ||
|
|
||
| ``` | ||
| AccountSid=AC000000000000000000000000000&ButtonPayload=chat%3A%7B%22a%22%3A%22approve%22%2C%22v%22%3A%22prod%22%7D&ButtonText=Approve&ChannelMetadata=%7B%22type%22%3A%22rcs%22%7D&From=%2B15551234567&MessageSid=SM22222222222222222222222222222&To=%2B15559876543 | ||
| ``` | ||
|
|
||
| ## RCS location share | ||
|
|
||
| ``` | ||
| AccountSid=AC000000000000000000000000000&Address=1600+Amphitheatre+Parkway%2C+Mountain+View%2C+CA&Body=&ChannelMetadata=%7B%22type%22%3A%22rcs%22%7D&From=%2B15551234567&Label=Google+HQ&Latitude=37.4220936&Longitude=-122.0840897&MessageSid=SM33333333333333333333333333333&NumMedia=0&To=%2B15559876543 | ||
| ``` | ||
|
|
||
| ## Status callback with ChannelPrefix=rcs | ||
|
|
||
| ``` | ||
| AccountSid=AC000000000000000000000000000&ChannelPrefix=rcs&From=%2B15559876543&MessageSid=SM44444444444444444444444444444&MessageStatus=delivered&To=%2B15551234567 | ||
| ``` | ||
|
|
||
| ## Status callback with EventType=READ | ||
|
|
||
| ``` | ||
| AccountSid=AC000000000000000000000000000&ChannelPrefix=rcs&EventType=READ&From=%2B15559876543&MessageSid=SM55555555555555555555555555555&MessageStatus=read&To=%2B15551234567 | ||
| ``` | ||
|
|
||
| ## MMS inbound with media | ||
|
|
||
| ``` | ||
| AccountSid=AC000000000000000000000000000&Body=Check+this+photo&From=%2B15551234567&MediaContentType0=image%2Fjpeg&MediaUrl0=https%3A%2F%2Fapi.twilio.com%2F2010-04-01%2FAccounts%2FAC000000000000000000000000000%2FMessages%2FSM66666666666666666666666666666%2FMedia%2FME00000000000000000000000000000&MessageSid=SM66666666666666666666666666666&NumMedia=1&To=%2B15559876543 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { createTwilioContent } from "./content"; | ||
|
|
||
| describe("createTwilioContent", () => { | ||
| it("posts JSON to the Content API", async () => { | ||
| const request = vi.fn(async () => | ||
| Response.json({ sid: "HX123", friendly_name: "test" }) | ||
| ); | ||
|
|
||
| const result = await createTwilioContent({ | ||
| contentBody: { | ||
| friendly_name: "test", | ||
| language: "en", | ||
| types: { | ||
| "twilio/quick-reply": { | ||
| body: "Pick one", | ||
| actions: [ | ||
| { id: 'chat:{"a":"yes"}', title: "Yes", type: "quick_reply" }, | ||
| ], | ||
| }, | ||
| "twilio/text": { body: "Pick one: Yes" }, | ||
| }, | ||
| }, | ||
| credentials: { accountSid: "AC123", authToken: "token" }, | ||
| fetch: request, | ||
| }); | ||
|
|
||
| expect(result.sid).toBe("HX123"); | ||
| expect(String(request.mock.calls[0]?.[0])).toBe( | ||
| "https://content.twilio.com/v1/Content" | ||
| ); | ||
| const options = request.mock.calls[0]?.[1]; | ||
| expect(options?.method).toBe("POST"); | ||
| expect(options?.headers).toMatchObject({ | ||
| "content-type": "application/json", | ||
| }); | ||
| const body = JSON.parse(options?.body as string); | ||
| expect(body.friendly_name).toBe("test"); | ||
| }); | ||
|
|
||
| it("uses custom contentApiUrl when provided", async () => { | ||
| const request = vi.fn(async () => Response.json({ sid: "HX456" })); | ||
|
|
||
| await createTwilioContent({ | ||
| contentApiUrl: "https://content.test", | ||
| contentBody: { | ||
| friendly_name: "test", | ||
| language: "en", | ||
| types: { "twilio/text": { body: "hello" } }, | ||
| }, | ||
| credentials: { accountSid: "AC123", authToken: "token" }, | ||
| fetch: request, | ||
| }); | ||
|
|
||
| expect(String(request.mock.calls[0]?.[0])).toBe( | ||
| "https://content.test/v1/Content" | ||
| ); | ||
| }); | ||
|
|
||
| it("throws on non-ok responses", async () => { | ||
| const request = vi.fn( | ||
| async () => | ||
| new Response(JSON.stringify({ message: "bad request" }), { | ||
| headers: { "content-type": "application/json" }, | ||
| status: 400, | ||
| }) | ||
| ); | ||
|
|
||
| await expect( | ||
| createTwilioContent({ | ||
| contentBody: { | ||
| friendly_name: "test", | ||
| language: "en", | ||
| types: {}, | ||
| }, | ||
| credentials: { accountSid: "AC123", authToken: "token" }, | ||
| fetch: request, | ||
| }) | ||
| ).rejects.toThrow("Content API returned HTTP 400"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import type { TwilioContentBody } from "../cards"; | ||
| import type { TwilioApiOptions } from "./index"; | ||
| import { resolveTwilioCredential } from "./index"; | ||
|
|
||
| const DEFAULT_CONTENT_API_URL = "https://content.twilio.com"; | ||
|
|
||
| export interface TwilioContentResource { | ||
| account_sid?: string; | ||
| date_created?: string; | ||
| date_updated?: string; | ||
| friendly_name?: string; | ||
| language?: string; | ||
| sid: string; | ||
| types?: Record<string, unknown>; | ||
| url?: string; | ||
| variables?: Record<string, string>; | ||
| } | ||
|
|
||
| export interface CreateTwilioContentOptions extends TwilioApiOptions { | ||
| contentApiUrl?: string; | ||
| contentBody: TwilioContentBody; | ||
| } | ||
|
|
||
| export async function createTwilioContent( | ||
| options: CreateTwilioContentOptions | ||
| ): Promise<TwilioContentResource> { | ||
| const accountSid = await resolveTwilioCredential( | ||
| options.credentials?.accountSid, | ||
| "TWILIO_ACCOUNT_SID" | ||
| ); | ||
| const authToken = await resolveTwilioCredential( | ||
| options.credentials?.authToken, | ||
| "TWILIO_AUTH_TOKEN" | ||
| ); | ||
|
|
||
| const baseUrl = options.contentApiUrl ?? DEFAULT_CONTENT_API_URL; | ||
| const url = new URL("/v1/Content", baseUrl); | ||
|
|
||
| const request = options.fetch ?? fetch; | ||
| const response = await request(url, { | ||
| body: JSON.stringify(options.contentBody), | ||
| headers: { | ||
| authorization: `Basic ${btoa(`${accountSid}:${authToken}`)}`, | ||
| "content-type": "application/json", | ||
| }, | ||
| method: "POST", | ||
| }); | ||
|
|
||
| const body = await response.text(); | ||
| let parsed: unknown; | ||
| try { | ||
| parsed = JSON.parse(body); | ||
| } catch { | ||
| parsed = body; | ||
| } | ||
|
|
||
| if (!response.ok) { | ||
| throw new Error( | ||
| `Content API returned HTTP ${response.status}: ${typeof parsed === "string" ? parsed : JSON.stringify(parsed)}` | ||
| ); | ||
| } | ||
|
|
||
| return parsed as TwilioContentResource; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.