|
| 1 | +import { sign, decode } from "jsonwebtoken"; |
| 2 | +import { Context } from "@twilio-labs/serverless-runtime-types/types"; |
| 3 | + |
| 4 | +/** |
| 5 | + * Sends a message to the bot |
| 6 | + * @param context - The Twilio runtime context |
| 7 | + * @param botInstanceId - The ID of the bot instance |
| 8 | + * @param body - The message body to send |
| 9 | + */ |
| 10 | +export async function sendMessageToBot( |
| 11 | + context: Context & { TWILIO_REGION?: string; ACCOUNT_SID: string; AUTH_TOKEN: string }, |
| 12 | + botInstanceId: string, |
| 13 | + body: Record<string, any> |
| 14 | +) { |
| 15 | + const url = `https://ROVO_URL_HERE`; |
| 16 | + |
| 17 | + const response = await fetch(url, { |
| 18 | + method: "POST", |
| 19 | + body: JSON.stringify(body), |
| 20 | + headers: { |
| 21 | + Authorization: `Basic ${Buffer.from( |
| 22 | + `${context.ACCOUNT_SID}:${context.AUTH_TOKEN}`, |
| 23 | + "utf-8" |
| 24 | + ).toString("base64")}`, |
| 25 | + "Content-Type": "application/json", |
| 26 | + Accept: "application/json", |
| 27 | + }, |
| 28 | + }); |
| 29 | + if (response.ok) { |
| 30 | + console.log("Sent message to Bot"); |
| 31 | + return; |
| 32 | + } else { |
| 33 | + throw new Error( |
| 34 | + "Failed to send request to Bot. " + (await response.text()) |
| 35 | + ); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +/** |
| 40 | + * Reads attributes from a conversation |
| 41 | + * @param context - The Twilio runtime context |
| 42 | + * @param chatServiceSid - The Chat Service SID |
| 43 | + * @param conversationSid - The Conversation SID |
| 44 | + * @returns The parsed conversation attributes |
| 45 | + */ |
| 46 | +export async function readConversationAttributes( |
| 47 | + context: Context & { getTwilioClient: () => any }, |
| 48 | + chatServiceSid: string, |
| 49 | + conversationSid: string |
| 50 | +) { |
| 51 | + try { |
| 52 | + const client = context.getTwilioClient(); |
| 53 | + const data = await client.conversations.v1 |
| 54 | + .services(chatServiceSid) |
| 55 | + .conversations(conversationSid) |
| 56 | + .fetch(); |
| 57 | + return JSON.parse(data.attributes); |
| 58 | + } catch (err) { |
| 59 | + console.error(err); |
| 60 | + return {}; |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Gets the bot ID from context or event |
| 66 | + * @param context - The Twilio runtime context |
| 67 | + * @param event - The event object |
| 68 | + * @returns The bot ID |
| 69 | + */ |
| 70 | +export async function getBotId( |
| 71 | + context: Context & { AUTH_TOKEN: string; BOT_ID?: string }, |
| 72 | + event: { |
| 73 | + EventType?: string; |
| 74 | + botId?: string; |
| 75 | + ConversationSid?: string; |
| 76 | + ChatServiceSid?: string; |
| 77 | + } |
| 78 | +) { |
| 79 | + if (event.EventType === "onMessageAdded") { |
| 80 | + try { |
| 81 | + const { ConversationSid, ChatServiceSid } = event; |
| 82 | + const parsed = await readConversationAttributes( |
| 83 | + context, |
| 84 | + ChatServiceSid, |
| 85 | + ConversationSid |
| 86 | + ); |
| 87 | + if (typeof parsed.botId === "string" && parsed.botId) { |
| 88 | + return parsed.botId; |
| 89 | + } |
| 90 | + } catch (err) { |
| 91 | + console.log("Invalid attribute structure", err); |
| 92 | + } |
| 93 | + } |
| 94 | + const botId = event.botId || context.BOT_ID || event.botId || context.BOT_ID; |
| 95 | + |
| 96 | + if (!botId) { |
| 97 | + throw new Error("Missing Bot ID configuration"); |
| 98 | + } |
| 99 | + |
| 100 | + return botId; |
| 101 | +} |
| 102 | + |
| 103 | +/** |
| 104 | + * Signs a request with JWT |
| 105 | + * @param context - The Twilio runtime context |
| 106 | + * @param event - The event object |
| 107 | + * @returns The signed JWT token |
| 108 | + */ |
| 109 | +export async function signRequest( |
| 110 | + context: Context & { AUTH_TOKEN: string }, |
| 111 | + event: Record<string, any> |
| 112 | +) { |
| 113 | + const assistantSid = await getBotId(context, event); |
| 114 | + const authToken = context.AUTH_TOKEN; |
| 115 | + if (!authToken) { |
| 116 | + throw new Error("No auth token found"); |
| 117 | + } |
| 118 | + return sign({ assistantSid }, authToken, { expiresIn: "5m" }); |
| 119 | +} |
| 120 | + |
| 121 | +/** |
| 122 | + * Verifies a request token |
| 123 | + * @param context - The Twilio runtime context |
| 124 | + * @param event - The event object containing the token |
| 125 | + * @returns Whether the token is valid |
| 126 | + */ |
| 127 | +export function verifyRequest( |
| 128 | + context: Context & { AUTH_TOKEN: string }, |
| 129 | + event: { _token: string } |
| 130 | +) { |
| 131 | + const token = event._token; |
| 132 | + if (!token) { |
| 133 | + throw new Error("Missing token"); |
| 134 | + } |
| 135 | + |
| 136 | + const authToken = context.AUTH_TOKEN; |
| 137 | + if (!authToken) { |
| 138 | + throw new Error("No auth token found"); |
| 139 | + } |
| 140 | + |
| 141 | + try { |
| 142 | + // The decode function from jsonwebtoken only takes a token and options |
| 143 | + const decoded = decode(token, { json: true }); |
| 144 | + if (decoded && typeof decoded === 'object' && 'assistantSid' in decoded) { |
| 145 | + return true; |
| 146 | + } |
| 147 | + } catch (err) { |
| 148 | + console.error("Failed to verify token", err); |
| 149 | + return false; |
| 150 | + } |
| 151 | + return false; |
| 152 | +} |
| 153 | + |
| 154 | +// All functions are already exported using named exports |
0 commit comments