LoginSignup
0
0

More than 1 year has passed since last update.

【GAS】SlackのchannelIDを一括で取得する

Last updated at Posted at 2023-04-16

目的

Slackのユーザーに直接DMを送りたいため、channelIDを取得するGASを作成

前提条件

  • Slackの管理者であること
  • Bot User OAuth Token があること
  • Bot Token Scopes で下記の権限を追加していること

・channels:read
View basic information about public channels in a workspace

・chat:write
Send messages as @xxx

・chat:write.customize
Send messages as @xxx with a customized username and avatar

・commands
Add shortcuts and/or slash commands that people can use

・groups:write
Manage private channels that xxxx has been added to and create new ones

・im:read
View basic information about direct messages that xxxx has been added to

・im:write
Start direct messages with people

・links:write
Show previews of URLs in messages

・mpim:read
View basic information about group direct messages that xxxx has been added to

・mpim:write
Start group direct messages with people

・usergroups:read
View user groups in a workspace

users.profile:read
View profile details about people in a workspace

users:read
View people in a workspace

手順

1.新規にスプレッドシートを作成する。
2.slackという名前のシートを作成する。
3.「拡張機能」「AppScript」の順に開く
4.Bot User OAuth Tokenをプロパティに登録するスクリプトを作成する
https://qiita.com/80syokumotsu/items/46205cd71da5cd2d6ce1
5.Slackの情報を取得するスクリプトを作成
スクリプト名:slackget.gs

const slack_app_token4 = PropertiesService.getScriptProperties().getProperty("slack_token");

function getSlackUser() {

  const options = {
    "method" : "get",
    "contentType": "application/x-www-form-urlencoded",
    "payload" : { 
      "token": slack_app_token4
    }
  };


  const url = "https://slack.com/api/users.list";
  const response = UrlFetchApp.fetch(url, options);
  
  const members = JSON.parse(response).members;

  let arr = [];


  for (const member of members) {
    
    //削除済、botユーザー、Slackbotを除く
    if (!member.deleted && !member.is_bot && member.id !== "USLACKBOT") {

      let member_id = member.id;  
      let dispname = member.profile.display_name; //表示名
      let real_name = member.real_name; //氏名(※表示名ではない)
      let channelid = getChannelID_(member_id);
      console.log(dispname);
      
      arr.push([member_id,dispname,real_name,channelid]);
    }
    
  }


  //スプレッドシートに書き込み
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('slack');
  sheet.getRange(1,1,1,1).setValue("SlackID");
  sheet.getRange(1,2,1,1).setValue("表示名");
  sheet.getRange(1,3,1,1).setValue("ユーザー名");
  sheet.getRange(1,4,1,1).setValue("channelid");
  sheet.getRange(2, 1, sheet.getMaxRows()-1, 2).clearContent();
  sheet.getRange(2, 1, arr.length, arr[0].length).setValues(arr);
}


function getChannelID_(member_id) {
 
  const options = {
    "method" : "post",
    "contentType": "application/x-www-form-urlencoded",
    "payload" : {
      "token": slack_app_token4,
      "users": member_id
    }
  }
  
  //必要scope = im:write
  const url = 'https://slack.com/api/conversations.open';
  const response = UrlFetchApp.fetch(url, options);
  
  const obj = JSON.parse(response);
  console.log(obj);
  
  return obj.channel.id;
 
}

6.slackシートにslackのユーザーの情報が追記される
image.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0