Slack統合マッピングの作成

Slack統合マッピングの作成

POST integration_mappings/slack/:integration_mapping_id呼び出しを使用してマッピングを作成します。この呼び出しを動作させるには、box_itemパラメータとpartner_itemパラメータが必要です。これらのパラメータはそれぞれ、BoxフォルダとSlackチャンネルを示します。

マッピングを作成するには、このサービスアカウントを、マッピングされるフォルダの共同所有者のロールとして設定しておく必要があることに注意してください。エラーが発生した場合は、トラブルシューティングガイドを参照してください。

cURL
curl -X -L POST "https://api.box.com/2.0/integration_mappings/slack" \
     -H "Authorization: Bearer <ACCESS_TOKEN>" \
     -H 'Content-Type: application/json' \
     -d '{
          "partner_item": {
              "id": "C987654321",
              "type": "channel",
              "slack_workspace_id": "T5555555"
          },
          "box_item": {
              "id": "123456789",
              "type": "folder"
          }
      }'
Node
const mapping = await client.integrationMappings.createSlackIntegrationMapping({
	partner_item: {
		type: 'channel',
		id: 'C12378991223',
		slack_org_id: 'E1234567'
	},
	box_item: {
		id: '12345',
		type: 'folder',
	}
});
console.log(
    `Slack integration mapping with id ${mapping.id} was created`
);

作成されたマッピングのデフォルト設定を変更するオプションを指定できます。たとえば、is_access_management_disabledtrueに設定すると、コラボレーションの管理が無効になります。Slackのチャンネルメンバーはチャンネルフォルダのコラボレータにならず、1000人以上のメンバーがいるチャンネルに共有リンクは作成されません。

Box SDKによるSlack統合マッピングの作成

Box SDKを使用すると、SlackチャンネルとBoxフォルダのマッピングでのサービスアカウントによる共同所有者のコラボレーションなど、統合マッピングを自動的に作成できます。それには、次のスクリプトを使用します。

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);

async function postIntegrationMappingSlack(){

 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 level
   type: "channel"
  },
  box_item: {
   id: boxFolderId,
   type: "folder"
  }
 }, {
  headers: {
   'Authorization': `Bearer ${developerToken}`
  }
 });

}

function isPlaceholder(str){
 return str === '<PLACEHOLDER>';
}

async function addCoowner(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;
  }
 }
}

async function logServiceAccountId() {
 try {
  await postIntegrationMappingSlack();
 } catch (error) {
  console.log(`Replace the value of serviceAccountId with: ${error.response.data.context_info.service_account_id} and re-run the script.`)
 }
}

async function createSlackIntegrationMapping() {

 if(isPlaceholder(serviceAccountId)){
  await logServiceAccountId();
 } else {
  await addCoowner(serviceAccountId, boxFolderId);
  await postIntegrationMappingSlack();
 }

}

module.exports = { createSlackIntegrationMapping }

PLACEHOLDERをログに記録されたserviceAccountIdの値に必ず置き換えてください。