"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { IncomingWebhook: () => IncomingWebhook, MicrosoftTeamsError: () => MicrosoftTeamsError, isMicrosoftTeamsError: () => isMicrosoftTeamsError }); module.exports = __toCommonJS(src_exports); // src/IncomingWebhook.ts var ACData = __toESM(require("adaptivecards-templating"), 1); var import_axios = __toESM(require("axios"), 1); // src/errors/MicrosoftTeamsError.ts var MicrosoftTeamsError = class _MicrosoftTeamsError extends Error { code; description; number; fileName; lineNumber; columnNumber; config; response; constructor(message, code) { super(message); this.name = "MicrosoftTeamsError"; this.code = code; if (Error.captureStackTrace) { Error.captureStackTrace(this, this.constructor); } else { this.stack = new Error().stack; } } toJSON() { return { // Standard message: this.message, name: this.name, // Microsoft description: this.description, number: this.number, // Mozilla fileName: this.fileName, lineNumber: this.lineNumber, columnNumber: this.columnNumber, stack: this.stack, // Axios code: this.code, status: this.response && this.response.status ? this.response.status : null }; } static from(error, code) { return new _MicrosoftTeamsError(error.message, code); } }; Object.defineProperty(MicrosoftTeamsError.prototype, "isMicrosoftTeamsError", { value: true }); // src/utils/tinyassert.ts function tinyassert(value, message) { if (value) { return; } if (message instanceof Error) { throw message; } throw new TinyAssertionError(message, tinyassert); } var TinyAssertionError = class extends Error { constructor(message, stackStartFunction) { super(message ?? "TinyAssertionError"); if (stackStartFunction && "captureStackTrace" in Error) { Error.captureStackTrace(this, stackStartFunction); } } }; // src/utils/url-deprecated-warning.ts var deprecatedURLs = ["webhook.office.com/webhookb2"]; async function showWebhookUrlDeprecatedWarning(url) { for (const deprecatedURL of deprecatedURLs) { if (url.includes(deprecatedURL)) { await import("chalk").then((_chalk) => { const chalk = _chalk.default; console.warn( ` ${chalk.yellowBright( "############# WARNING #############" )} ${chalk.yellowBright( "You are using a deprecated Webhook URL:" )} ${chalk.yellow(url)} Retirement of Office 365 connectors within Microsoft Teams. Create a new Workspace App and use the new URL with this library. ${chalk.gray( "Upgrade documentation: https://github.com/waigel/node-ms-teams-webhook/blob/master/MIGRATION.md" )} ${chalk.gray( "Original message from Microsoft: https://devblogs.microsoft.com/microsoft365dev/retirement-of-office-365-connectors-within-microsoft-teams" )} ` ); }); } } } // src/IncomingWebhook.ts var IncomingWebhook = class { /** * The webhook URL */ url; /** * Axios HTTP client instance used by this client */ axios; constructor(url) { if (url === void 0) { throw new Error("Incoming webhook URL is required"); } this.url = url; this.axios = import_axios.default.create({ baseURL: url, maxRedirects: 0, proxy: false }); } /** * Send a notification to a conversation * @param message the message (object describing the message) * @deprecated Microsoft plans to deprecate the old Webhook Connector by the end of December 2025. * Please switch to the new Workflow Apps. A migration tool and guide will be provided in the future. */ async send(message) { let payload = message; if (typeof payload === "string") { throw new Error( "Message must be a JSON object. Dont use a string or JSON.stringify() your message" ); } try { const response = await this.axios.post(this.url, payload); return this.buildResult(response).data; } catch (error) { return handleError(error); } } /** * Sends a plain text message to a team channel or conversation. * * @param text The text message to be sent. * @returns A promise that resolves when the message is sent successfully. */ async sendText(text) { tinyassert(text, "text cannot be empty or undefined"); tinyassert(typeof text === "string", "text must be a string"); showWebhookUrlDeprecatedWarning(this.url); return await this.sendPlainTextRequest(text); } /** * Sends an adaptive card to a team channel or conversation. * Use the Adaptive Card Designer (https://adaptivecards.io/designer) to create your card schema. * * This function takes a template payload and fills it with the provided data. Placeholders in the * template should be defined using ${variableName}, which will be replaced with values from the * data object. The variable names must match the keys in the data object. * * If you want to send multiple adaptive cards with different data, provide an array of data objects. * * @param templatePayload The adaptive card schema to be used as the template. * @param data The data object or array of data objects used to fill the adaptive card. If not provided, * the card will be sent as is. * @returns A promise that resolves when the card is sent successfully. */ async sendAdaptiveCard(templatePayload, data) { tinyassert( templatePayload, "adaptiveCard schema cannot be empty or undefined" ); tinyassert( templatePayload.type === "AdaptiveCard", "Only 'AdaptiveCard' type is supported" ); showWebhookUrlDeprecatedWarning(this.url); const templatePayloadData = Array.isArray(data) ? data : [data]; const cards = []; for (const data2 of templatePayloadData) { const template = new ACData.Template(templatePayload); const context = { $root: { ...data2 } }; cards.push(template.expand(context)); } if (cards.length === 0) { cards.push(new ACData.Template(templatePayload)); } return await this.sendPlainJSONRequest({ type: "message", summary: "Adaptive Card", attachments: cards.map((card) => ({ contentType: "application/vnd.microsoft.card.adaptive", content: card })) }); } /** * Sends a raw JSON payload to the incoming webhook. This method is generic and can be used for sending * any type of message payload, including adaptive cards. However, it is recommended to use the * `sendText` and `sendAdaptiveCard` methods for sending text and adaptive cards, respectively. * * Note: When sending an adaptive card, ensure that your card JSON is wrapped in the following structure: * { * "type": "message", * "attachments": [ * { * "contentType": "application/vnd.microsoft.card.adaptive", * "contentUrl": null, * "content": { >>> YOUR CARD HERE <<< } * } * ] * } * * @param card The raw JSON payload representing the message or card to be sent. * @returns A promise that resolves when the raw card is sent successfully. */ async sendRawAdaptiveCard(card) { return await this.sendPlainJSONRequest(card); } async sendPlainTextRequest(text) { try { const response = await this.axios.post(this.url, text, { headers: { "Content-Type": "text/plain" } }); return this.buildResult(response); } catch (error) { handleError(error); } } async sendPlainJSONRequest(jsonPayload) { try { const response = await this.axios.post(this.url, jsonPayload, { headers: { "Content-Type": "application/json" } }); return this.buildResult(response); } catch (error) { handleError(error); } } /** * Processes an HTTP response into an IncomingWebhookResult. */ buildResult(response) { return response; } }; function handleError(error) { if ((0, import_axios.isAxiosError)(error)) { const data = error.response?.data; const status = error.response?.status; if (data && status === 400) { if ("error" in data && "code" in data.error) { const microsoftErrorCode = data.error.code; const microsoftErrorMessage = data.error.message; throw new MicrosoftTeamsError( microsoftErrorMessage, microsoftErrorCode ); } } } throw error; } // src/errors/isMicrosoftTeamsError.ts function isMicrosoftTeamsError(payload) { return payload !== null && typeof payload === "object" && payload?.isMicrosoftTeamsError === true; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { IncomingWebhook, MicrosoftTeamsError, isMicrosoftTeamsError });