import {z} from 'zod' export type RoleTypes = 'user' | 'bot'; export type ChannelAccount = {id?: string | undefined, name?: string | undefined, role?: RoleTypes | undefined}; export type ActivityTypes = 'message' | 'contactRelationUpdate' | 'conversationUpdate' | 'typing' | 'ping' | 'endOfConversation' | 'event' | 'invoke' | 'deleteUserData' | 'messageUpdate' | 'messageDelete' | 'installationUpdate' | 'messageReaction' | 'suggestion' | 'trace'; export type ConversationAccount = {isGroup?: boolean | undefined, conversationType?: string | undefined, id?: string | undefined, name?: string | undefined, role?: RoleTypes | undefined}; export type TextFormatTypes = 'markdown' | 'plain' | 'xml'; export type AttachmentLayoutTypes = 'list' | 'carousel'; export type MessageReactionTypes = 'like' | 'plusOne'; export type MessageReaction = {type?: MessageReactionTypes | undefined}; export type InputHints = 'acceptingInput' | 'ignoringInput' | 'expectingInput'; export type ActionTypes = 'openUrl' | 'imBack' | 'postBack' | 'playAudio' | 'playVideo' | 'showImage' | 'downloadFile' | 'signin' | 'call' | 'payment' | 'messageBack'; export type CardAction = {type?: ActionTypes | undefined, title?: string | undefined, image?: string | undefined, text?: string | undefined, displayText?: string | undefined, value?: Record | undefined}; export type SuggestedActions = {to?: Array | undefined, actions?: Array | undefined}; export type Attachment = {contentType?: string | undefined, contentUrl?: string | undefined, content?: Record | undefined, name?: string | undefined, thumbnailUrl?: string | undefined}; export type Entity = {type?: string | undefined}; export type ConversationReference = {activityId?: string | undefined, user?: ChannelAccount | undefined, bot?: ChannelAccount | undefined, conversation?: ConversationAccount | undefined, channelId?: string | undefined, serviceUrl?: string | undefined}; export type EndOfConversationCodes = 'unknown' | 'completedSuccessfully' | 'userCancelled' | 'botTimedOut' | 'botIssuedInvalidMessage' | 'channelFailed'; export type TextHighlight = {text?: string | undefined, occurence?: number | undefined}; export type Activity = {type?: ActivityTypes | undefined, id?: string | undefined, timestamp?: string | undefined, localTimestamp?: string | undefined, serviceUrl?: string | undefined, channelId?: string | undefined, from?: ChannelAccount | undefined, conversation?: ConversationAccount | undefined, recipient?: ChannelAccount | undefined, textFormat?: TextFormatTypes | undefined, attachmentLayout?: AttachmentLayoutTypes | undefined, membersAdded?: Array | undefined, membersRemoved?: Array | undefined, reactionsAdded?: Array | undefined, reactionsRemoved?: Array | undefined, topicName?: string | undefined, historyDisclosed?: boolean | undefined, locale?: string | undefined, text?: string | undefined, speak?: string | undefined, inputHint?: InputHints | undefined, summary?: string | undefined, suggestedActions?: SuggestedActions | undefined, attachments?: Array | undefined, entities?: Array | undefined, channelData?: Record | undefined, action?: string | undefined, replyToId?: string | undefined, label?: string | undefined, valueType?: string | undefined, value?: Record | undefined, name?: string | undefined, relatesTo?: ConversationReference | undefined, code?: EndOfConversationCodes | undefined, expiration?: string | undefined, importance?: string | undefined, deliveryMode?: string | undefined, textHighlights?: Array | undefined}; export type ConversationParameters = {isGroup?: boolean | undefined, bot?: ChannelAccount | undefined, members?: Array | undefined, topicName?: string | undefined, activity?: Activity | undefined, channelData?: Record | undefined}; export type CreateApiV3ConversationsArgs = {body: ConversationParameters}; export type CreateApiV3ConversationsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const conversationResourceResponse = z.object({activityId: z.string().optional(), serviceUrl: z.string().optional(), id: 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 }; /** * CreateConversation * * POST /v3/conversations */ export const createApiV3Conversations = async (args: CreateApiV3ConversationsArgs, options?: CreateApiV3ConversationsOptions) => { const {baseUrl = "https://api.botframework.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v3/conversations${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(conversationResourceResponse, await res.json()) };