UNPKG

@sinch/mcp

Version:

Sinch MCP server

166 lines 7.94 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.sendCardOrChoiceMessageHandler = exports.registerSendCardOrChoiceMessage = void 0; const zod_1 = require("zod"); const conversation_service_helper_1 = require("./utils/conversation-service-helper"); const send_message_builder_1 = require("./utils/send-message-builder"); const geocoding_1 = require("./utils/geocoding"); const prompt_schemas_1 = require("./prompt-schemas"); const utils_1 = require("../../utils"); const types_1 = require("../../types"); const callChoice = zod_1.z.object({ phone_number: zod_1.z.string(), title: zod_1.z.string() }).describe('Message for triggering a call. The phone number is in E.164 format, and the title is the text to display next to the call button.'); const locationAsCoordinates = zod_1.z.object({ lat: zod_1.z.number(), long: zod_1.z.number(), title: zod_1.z.string() }).describe('Message containing geographic location as coordinates. The coordinates are latitude and longitude, and the title is the text to display next to the location.'); const locationAsAddress = zod_1.z.object({ address: zod_1.z.string().nonempty() }).describe('Message containing a plain text address.'); const locationChoice = zod_1.z.union([locationAsAddress, locationAsCoordinates]) .describe('Message for sending a location. It can either be the plain text address that will be converted into latitude/longitude or directly the latitude/longitude coordinates if the user wants to send a specific location.'); const textChoice = zod_1.z.object({ text: zod_1.z.string() }).describe('Message containing only text. This is a simple text message that can be used as a choice.'); const urlChoice = zod_1.z.object({ url: zod_1.z.string().url(), title: zod_1.z.string() }).describe('Message containing a URL. The URL is the link to click on, and the title is the text to display next to the URL button.'); const choiceMessage = zod_1.z.union([ callChoice, locationChoice, textChoice, urlChoice ]).describe('Choice message that can contain a call, location, text or URL. Each choice can be a call message (phone number + title to display next to it), a location message (latitude/longitude or plain text address + title to display next to it), a text message or a URL message (the URL to click on + title to display next to it).'); const registerSendCardOrChoiceMessage = (server, tags) => { if (!(0, utils_1.hasMatchingTag)(['all', 'conversation', 'notification'], tags)) { return; } server.tool('send-choice-message', 'Send a choice message to the user. The choice message can contain up to 3 choices if not text or up to 10 message if text only. Each choice can be a call message (phone number + title to display next to it), a location message (latitude / longitude + title to display next to it), a text message or a URL message (the URL to click on + title to display next to it). The contact can be a phone number in E.164 format, or the identifier for the specified channel.', { recipient: prompt_schemas_1.Recipient, choiceContent: zod_1.z.array(choiceMessage).max(10).optional().describe('The list of choices to send to the user'), text: zod_1.z.string().describe('The text to be sent along the choice array'), mediaUrl: zod_1.z.string().optional().describe('The media URL to be sent along the choice array'), channel: prompt_schemas_1.ConversationChannel, appId: prompt_schemas_1.ConversationAppIdOverride, sender: prompt_schemas_1.MessageSenderNumberOverride, region: prompt_schemas_1.ConversationRegionOverride, }, exports.sendCardOrChoiceMessageHandler); }; exports.registerSendCardOrChoiceMessage = registerSendCardOrChoiceMessage; const sendCardOrChoiceMessageHandler = async ({ recipient, channel, choiceContent, text, mediaUrl, appId, sender, region }) => { const maybeAppId = (0, conversation_service_helper_1.getConversationAppId)(appId); if ((0, utils_1.isPromptResponse)(maybeAppId)) { return maybeAppId.promptResponse; } const conversationAppId = maybeAppId; const maybeClient = (0, conversation_service_helper_1.getConversationService)(); if ((0, utils_1.isPromptResponse)(maybeClient)) { return maybeClient.promptResponse; } const sinchClient = maybeClient; const conversationRegion = (0, conversation_service_helper_1.getConversationRegion)(region); sinchClient.conversation.setRegion(conversationRegion); const choices = []; for (const choice of choiceContent || []) { if ('phone_number' in choice && 'title' in choice) { choices.push({ call_message: { phone_number: choice.phone_number, title: choice.title } }); } else if ('lat' in choice && 'long' in choice && 'title' in choice) { choices.push({ location_message: { coordinates: { latitude: choice.lat, longitude: choice.long }, title: choice.title } }); } else if ('address' in choice) { const coordinates = await (0, geocoding_1.getLatitudeLongitudeFromAddress)(choice.address); choices.push({ location_message: { coordinates: { latitude: coordinates.latitude, longitude: coordinates.longitude }, title: coordinates.formattedAddress } }); } else if ('text' in choice) { choices.push({ text_message: { text: choice.text } }); } else if ('url' in choice && 'title' in choice) { choices.push({ url_message: { url: choice.url, title: choice.title } }); } } const requestBase = await (0, send_message_builder_1.buildMessageBase)(sinchClient, conversationAppId, recipient, channel, sender); let request; if (mediaUrl) { request = { sendMessageRequestBody: { ...requestBase, message: { card_message: { choices, title: text, media_message: { url: mediaUrl } } } } }; } else { request = { sendMessageRequestBody: { ...requestBase, message: { choice_message: { choices, text_message: { text } } } } }; } let response; let reply; try { if (mediaUrl) { response = await sinchClient.conversation.messages.sendCardMessage(request); } else { response = await sinchClient.conversation.messages.sendChoiceMessage(request); } reply = `${mediaUrl ? 'Card' : 'Choice'} message submitted on channel ${channel}! The message ID is ${response.message_id}`; } catch (error) { reply = `An error occurred when trying to send the ${mediaUrl ? 'card' : 'choice'} message: ${JSON.stringify(error)}. Are you sure you are using the right region to send your message? The current region is ${region}.`; } return new types_1.PromptResponse(reply).promptResponse; }; exports.sendCardOrChoiceMessageHandler = sendCardOrChoiceMessageHandler; //# sourceMappingURL=send-card-or-choice-message.js.map