---
schema_version: "1"
id: 3689d90192624c01b15977a24b041a524ac1d7b28ec573a523283b078d8bf265
title: Slack Connector 1.0 の例
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/slack-connector/1.0/slack-connector-examples.md
canonical_url: https://docs.mulesoft.com/jp/slack-connector/1.0/slack-connector-examples
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 1cf74a92ffbbb4205a5e4a3a3a32765d8a3dcd3909027a1251cc5da61723b9d9
description: MuleSoft Documentation Site
release: "1.0"
latest: false
---
# Slack Connector 1.0 の例

次の例は、公開 Slack チャネルを作成してメッセージをチャネルに投稿する方法を示しています。

これらの例では、一部の項目値に変数を使用しています。次のいずれかを実行できます。

- コード内で変数を値に置き換える。
- プロパティファイルで各変数の値を指定しておき、コネクタ設定からそのファイルを参照する。

プロパティファイルの使い方が分からない場合は、​[「プロパティプレースホルダーの設定」](https://docs.mulesoft.com/mule-runtime/latest/mule-app-properties-to-configure)​を参照してください。

この例を試す前に、Anypoint Studio (Studio) にアクセスし、​**\[Mule Palette (Mule パレット)\]**​ ビューに Slack Connector のエントリが表示されていることを確認します。表示されない場合、​[「コネクタを Mule プロジェクトに追加する」](https://docs.mulesoft.com/jp/slack-connector/1.0/slack-connector-studio#add-connector-to-project)​の手順に従ってください。

## [](#チャネルの作成)チャネルの作成

次の例は、ユーザー固有の名前で公開 Slack チャネルを作成する方法を示しています。

次のスクリーンショットは、この例のアプリケーションフローを示しています。

![チャネルの作成の例で使用されるコンポーネントを示しているアプリケーションフロー](/mulesoft/current/ja-jp/slack-connector/1.0/_images/slack-connector-create-channel-flow.png)

Figure 1. チャネルの作成の例のアプリケーションフロー

この例を実装するには、新規 Mule プロジェクトを作成し、HTTP リスナー、Create Channel 操作、2 つの \[Transform Message\] コンポーネント、1 つの \[Logger (ロガー)\] コンポーネントを設定する必要があります。

### [](#始める前に)始める前に

この例では、接続設定と Slack アプリケーションの両方の ​`channels:write`​ スコープが必要です。

### [](#http-リスナーを設定する)HTTP リスナーを設定する

ローカルホストポート ​`8081`​ で ​`/create-channel`​ パスへのコールが行われたときに Mule フローを開始するように HTTP リスナーを設定します。

1. Studio で新しい Mule プロジェクトを作成します。
2. Studio で ​**\[HTTP\]**​ をクリックし、​**\[Listener\]**​ 操作をキャンバスにドラッグします。
3. \[Listener (リスナー)\] プロパティウィンドウで、​**\[Connector configuration (コネクタ設定)\]**​ 項目の横にある ​**\[+\]**​ をクリックしてグローバル要素を追加します。
4. デフォルトを受け入れます。
5. \[Listener (リスナー)\] プロパティウィンドウで、​**\[Path (パス)\]**​ 項目値を ​`/create-channel`​ に設定します。

### [](#最初の-transform-message-コンポーネントを追加する)最初の \[Transform Message\] コンポーネントを追加する

最初の \[Transform Message\] コンポーネントではチャネル名を入力として受け取り、名前を JSON 形式に変換します。

1. **\[Mule Palette (Mule パレット)\]**​ ビューで ​**\[Core (コア)\]**​ を選択して ​**\[Transform Message\]**​ コンポーネントを ​**\[Listener (リスナー)\]**​ の右側にドラッグします。
2. **\[Transform Message\]**​ コンポーネントの名前を ​`Channel Details`​ に変更します。
3. **\[Transform Message\]**​ コンポーネントをクリックし、チャネル名を出力に追加して、出力形式を ​`application/json`​ に設定します。

![チャネル名を JSON 形式で指定している \[Transform Message\] コンポーネント](_images/slack-connector-transform-channel-details.png)

Figure 2. チャネルの作成の例の最初の \[Transform Message\] コンポーネント

### [](#create-channel-操作を追加する)Create Channel 操作を追加する

Create Channel 操作では Slack に接続し、要求を ​`https://slack.com/api/conversations.create`​ エンドポイントに投稿します。

1. **\[Mule Palette (Mule パレット)\]**​ ビューから、​**\[Slack\]**​ を選択して ​**\[Create Channel\]**​ 操作を ​**\[Listener (リスナー)\]**​ の右側にドラッグします。
2. \[Search properties (検索プロパティ)\] ウィンドウで、​**\[Connector configuration (コネクタ設定)\]**​ 項目の横にある ​**\[+\]**​ をクリックしてグローバル要素を追加します。
3. 次の値を入力します。
    
    <table class="tableblock frame-all grid-all fit-content spread">
<colgroup>
<col>
<col>
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">項目</th>
<th class="tableblock halign-left valign-top">Value (値)</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Consumer Key (コンシューマーキー)</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>${consumer.key}</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Consumer Secret (コンシューマーシークレット)</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>${consumer.secret}</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Authorization url (認証 URL)</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>https://slack.com/oauth/authorize</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Access token url (アクセストークン URL)</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>https://slack.com/api/oauth.access</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">スコープ</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>channels:write groups:write im:write mpim:write users:read.email</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">Listener config (リスナー設定)</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>HTTP_Listener-config</code></p></td>
</tr>
</tbody>
</table>
4. \[Slack properties (Slack プロパティ)\] ウィンドウで、​**\[Conversationcreate content (Conversationcreate のコンテンツ)\]**​ 項目に「​`payload`​」と入力します。

### [](#2-つ目の-transform-message-コンポーネントを追加する)2 つ目の \[Transform Message\] コンポーネントを追加する

2 番目の \[Transform Message\] コンポーネントでは、​**Create Channel**​ 操作の結果を JSON 形式に変換します。

1. **\[Mule Palette (Mule パレット)\]**​ ビューで ​**\[Core (コア)\]**​ を選択して ​**\[Transform Message\]**​ コンポーネントを ​**\[Listener (リスナー)\]**​ の右側にドラッグします。
2. **\[Transform Message\]**​ コンポーネントの名前を ​`Payload as JSON`​ に変更します。
3. 新しい ​**\[Transform Message\]**​ コンポーネントをクリックして、出力を ​`application/json`​ に設定します。
    
    ![Create Channel 操作の出力を JSON 形式に変換する \[Transform Message\] コンポーネント](_images/slack-connector-transform-channel-payload.png)
    
    Figure 3. チャネルの作成の例の 2 番目の \[Transform Message\] コンポーネント

### [](#logger-ロガー-コンポーネントを追加する)\[Logger (ロガー)\] コンポーネントを追加する

\[Logger (ロガー)\] コンポーネントでは、クエリ結果がコンソールに記録されます。

1. **\[Mule Palette (Mule パレット)\]**​ ビューで ​**\[Core (コア)\]**​ を選択し、​**\[Logger (ロガー)\]**​ コンポーネントを選択します。
2. **\[Logger (ロガー)\]**​ コンポーネントを設定します。
3. プロジェクトを保存します。

### [](#アプリケーションを実行する)アプリケーションを実行する

アプリケーションを実行する手順は、次のとおりです。

1. Mule アプリケーションを起動します。
2. 要求を送信するには、OAuth ダンスを実行する必要があります。ブラウザーを開き、​`[http://localhost:8082/authorize](http://localhost:8082/authorize)`​ に移動して、アプリケーションにアクセスできるようにします。
3. POST 要求を ​`http://localhost:8081/account?name="your channel name"`​ に送信します。

### [](#この例の-xml-コード)この例の XML コード

この例のフローをすばやく Mule アプリケーションに読み込むには、次のコードを Studio XML エディターに貼り付けます。

```xml
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
	xmlns:slack="http://www.mulesoft.org/schema/mule/slack"
	xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/slack http://www.mulesoft.org/schema/mule/slack/current/mule-slack.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
	<slack:config name="Slack_Connector_Config" doc:name="Slack Connector Config"  >
		<slack:slack-auth-connection >
			<slack:oauth-authorization-code consumerKey="${consumer.key}" consumerSecret="${consumer.secret}" scopes="channels:write groups:write im:write mpim:write users:read.email "/>
			<slack:oauth-callback-config listenerConfig="HTTP_Listener_config" callbackPath="/callback" authorizePath="/authorize" externalCallbackUrl="http://localhost:8081/callback"/>
		</slack:slack-auth-connection>
	</slack:config>
	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" >
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
	<configuration-properties doc:name="Configuration properties" file="application.properties" />
	<flow name="create-channel-flow" >
		<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/create-channel"/>
		<ee:transform doc:name="Channel Details" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
	name: "your-channel-name"
}]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<slack:create-conversationscreate doc:name="Create Channel" config-ref="Slack_Connector_Config"/>
		<ee:transform doc:name="Payload as Json" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<logger level="INFO" doc:name="Result" message="#[payload]"/>
	</flow>
  <flow name="send-message-to-channel-flow" >
		<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/send-message"/>
		<ee:transform doc:name="Message Details" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
	channel: "your-existing-channel",
	text: "Your text goes here"
}]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<slack:create-chatpost-message doc:name="Send Message" config-ref="Slack_Connector_Config"/>
		<ee:transform doc:name="Payload as Json" >
			<ee:message>
				<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<logger level="INFO" doc:name="Result" message="#[payload]"/>
	</flow>
</mule>
```

## [](#チャネルへのメッセージの投稿)チャネルへのメッセージの投稿

次の例では、メッセージを公開、非公開、またはダイレクトメッセージ (IM) チャネルに投稿する方法を示しています。

次のスクリーンショットは、この例のアプリケーションフローを示しています。

![チャネルへのメッセージの投稿の例で使用されるコンポーネントを示しているアプリケーションフロー](/mulesoft/current/ja-jp/slack-connector/1.0/_images/slack-connector-send-message-flow.png)

Figure 4. チャネルへのメッセージの投稿の例のアプリケーションフロー

### [](#始める前に-2)始める前に

この例では、接続設定と Slack アプリケーションの両方の次のスコープが必要です。

- `chat:write`
- `chat:write:user`
- `chat:write:bot`

### [](#コンポーネント)コンポーネント

この例では、次のコンポーネントを使用します。

- HTTP Listener (HTTP リスナー)
    
    ローカルホストポート 8081 で ​`/send/message`​ パスへのコールが行われたときに Mule フローを開始します。
- 最初の \[Transform Message\] コンポーネント
    
    対象チャネルを指定し、Send Message 操作のメッセージコンテンツを提供します。
- Send Message 操作
    
    Slack に接続し、メッセージを指定されたチャネルに投稿します。
- 2 番目の \[Transform Message\] コンポーネント
    
    Send Message 操作の結果を JSON 形式で出力します。
- ロガー
    
    Send Message 操作の結果をコンソールに記録します。

### [](#この例の-xml-コード-2)この例の XML コード

この例のフローをすばやく Mule アプリケーションに読み込むには、次のコードを Studio XML エディターに貼り付けます。

```xml
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:ee="http://www.mulesoft.org/schema/mule/ee/core"
	xmlns:slack="http://www.mulesoft.org/schema/mule/slack"
	xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/slack http://www.mulesoft.org/schema/mule/slack/current/mule-slack.xsd
http://www.mulesoft.org/schema/mule/ee/core http://www.mulesoft.org/schema/mule/ee/core/current/mule-ee.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
	<slack:config name="Slack_Connector_Config" doc:name="Slack Connector Config" >
		<slack:slack-auth-connection >
			<slack:oauth-authorization-code consumerKey="${consumer.key}" consumerSecret="${consumer.secret}" scopes="channels:write groups:write im:write mpim:write users:read.email "/>
			<slack:oauth-callback-config listenerConfig="HTTP_Listener_config" callbackPath="/callback" authorizePath="/authorize" externalCallbackUrl="http://localhost:8081/callback"/>
		</slack:slack-auth-connection>
	</slack:config>
	<http:listener-config name="HTTP_Listener_config" doc:name="HTTP Listener config" >
		<http:listener-connection host="0.0.0.0" port="8081" />
	</http:listener-config>
	<configuration-properties doc:name="Configuration properties" file="application.properties" />
	<flow name="find-user-by-email-flow"  >
		<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="find-by-email"/>
		<slack:get-userslookup-by-email doc:name="Find User by Email" config-ref="Slack_Connector_Config" email="example@emailaddress.com"/>
		<ee:transform doc:name="Payload as Json" >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<logger level="INFO" doc:name="Lookup Result" message="#[payload]"/>
	</flow>
  <flow name="send-message-to-channel-flow" >
		<http:listener doc:name="Listener" config-ref="HTTP_Listener_config" path="/send-message"/>
		<ee:transform doc:name="Message Details"  >
			<ee:message >
				<ee:set-payload ><![CDATA[%dw 2.0
output application/json
---
{
	channel: "your-existing-channel",
	text: "Your text goes here"
}]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<slack:create-chatpost-message doc:name="Send Message" config-ref="Slack_Connector_Config"/>
		<ee:transform doc:name="Payload as Json" >
			<ee:message>
				<ee:set-payload><![CDATA[%dw 2.0
output application/json
---
payload]]></ee:set-payload>
			</ee:message>
		</ee:transform>
		<logger level="INFO" doc:name="Result" message="#[payload]"/>
	</flow>
</mule>
```

## [](#関連情報)関連情報

- [Anypoint Connector の概要](https://docs.mulesoft.com/connectors/introduction/introduction-to-anypoint-connectors)
- [MuleSoft ヘルプセンター](https://help.mulesoft.com)