UNPKG

@microsoft/agents-hosting-extensions-teams

Version:

Microsoft 365 Agents SDK for JavaScript. Teams extensions

80 lines 3.61 kB
"use strict"; /** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TeamsAttachmentDownloader = void 0; const axios_1 = __importDefault(require("axios")); const zod_1 = require("zod"); /** * Downloads attachments from Teams using the bots access token. */ class TeamsAttachmentDownloader { constructor() { this._httpClient = axios_1.default.create(); } /** * Download any files relative to the current user's input. * @template TState - Type of the state object passed to the `TurnContext.turnState` method. * @param {TurnContext} context Context for the current turn of conversation. * @param {TState} state Application state for the current turn of conversation. * @returns {Promise<InputFile[]>} Promise that resolves to an array of downloaded input files. */ async downloadFiles(context, state) { var _a; // Filter out HTML attachments const attachments = (_a = context.activity.attachments) === null || _a === void 0 ? void 0 : _a.filter((a) => !a.contentType.startsWith('text/html')); if (!attachments || attachments.length === 0) { return Promise.resolve([]); } const connectorClient = context.turnState.get('connectorClient'); this._httpClient.defaults.headers = connectorClient.axiosInstance.defaults.headers; const files = []; for (const attachment of attachments) { const file = await this.downloadFile(attachment); if (file) { files.push(file); } } return files; } /** * @private * @param {Attachment} attachment - Attachment to download. * @param {string} accessToken - Access token to use for downloading. * @returns {Promise<InputFile>} - Promise that resolves to the downloaded input file. */ async downloadFile(attachment) { let inputFile; if (attachment.contentType === 'application/vnd.microsoft.teams.file.download.info') { const contentSchema = zod_1.z.object({ downloadUrl: zod_1.z.string() }); const contentValue = contentSchema.parse(attachment.content); const response = await this._httpClient.get(contentValue.downloadUrl, { responseType: 'arraybuffer' }); const content = Buffer.from(response.data, 'binary'); let contentType = attachment.contentType; if (contentType === 'image/*') { contentType = 'image/png'; } inputFile = { content, contentType, contentUrl: attachment.contentUrl }; } else if (attachment.contentType === 'image/*') { const response = await this._httpClient.get(attachment.contentUrl, { responseType: 'arraybuffer' }); const content = Buffer.from(response.data, 'binary'); inputFile = { content, contentType: attachment.contentType, contentUrl: attachment.contentUrl }; } else { inputFile = { content: Buffer.from(attachment.content), contentType: attachment.contentType, contentUrl: attachment.contentUrl }; } return inputFile; } } exports.TeamsAttachmentDownloader = TeamsAttachmentDownloader; //# sourceMappingURL=teamsAttachmentDownloader.js.map