@sinch/mcp
Version:
Sinch MCP server
111 lines • 5.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendTemplateMessageHandler = exports.registerSendTemplateMessage = void 0;
const zod_1 = require("zod");
const types_1 = require("../../types");
const utils_1 = require("../../utils");
const conversation_service_helper_1 = require("./utils/conversation-service-helper");
const conversation_tools_helper_1 = require("./utils/conversation-tools-helper");
const send_message_builder_1 = require("./utils/send-message-builder");
const prompt_schemas_1 = require("./prompt-schemas");
const TOOL_KEY = 'sendTemplateMessage';
const TOOL_NAME = (0, conversation_tools_helper_1.getToolName)(TOOL_KEY);
const registerSendTemplateMessage = (server, tags) => {
if (!(0, conversation_tools_helper_1.shouldRegisterTool)(TOOL_KEY, tags))
return;
server.tool(TOOL_NAME, 'Send a template message to a contact on the specified channel. The contact can be a phone number in E.164 format, or the identifier for the specified channel.', {
recipient: prompt_schemas_1.Recipient,
templateId: zod_1.z.string().optional()
.describe('The ID (ULID format) of the omni-template template to use for sending the message.'),
language: zod_1.z.string().optional()
.describe('The language to use for the omni-template (BCP-47). If not set, the default language code will be used.'),
whatsAppTemplateName: zod_1.z.string().optional()
.describe('The name of the template to use for sending the message on WhatsApp specifically. At least one of templateId or templateName should be provided. If this is the template name, the message will be sent as a WhatsApp message, otherwise, it will be considered as an omni-channel message.'),
whatsAppTemplateLanguage: zod_1.z.string().optional()
.describe('The language to use for the WhatsApp template (BCP-47). It is mandatory is the templateName is provided.'),
parameters: zod_1.z.record(zod_1.z.string(), zod_1.z.string()).optional()
.describe('The parameters to use for the template. This is a key-value map where the key is the parameter name and the value is the parameter value.'),
channel: prompt_schemas_1.ConversationChannel,
appId: prompt_schemas_1.ConversationAppIdOverride,
sender: prompt_schemas_1.MessageSenderNumberOverride,
region: prompt_schemas_1.ConversationRegionOverride
}, exports.sendTemplateMessageHandler);
};
exports.registerSendTemplateMessage = registerSendTemplateMessage;
const sendTemplateMessageHandler = async ({ recipient, channel, templateId, language, whatsAppTemplateName, whatsAppTemplateLanguage, parameters, appId, sender, region }) => {
if (!whatsAppTemplateName && !templateId) {
return new types_1.PromptResponse('At least one of templateId or whatsAppTemplateName should be provided.').promptResponse;
}
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.getConversationClient)(TOOL_NAME);
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 requestBase = await (0, send_message_builder_1.buildMessageBase)(sinchClient, conversationAppId, recipient, channel, sender);
let templateMessage = {};
if (whatsAppTemplateName && whatsAppTemplateLanguage) {
const whatsappMessage = {
channel_template: {
WHATSAPP: {
template_id: whatsAppTemplateName,
language_code: whatsAppTemplateLanguage,
version: '',
parameters: {
...parameters
}
}
}
};
templateMessage = {
...templateMessage,
...whatsappMessage
};
}
if (templateId) {
const omniChannelMessage = {
omni_template: {
template_id: templateId,
version: 'latest',
parameters: {
...parameters
}
}
};
if (language) {
omniChannelMessage.omni_template.language_code = language;
}
templateMessage = {
...templateMessage,
...omniChannelMessage
};
}
const request = {
sendMessageRequestBody: {
...requestBase,
message: {
template_message: {
...templateMessage
}
}
}
};
let response;
let reply;
try {
response = await sinchClient.conversation.messages.sendTemplateMessage(request);
reply = `Template message submitted on channel ${channel}! The message ID is ${response.message_id}`;
}
catch (error) {
reply = `An error occurred when trying to send the text 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.sendTemplateMessageHandler = sendTemplateMessageHandler;
//# sourceMappingURL=send-template-message.js.map