POST integration_mappings/slack/:integration_mapping_id呼び出しを使用してマッピングを作成します。この呼び出しを動作させるには、box_itemパラメータとpartner_itemパラメータが必要です。これらのパラメータはそれぞれ、BoxフォルダとSlackチャンネルを示します。
const BoxSDK =require('box-node-sdk');const axios =require('axios');const integrationMappingsApiUrl ='https://api.box.com/2.0/integration_mappings/slack'const boxFolderId ='PASTE YOUR FOLDER ID HERE';const slackChannelId ='PASTE YOUR CHANNEL ID HERE';const slackOrgId ='PASTE YOUR SLACK ORG ID HERE (CHANGE TO WORKSPACE ID IF NECESSARY)';const developerToken ='PASTE YOUR DEVELOPER TOKEN HERE';let serviceAccountId ='<PLACEHOLDER>';const client = BoxSDK.getBasicClient(developerToken);asyncfunctionpostIntegrationMappingSlack(){return axios.post(integrationMappingsApiUrl,{partner_item:{id: slackChannelId,slack_org_id: slackOrgId,// change slack_org_id to slack_workspace_id if Box for Slack is installed on the workspace leveltype:"channel"},box_item:{id: boxFolderId,type:"folder"}},{headers:{'Authorization':`Bearer ${developerToken}`}});}functionisPlaceholder(str){return str ==='<PLACEHOLDER>';}asyncfunctionaddCoowner(serviceAccountId, folderId){try{await client.collaborations.createWithUserID(serviceAccountId, folderId,'co-owner')}catch(error){if(error.response.body.code ==='user_already_collaborator'){
console.log('Service account already collaborated in the co-owner role.')}else{throw error;}}}asyncfunctionlogServiceAccountId(){try{awaitpostIntegrationMappingSlack();}catch(error){
console.log(`Replace the value of serviceAccountId with: ${error.response.data.context_info.service_account_id} and re-run the script.`)}}asyncfunctioncreateSlackIntegrationMapping(){if(isPlaceholder(serviceAccountId)){awaitlogServiceAccountId();}else{awaitaddCoowner(serviceAccountId, boxFolderId);awaitpostIntegrationMappingSlack();}}
module.exports ={ createSlackIntegrationMapping }