LoginSignup
0
1

More than 1 year has passed since last update.

Workato:RubyコネクターでSlackのチャンネル名からチャンネルIDを取得する

Last updated at Posted at 2021-09-07

はじめに

WorkatoでSlackに関する何らかの処理を行う際に、Slackのチャンネル名に対応するチャンネルIDが必要となる場合があります。

Workatoではこのような処理をノーコードで実装することは出来ますが、実装の複雑化や実行タスク数の増加が生じるため、Rubyコネクターを利用するのが効果的です。

ここでは、Rubyコネクターによるチャンネル名からチャンネルIDを取得する手順についてご説明します。

前提条件

https://api.slack.com/ で以下の対応が事前に行われていること

  • Rubyコネクターで利用するSlackアプリのOAuthトークン(Bot User Token)が取得済みである
  • 上記OAuthトークンにおいては、Bot token scopesとして channels:read groups:read im:read mpim:read が有効になっている(許可されている)

手順

1.Rubyコネクターを追加します。

image.png

2.Inputに以下の変数を定義します。必要に応じて値をセットします。

image.png

変数
token SlackのOAuth(Bearer)トークン
channel チャンネルIDを取得したいSlackチャンネル名

3.Output schemaを以下のとおり定義します。

image.png

4.Codeへ以下のコードをセットします。


url = 'https://slack.com/api/conversations.list'
cursor = ''
is_last = false
is_exist = false

result = {
  id: '',
  channel: input['channel']
}

while ! is_last do
    res = get(url)
        .params(
          cursor: cursor,
          exclude_archived: 'true',
          limit: '100',
          types: 'public_channel, private_channel'
        )
        .headers(Authorization: 'Bearer ' + input['token'])
        .response_format_json

    res["channels"].each do | channel |
        if input['channel'] == channel['name']
          result['id'] = channel['id']
          is_last = true
          is_exist = true
        end
    end

    if res["response_metadata"]["next_cursor"].blank?
      is_last = true
    else
      cursor = res["response_metadata"]["next_cursor"]
    end
end

{
  id: result['id'],
  channel: input['channel'],
  exists: is_exist
}

なお、以下のようにCallable Recipe化していただきますと、様々なレシピで本処理を活用いただけます。
image.png

0
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
0
1