1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[GAS] Chatwork の指定グループに属するユーザーIDをすべて取得する

Posted at

Chatwork のユーザーIDを取得するスクリプト

  • 目的: Chatwork の指定したチャットルームに所属しているメンバーのユーザーIDを取得する。
  • API: Chatwork APIを使用
  • 参考URL: Chatwork API ドキュメント

スクリプト

/**
 * Chatwork のユーザーIDを取得する
 * 
 * 参考URL(公式APIドキュメント):
 * https://developer.chatwork.com/docs 
 *
 */
function getAllChatworkIds() {

  // チャットルームのIDを定義
  const room_ids = {
    'チャットグループ1': '000000000',
    'チャットグループ2': '111111111',
  };

  // 各ルームIDに対してメンバーIDを取得
  for (let key in room_ids) {
    console.log("===== " + key + " =====");
    getChatworkId(room_ids[key]);
  }
}

function getChatworkId(room_id) {

  api_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
  end_point = 'https://api.chatwork.com/v2/rooms/' + room_id + '/members';

  const options = {
    method: 'GET',
    headers: {
      accept: 'application/json',
      'x-chatworktoken': api_token
    }
  };

  let response = UrlFetchApp.fetch('https://api.chatwork.com/v2/rooms/' + room_id + '/members', options).getContentText();

  response = unicodeUnescape(response);
  response = response.replace(/,/g, ",\n\t");
  response = response.replace(/{/g, "\n{");
  Logger.log(response);
  
}
/**
 * Unicode(¥uxxx形式)を文字列へアンエスケープ
 * @param {string} str エスケープされた文字を含む文字列
 * @return 文字列へアンエスケープされた文字列を返す
 */
function unicodeUnescape(str) {
  let ret = str;
  let matches = str.match(/\\u.{4}/gi);
  for (let ii = 0; ii < matches.length; ii++) {
    let new_str = String.fromCharCode(matches[ii].replace("\\u", "0x"));
    let old_str = matches[ii];
    ret = ret.replace(old_str, new_str);
  }
  return ret;
};
1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?