@sinch/mcp
Version:
Sinch MCP server
78 lines • 3.99 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()
.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.'),
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, parameters, 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.getConversationClient)(TOOL_NAME);
if ((0, utils_1.isPromptResponse)(maybeClient)) {
return maybeClient.promptResponse;
}
const sinchClient = maybeClient;
(0, conversation_service_helper_1.setConversationRegion)(region, sinchClient);
const requestBase = await (0, send_message_builder_1.buildMessageBase)(sinchClient, conversationAppId, recipient, channel, sender);
const omniChannelMessage = {
omni_template: {
template_id: templateId,
version: 'latest',
parameters: {
...parameters
}
}
};
if (language) {
omniChannelMessage.omni_template.language_code = language;
}
const request = {
sendMessageRequestBody: {
...requestBase,
message: {
template_message: {
...omniChannelMessage
}
}
}
};
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