> ## Documentation Index
> Fetch the complete documentation index at: https://developer.box.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Slack統合マッピングの作成

export const Link = ({href, children, className, ...props}) => {
  const localizedHref = localizeLink(href);
  return <a href={localizedHref} className={className} {...props}>
      {children}
    </a>;
};

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

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

<CodeGroup>
  ```sh cURL theme={null}
  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"
            }
        }'
  ```

  ```typescript Node/TypeScript v10 theme={null}
  await userClient.integrationMappings.createSlackIntegrationMapping({
    partnerItem: new IntegrationMappingPartnerItemSlack({
      id: slackPartnerItemId,
      slackOrgId: slackOrgId,
    }),
    boxItem: new IntegrationMappingBoxItemSlack({ id: folder.id }),
  } satisfies IntegrationMappingSlackCreateRequest);
  ```

  ```python Python v10 theme={null}
  user_client.integration_mappings.create_slack_integration_mapping(
      IntegrationMappingPartnerItemSlack(
          id=slack_partner_item_id, slack_org_id=slack_org_id
      ),
      IntegrationMappingBoxItemSlack(id=folder.id),
  )
  ```

  ```cs .NET v10 theme={null}
  await userClient.IntegrationMappings.CreateSlackIntegrationMappingAsync(requestBody: new IntegrationMappingSlackCreateRequest(partnerItem: new IntegrationMappingPartnerItemSlack(id: slackPartnerItemId) { SlackOrgId = slackOrgId }, boxItem: new IntegrationMappingBoxItemSlack(id: folder.Id)));
  ```

  ```swift Swift v10 theme={null}
  try await userClient.integrationMappings.createSlackIntegrationMapping(requestBody: IntegrationMappingSlackCreateRequest(partnerItem: IntegrationMappingPartnerItemSlack(id: slackPartnerItemId, slackOrgId: slackOrgId), boxItem: IntegrationMappingBoxItemSlack(id: folder.id)))
  ```

  ```java Java v10 theme={null}
  userClient.getIntegrationMappings().createSlackIntegrationMapping(new IntegrationMappingSlackCreateRequest(new IntegrationMappingPartnerItemSlack.Builder(slackPartnerItemId).slackOrgId(slackOrgId).build(), new IntegrationMappingBoxItemSlack(folder.getId())))
  ```

  ```javascript Node v4 theme={null}
  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`
  );
  ```
</CodeGroup>

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

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

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

```js theme={null}
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 }
```

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