import {z} from 'zod' export type AdaptiveCardColumnItemInfo = {type?: string | undefined, url?: string | undefined, size?: string | undefined, style?: string | undefined, wrap?: boolean | undefined, spacing?: string | undefined, text?: string | undefined, isSubtle?: boolean | undefined}; export type AdaptiveCardColumnInfo = {type?: string | undefined, width?: string | undefined, items?: Array | undefined}; export type AdaptiveCardInfoRequestItem = {type?: ('TextBlock' | 'ColumnSet' | 'Column' | 'FactSet') | undefined, text?: string | undefined, weight?: string | undefined, size?: string | undefined, columns?: Array | undefined}; export type AdaptiveCardInfoRequest = {type?: 'Container' | undefined, items?: Array | undefined}; export type ActionCardBody = {type?: 'Input.Text' | undefined, id?: string | undefined, isMultiline?: boolean | undefined, placeholder?: string | undefined}; export type ActionAdaptiveCardInfo = {type?: 'AdaptiveCard' | undefined, body?: Array | undefined}; export type AdaptiveCardAction = {type?: ('Action.ShowCard' | 'Action.Submit' | 'Action.OpenUrl' | 'Action.ToggleVisibility') | undefined, title?: string | undefined, card?: ActionAdaptiveCardInfo | undefined, url?: string | undefined}; export type AdaptiveCardSelectAction = {type: 'Action.Submit' | 'Action.OpenUrl' | 'Action.ToggleVisibility'}; export type ImageFillMode = 'cover' | 'repeatHorizontally' | 'repeatVertically' | 'repeat' | string; export type HorizontalAlignment = 'left' | 'center' | 'right' | string; export type VerticalAlignment = 'top' | 'center' | 'bottom' | string; export type BackgroundImage = {type?: 'BackgroundImage' | undefined, url: string, fillMode?: ImageFillMode | undefined, horizontalAlignment?: HorizontalAlignment | undefined, verticalAlignment?: VerticalAlignment | undefined}; export type VerticalContentAlignment = 'top' | 'center' | 'bottom' | string; export type AdaptiveCardRequest = {type: 'AdaptiveCard', version: string, body?: Array | undefined, actions?: Array | undefined, selectAction?: AdaptiveCardSelectAction | undefined, fallbackText?: string | undefined, backgroundImage?: (string | BackgroundImage) | undefined, minHeight?: string | undefined, speak?: string | undefined, lang?: ('en' | 'fr' | 'es') | undefined, verticalContentAlignment?: VerticalContentAlignment | undefined}; export type CreateApiTeamMessagingV1ChatsChatIdAdaptiveCardsArgs = {chatId: string, body: AdaptiveCardRequest}; export type CreateApiTeamMessagingV1ChatsChatIdAdaptiveCardsOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const adaptiveCardCreator = z.object({id: z.string().optional()}); export const adaptiveCardShortInfo = z.object({id: z.string().optional(), creationTime: z.string().optional(), lastModifiedTime: z.string().optional(), $schema: z.string().optional(), type: z.literal("AdaptiveCard").optional(), version: z.string().optional(), creator: adaptiveCardCreator.optional(), chatIds: z.array(z.string()).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Create Adaptive Card * * POST /team-messaging/v1/chats/{chatId}/adaptive-cards */ export const createApiTeamMessagingV1ChatsChatIdAdaptiveCards = async (args: CreateApiTeamMessagingV1ChatsChatIdAdaptiveCardsArgs, options?: CreateApiTeamMessagingV1ChatsChatIdAdaptiveCardsOptions) => { const {baseUrl = "https://platform.ringcentral.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/team-messaging/v1/chats/${encodeURIComponent(String(args.chatId))}/adaptive-cards${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(adaptiveCardShortInfo, await res.json()) };