5
8

More than 1 year has passed since last update.

Google Chat botがメンションするのに必要なUser IDを開発者コンソールに出力するスクリプト

Last updated at Posted at 2022-02-06

Google Chatのbotがユーザーにメンションするには、そのユーザーのUSER_IDを調べて、テキストメッセージに次のコマンドを埋め込む必要があります。

<users/USER_ID>

このUSER_IDを調べる方法を2つご紹介します。

Admin SDK API + Apps Scriptを使う

にあるやり方で、スクリプトエディターの左ペイン「サービス」に「Admin SDK API」を追加します。

ちなみに、オプションに viewType: 'domain_public' を指定しないと、組織内の情報であっても取得できないのでご注意ください。

const email = '探したいユーザーのメールアドレス';
const user = AdminDirectory.Users.get(email, { viewType: 'domain_public' });
console.log(user.id); // Google Chat の USER_ID

ブラウザーの開発者コンソールでスクリプトを実行する

https://chat.google.com で開発者コンソールを開き、次のスクリプトを実行して表示することもできます。

スペースを選択して、メンバー一覧を表示してから、次のスクリプトを実行します。

let users = {};
for (let div of document.querySelectorAll('div[data-member-id]')) {
  const name = div.dataset.name;
  const memberId = div.dataset.memberId.split('/')[2];
  if (name && memberId) users[name] = memberId;
}
console.log(users);

おまけ:スペースのメンバーをApps Scriptで取得するのが大変

Apps ScriptからGoogle ChatのREST APIを使うと、botがいるスペースのメンバーを取得できます(Google Chat は個人のアカウントをAPIで操作できないため、bot をスペースに追加しないといけません)。

いろいろと試したのですが、結構手間がかかるんですよね。

5
8
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
5
8