UNPKG

react-native-feedback-hub

Version:

React Native feedback SDK with Slack, Jira, Discord and Microsoft Teams integration

116 lines 5.2 kB
import axios from 'axios'; import { readFile } from 'react-native-fs'; import { convertToBytes } from '../utils/convertToBuytes'; import { TEAMS_API_ENDPOINTS } from '../utils/endpoints'; import { getFileNameAndType } from '../utils/getFileNameAndType'; export const sendToTeams = async (payload, config) => { var _a, _b, _c, _d, _e; const { title, message, type, screenshot, video } = payload; const { accessToken, teamId, channelId } = config; const fileUris = [screenshot, video].filter(Boolean); const uploadedFiles = []; try { // 1. Get filesFolder and driveId let filesFolderId; let driveId; try { const channelRes = await axios.get(TEAMS_API_ENDPOINTS.getFilesFolder(teamId, channelId), { headers: { Authorization: `Bearer ${accessToken}`, }, }); filesFolderId = (_a = channelRes === null || channelRes === void 0 ? void 0 : channelRes.data) === null || _a === void 0 ? void 0 : _a.id; driveId = (_c = (_b = channelRes === null || channelRes === void 0 ? void 0 : channelRes.data) === null || _b === void 0 ? void 0 : _b.parentReference) === null || _c === void 0 ? void 0 : _c.driveId; if (!filesFolderId || !driveId) { throw new Error('filesFolder or driveId not found — ensure channel file storage is initialized.'); } } catch (metaErr) { console.warn('[Teams] Could not fetch channel metadata:', ((_d = metaErr.response) === null || _d === void 0 ? void 0 : _d.data) || metaErr.message); throw metaErr; } // 2. Upload each file for (const uri of fileUris) { const { fileName, fileType } = getFileNameAndType(uri); try { const sessionRes = await axios.post(TEAMS_API_ENDPOINTS.createUploadSession(driveId, filesFolderId, fileName), { item: { '@microsoft.graph.conflictBehavior': 'rename', name: fileName, }, }, { headers: { Authorization: `Bearer ${accessToken}`, 'Content-Type': 'application/json', }, }); const uploadUrl = sessionRes.data.uploadUrl; const fileBase64 = await readFile(uri, 'base64'); const fileBuffer = convertToBytes(fileBase64); await axios.put(uploadUrl, fileBuffer, { headers: { 'Content-Length': fileBuffer.length, 'Content-Range': `bytes 0-${fileBuffer.length - 1}/${fileBuffer.length}`, 'Content-Type': fileType, }, }); const fileMeta = await axios.get(TEAMS_API_ENDPOINTS.getFileMeta(driveId, filesFolderId, fileName), { headers: { Authorization: `Bearer ${accessToken}`, }, }); uploadedFiles.push({ name: fileMeta.data.name, webUrl: fileMeta.data.webUrl, }); console.log('[Teams] Uploaded:', fileName); } catch (uploadError) { console.error('[Teams] Upload failed for:', fileName); console.error(((_e = uploadError.response) === null || _e === void 0 ? void 0 : _e.data) || uploadError.message); } } // 3. Send main feedback message const mainContent = ` <b>${type.toUpperCase()}</b><br/><br/> <b>Title:</b> ${title}<br/><br/> <b>Details:</b> ${message}<br/> `; const messageRes = await axios.post(TEAMS_API_ENDPOINTS.postMessage(teamId, channelId), { body: { contentType: 'html', content: mainContent }, }, { headers: { Authorization: `Bearer ${accessToken}`, 'Content-Type': 'application/json', }, }); const messageId = messageRes.data.id; // 4. Threaded reply with file links if (uploadedFiles.length > 0) { const fileLinks = uploadedFiles .map(f => `• <a href="${f.webUrl}" target="_blank">${f.name}</a>`) .join('<br/>'); const replyMessage = `<b>Attachments:</b><br/>${fileLinks}`; await axios.post(TEAMS_API_ENDPOINTS.postReply(teamId, channelId, messageId), { body: { contentType: 'html', content: replyMessage }, }, { headers: { Authorization: `Bearer ${accessToken}`, 'Content-Type': 'application/json', }, }); } console.log('[Teams] Feedback message and file links sent.'); } catch (err) { console.error('[Teams] Failed to send feedback:', err.message || err); throw err; } }; //# sourceMappingURL=teams.js.map