@convex-dev/twilio
Version:
Convex component for sending/receiving SMS messages with Twilio.
424 lines • 15.9 kB
TypeScript
import { type Expand, type FunctionReference, type GenericActionCtx, type GenericDataModel, type GenericQueryCtx, HttpRouter } from "convex/server";
import type { Infer } from "convex/values";
import type { ComponentApi } from "../component/_generated/component.js";
export declare const messageValidator: import("convex/values").VObject<{
counterparty?: string | undefined;
rest?: any;
account_sid: string;
api_version: string;
body: string;
date_created: string;
date_sent: string | null;
date_updated: string | null;
direction: string;
error_code: number | null;
error_message: string | null;
from: string;
messaging_service_sid: string | null;
num_media: string;
num_segments: string;
price: string | null;
price_unit: string | null;
sid: string;
status: string;
subresource_uris: {
feedback?: string | undefined;
media: string;
} | null;
to: string;
uri: string;
}, {
account_sid: import("convex/values").VString<string, "required">;
api_version: import("convex/values").VString<string, "required">;
body: import("convex/values").VString<string, "required">;
counterparty: import("convex/values").VString<string | undefined, "optional">;
date_created: import("convex/values").VString<string, "required">;
date_sent: import("convex/values").VUnion<string | null, [import("convex/values").VString<string, "required">, import("convex/values").VNull<null, "required">], "required", never>;
date_updated: import("convex/values").VUnion<string | null, [import("convex/values").VString<string, "required">, import("convex/values").VNull<null, "required">], "required", never>;
direction: import("convex/values").VString<string, "required">;
error_code: import("convex/values").VUnion<number | null, [import("convex/values").VFloat64<number, "required">, import("convex/values").VNull<null, "required">], "required", never>;
error_message: import("convex/values").VUnion<string | null, [import("convex/values").VString<string, "required">, import("convex/values").VNull<null, "required">], "required", never>;
from: import("convex/values").VString<string, "required">;
messaging_service_sid: import("convex/values").VUnion<string | null, [import("convex/values").VString<string, "required">, import("convex/values").VNull<null, "required">], "required", never>;
num_media: import("convex/values").VString<string, "required">;
num_segments: import("convex/values").VString<string, "required">;
price: import("convex/values").VUnion<string | null, [import("convex/values").VString<string, "required">, import("convex/values").VNull<null, "required">], "required", never>;
price_unit: import("convex/values").VUnion<string | null, [import("convex/values").VString<string, "required">, import("convex/values").VNull<null, "required">], "required", never>;
sid: import("convex/values").VString<string, "required">;
status: import("convex/values").VString<string, "required">;
subresource_uris: import("convex/values").VUnion<{
feedback?: string | undefined;
media: string;
} | null, [import("convex/values").VObject<{
feedback?: string | undefined;
media: string;
}, {
media: import("convex/values").VString<string, "required">;
feedback: import("convex/values").VString<string | undefined, "optional">;
}, "required", "media" | "feedback">, import("convex/values").VNull<null, "required">], "required", "media" | "feedback">;
to: import("convex/values").VString<string, "required">;
uri: import("convex/values").VString<string, "required">;
rest: import("convex/values").VAny<any, "optional", string>;
}, "required", "account_sid" | "api_version" | "body" | "counterparty" | "date_created" | "date_sent" | "date_updated" | "direction" | "error_code" | "error_message" | "from" | "messaging_service_sid" | "num_media" | "num_segments" | "price" | "price_unit" | "sid" | "status" | "subresource_uris" | "to" | "uri" | "rest" | "subresource_uris.media" | "subresource_uris.feedback" | `rest.${string}`>;
export type Message = Infer<typeof messageValidator>;
export type MessageHandler = FunctionReference<"mutation", "internal", {
message: Message;
}>;
export declare class Twilio<From extends {
defaultFrom?: string;
} | Record<string, never>> {
componentApi: ComponentApi;
readonly accountSid: string;
readonly authToken: string;
readonly httpPrefix: string;
readonly defaultFrom?: From["defaultFrom"];
incomingMessageCallback?: MessageHandler;
defaultOutgoingMessageCallback?: MessageHandler;
constructor(componentApi: ComponentApi, options: {
TWILIO_ACCOUNT_SID?: string;
TWILIO_AUTH_TOKEN?: string;
httpPrefix?: string;
incomingMessageCallback?: MessageHandler;
defaultOutgoingMessageCallback?: MessageHandler;
} & From);
/**
* Registers the routes for handling Twilio message status and incoming messages.
*
* @param http - The HTTP router to register routes on.
*/
registerRoutes(http: HttpRouter): void;
/**
* Sends a message using the Twilio API.
*
* @param ctx - A Convex context for running the action.
* @param args - The arguments for sending the message.
* @param args.to - The recipient's phone number e.g. +14151234567.
* @param args.body - The body of the message.
* @param args.callback - An optional callback function to be called after successfully sending.
* @param args.from - The sender's phone number. If not provided, the default from number is used.
* @throws {Error} If the from number is missing and no default from number is set.
* @returns A promise that resolves with the result of the message creation action.
*/
sendMessage(ctx: RunActionCtx, args: Expand<{
to: string;
body: string;
callback?: MessageHandler;
} & (From["defaultFrom"] extends string ? {
from?: string;
} : {
from: string;
})>): Promise<{
account_sid: string;
api_version: string;
body: string;
counterparty?: string;
date_created: string;
date_sent: string | null;
date_updated: string | null;
direction: string;
error_code: number | null;
error_message: string | null;
from: string;
messaging_service_sid: string | null;
num_media: string;
num_segments: string;
price: string | null;
price_unit: string | null;
rest?: any;
sid: string;
status: string;
subresource_uris: {
feedback?: string;
media: string;
} | null;
to: string;
uri: string;
}>;
/**
* Registers an incoming SMS handler for a Twilio phone number.
*
* @param ctx - The Convex function context.
* @param args - The arguments for registering the SMS handler.
* @param args.sid - The SID of the phone number to update.
* @returns A promise that resolves with the result of the action.
*/
registerIncomingSmsHandler(ctx: RunActionCtx, args: {
sid: string;
}): Promise<any>;
/**
* Lists messages sent or received using this component.
*
* @param ctx - The Convex function context.
* @param args - Optional arguments for listing messages.
* @param args.limit - The maximum number of messages to retrieve.
* @returns A promise that resolves with the list of messages.
*/
list(ctx: RunQueryCtx, args?: {
limit?: number;
}): Promise<{
account_sid: string;
api_version: string;
body: string;
counterparty?: string;
date_created: string;
date_sent: string | null;
date_updated: string | null;
direction: string;
error_code: number | null;
error_message: string | null;
from: string;
messaging_service_sid: string | null;
num_media: string;
num_segments: string;
price: string | null;
price_unit: string | null;
rest?: any;
sid: string;
status: string;
subresource_uris: {
feedback?: string;
media: string;
} | null;
to: string;
uri: string;
}[]>;
/**
* Lists messages received using this component.
*
* @param ctx - The Convex function context.
* @param args - Optional arguments for listing messages.
* @param args.limit - The maximum number of messages to retrieve.
* @returns A promise that resolves with the list of messages.
*/
listIncoming(ctx: RunQueryCtx, args?: {
limit?: number;
}): Promise<{
account_sid: string;
api_version: string;
body: string;
counterparty?: string;
date_created: string;
date_sent: string | null;
date_updated: string | null;
direction: string;
error_code: number | null;
error_message: string | null;
from: string;
messaging_service_sid: string | null;
num_media: string;
num_segments: string;
price: string | null;
price_unit: string | null;
rest?: any;
sid: string;
status: string;
subresource_uris: {
feedback?: string;
media: string;
} | null;
to: string;
uri: string;
}[]>;
/**
* Lists messages sent using this component.
*
* @param ctx - The Convex function context.
* @param args - Optional arguments for listing messages.
* @param args.limit - The maximum number of messages to retrieve.
* @returns A promise that resolves with the list of messages.
*/
listOutgoing(ctx: RunQueryCtx, args?: {
limit?: number;
}): Promise<{
account_sid: string;
api_version: string;
body: string;
counterparty?: string;
date_created: string;
date_sent: string | null;
date_updated: string | null;
direction: string;
error_code: number | null;
error_message: string | null;
from: string;
messaging_service_sid: string | null;
num_media: string;
num_segments: string;
price: string | null;
price_unit: string | null;
rest?: any;
sid: string;
status: string;
subresource_uris: {
feedback?: string;
media: string;
} | null;
to: string;
uri: string;
}[]>;
/**
* Retrieves a message by its Twilio SID.
*
* @param ctx - The Convex function context.
* @param args - The arguments for retrieving the message.
* @param args.sid - The SID of the message to retrieve.
* @returns A promise that resolves with the message details.
*/
getMessageBySid(ctx: RunQueryCtx, args: {
sid: string;
}): Promise<{
account_sid: string;
api_version: string;
body: string;
counterparty?: string;
date_created: string;
date_sent: string | null;
date_updated: string | null;
direction: string;
error_code: number | null;
error_message: string | null;
from: string;
messaging_service_sid: string | null;
num_media: string;
num_segments: string;
price: string | null;
price_unit: string | null;
rest?: any;
sid: string;
status: string;
subresource_uris: {
feedback?: string;
media: string;
} | null;
to: string;
uri: string;
} | null>;
/**
* Retrieves messages sent to a specific phone number using the component.
*
* @param ctx - The Convex function context.
* @param args - The arguments for retrieving the messages.
* @param args.to - The recipient's phone number.
* @param args.limit - Optional. The maximum number of messages to retrieve.
* @returns A promise that resolves with the list of messages.
*/
getMessagesTo(ctx: RunQueryCtx, args: {
to: string;
limit?: number;
}): Promise<{
account_sid: string;
api_version: string;
body: string;
counterparty?: string;
date_created: string;
date_sent: string | null;
date_updated: string | null;
direction: string;
error_code: number | null;
error_message: string | null;
from: string;
messaging_service_sid: string | null;
num_media: string;
num_segments: string;
price: string | null;
price_unit: string | null;
rest?: any;
sid: string;
status: string;
subresource_uris: {
feedback?: string;
media: string;
} | null;
to: string;
uri: string;
}[]>;
/**
* Retrieves messages received from a specific phone number using the component.
*
* @param ctx - The Convex function context.
* @param args - The arguments for retrieving the messages.
* @param args.from - The sender's phone number.
* @param args.limit - Optional. The maximum number of messages to retrieve.
* @returns A promise that resolves with the list of messages.
*/
getMessagesFrom(ctx: RunQueryCtx, args: {
from: string;
limit?: number;
}): Promise<{
account_sid: string;
api_version: string;
body: string;
counterparty?: string;
date_created: string;
date_sent: string | null;
date_updated: string | null;
direction: string;
error_code: number | null;
error_message: string | null;
from: string;
messaging_service_sid: string | null;
num_media: string;
num_segments: string;
price: string | null;
price_unit: string | null;
rest?: any;
sid: string;
status: string;
subresource_uris: {
feedback?: string;
media: string;
} | null;
to: string;
uri: string;
}[]>;
/**
* Retrieves messages sent to or received from a specific phone number using the component.
*
* @param ctx - The Convex function context.
* @param args - The arguments for retrieving the messages.
* @param args.counterparty - The recipient's or sender's phone number.
* @param args.limit - Optional. The maximum number of messages to retrieve.
* @returns A promise that resolves with the list of messages.
*/
getMessagesByCounterparty(ctx: RunQueryCtx, args: {
counterparty: string;
limit?: number;
}): Promise<{
account_sid: string;
api_version: string;
body: string;
counterparty?: string;
date_created: string;
date_sent: string | null;
date_updated: string | null;
direction: string;
error_code: number | null;
error_message: string | null;
from: string;
messaging_service_sid: string | null;
num_media: string;
num_segments: string;
price: string | null;
price_unit: string | null;
rest?: any;
sid: string;
status: string;
subresource_uris: {
feedback?: string;
media: string;
} | null;
to: string;
uri: string;
}[]>;
}
export default Twilio;
declare global {
const Convex: Record<string, unknown>;
}
type RunActionCtx = {
runAction: GenericActionCtx<GenericDataModel>["runAction"];
};
type RunQueryCtx = {
runQuery: GenericQueryCtx<GenericDataModel>["runQuery"];
};
//# sourceMappingURL=index.d.ts.map