LoginSignup
13
5

More than 1 year has passed since last update.

Gather.townで定期的にスペースにいる人数をSlack通知するGASを作る方法

Last updated at Posted at 2023-03-17

実現すること

GASを用いて、定期的にGather.townのスペースにいるアクティブユーザーを取得し、Slackの特定チャンネルに通知する。

事前準備 - SlackAPIの設定/GatherApiKey

全体の流れ

  1. ManifestからSlackAppを作成する
  2. Settings > Install App > Install To Workspaceでワークスペースにインストールする
  3. Settings > Install App > Bot User OAuth Tokenをメモしておく
  4. ここからGatherのApiKeyを取得する

ManifestからSlackAppを作成する

Manifest

{
    "display_information": {
        "name": "Gather通知アプリ",
        "description": "毎日Gather.townにいるアクティブユーザーを通知します。",
        "background_color": "#1f1f1f"
    },
    "features": {
        "bot_user": {
            "display_name": "GatherNotification",
            "always_online": false
        }
    },
    "oauth_config": {
        "scopes": {
            "bot": [
                "chat:write"
            ]
        }
    },
    "settings": {
        "org_deploy_enabled": false,
        "socket_mode_enabled": false,
        "token_rotation_enabled": false
    }
}

対象ワークスペースを選ぶ

CleanShot 2023-03-17 at 21.10.15.png

manifestをコピペ

CleanShot 2023-03-17 at 21.10.24.png

Createする

CleanShot 2023-03-17 at 21.10.34.png

ワークスペースにインストールする

CleanShot 2023-03-17 at 21.11.28.png

SlackBotTokenを取得する

CleanShot 2023-03-17 at 21.13.13.png

Gather.townのApiKeyを取得する

こちらのリンクからGatherにログインしたことがあるのであれば、ApiKeyを取得することができます。

実装

GAS準備

  1. ライブラリ > スクリプトIDに以下を入力
    1. 1on93YOYfSmV92R5q59NpKmsyWIQD8qnoLYk-gkQBI92C58SPyA2x1-bq
  2. 検索を押下
  3. IDはSlackAppのまま、追加を押下

ライブラリをメニューから選択

CleanShot 2023-03-17 at 21.15.16.png

スクリプトIDを入力し、ライブラリを読み込む

CleanShot 2023-03-17 at 21.16.05.png

追加

CleanShot 2023-03-17 at 21.16.10.png

GASのコード全体

ソースコードは単純に現在接続数の数を返すだけになっています。ここに人数によって、フレーバーテキストを変えたり、GatherのスペースURLを貼るなどして工夫してみましょう!

function main() {
  const activeUserNow = getActiveUserCount();
  const channelId = PropertiesService.getScriptProperties().getProperty("slackChannelId");
  sendSlack(channelId, activeUserNow);
}

function getActiveUserCount() {
  // APIからスペース情報を取得
  const url = "https://api.gather.town/api/v2/users/me/owned-spaces";
  const headers = {
    "apiKey": PropertiesService.getScriptProperties().getProperty("API_KEY")
  };
  const response = UrlFetchApp.fetch(url, {
    "headers": headers
  });
  const data = JSON.parse(response.getContentText());

  // スペースIDを指定
  const spaceId = 'スペースID' // console.log(data)をして出てきたお目当てのスペースIDをコピペするのがオススメ
  // スペースの人数を取得
  const workSpace = data[spaceId]
  const activeUserCount = workSpace['numActive'];
  return activeUserCount;
}

function sendSlack(channel, message) {
  const slackToken = PropertiesService.getScriptProperties().getProperty('slackBotToken');
  const slackApp = SlackApp.create(slackToken);
  slackApp.postMessage(channel, String(message));
}

スクリプトプロパティの設定

Apps Scriptのプロジェクトの設定からスクリプトプロパティを設定します。

CleanShot 2023-03-17 at 21.34.50.png

スクリプトプロパティを設定します。

  • API_KEY
    • GatherのApiKey
  • slackBotToken
    • OAuth Bot User Tokenのもの
  • slackChannelId
    • Slackのチャンネル設定の一番下にあるところから取得することができます

CleanShot 2023-03-17 at 21.37.35 1.png

CleanShot 2023-03-17 at 21.34.57.png

アプリを対象チャンネルに追加する

作成したアプリをslackChannelIdで指定したチャンネルに追加してあげてください!

CleanShot 2023-03-17 at 22.12.56.png

CleanShot 2023-03-17 at 22.13.13.png

動作確認

色々権限を与える必要があるので、流れに沿って権限付与してあげてください!

日時実行設定

毎日お昼ごろに通知してほしいのでトリガーを設定します。

CleanShot 2023-03-17 at 21.33.46.png

CleanShot 2023-03-17 at 21.32.24.png

おわりに

Gather.townをより便利に使うことで、リモートワーク下でもコミュニケーションハブを作ることができます。GatherのようにバーチャルオフィスツールはDiscordやGoogle Meetとは異なり、楽しく、アバターを通して「そこにいる」ような感覚を実現することができます。

今回は手順に従うだけで簡単にできるように意識して書きました。ぜひ使ってみてください。

参考記事

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