Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature][Connector-V2][Slack] Add Slack sink connector #3226

Merged
merged 40 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
7014a51
[Doc]Fix typos in QuickStart Doc
lianghuan-xatu Oct 12, 2022
9b9df0a
Merge branch 'apache:dev' into dev
lianghuan-xatu Oct 21, 2022
e000574
Merge branch 'apache:dev' into dev
lianghuan-xatu Oct 26, 2022
8b27692
[Feature][Connector-V2][Slack] Add Slack sink connector
lianghuan-xatu Oct 29, 2022
d950956
[Feature][Connector-V2][Slack] Update pom and Add dependency in seatu…
lianghuan-xatu Oct 29, 2022
c550f0d
[Feature][Connector-V2][Slack] Update change log
lianghuan-xatu Oct 30, 2022
1ba3084
[Feature][Connector-V2][Slack] Update log info
lianghuan-xatu Oct 31, 2022
2d883ec
[Feature][Connector-V2][Slack] Remove unrelative dependience update
lianghuan-xatu Nov 4, 2022
ee498f8
[Feature][Connector-V2][Slack] Remove unrelative dependience update
lianghuan-xatu Nov 4, 2022
952927c
Merge remote-tracking branch 'origin/connector-slack-branch' into con…
lianghuan-xatu Nov 4, 2022
51baf1e
[Feature][Connector-V2][Slack] Upate file plugin-mapping.properties
lianghuan-xatu Nov 4, 2022
6385c38
[Feature][Connector-V2][Slack] Upate file plugin-mapping.properties
lianghuan-xatu Nov 4, 2022
4f4cc7a
Merge remote-tracking branch 'upstream/dev' into connector-slack-branch
lianghuan-xatu Nov 4, 2022
9b53e47
[Feature][Connector-V2][Slack] Fix conversationID bug
lianghuan-xatu Nov 4, 2022
7e0d71b
[Feature][Connector-V2][Slack] Fix code style
lianghuan-xatu Nov 4, 2022
76d4610
[Feature][Connector-V2][Slack] Fix code style
lianghuan-xatu Nov 6, 2022
4487f32
[Feature][Connector-V2][Slack] Fix NPE bug
lianghuan-xatu Nov 6, 2022
f09092b
[Feature][Connector-V2][Slack] Fix Code Style
lianghuan-xatu Nov 9, 2022
280d4f5
[Feature][Connector-V2][Slack] Resolve Branch Comflicts
lianghuan-xatu Nov 9, 2022
471e733
[Feature][Connector-V2][Slack] Update plugin-mapping.properties
lianghuan-xatu Nov 9, 2022
9173702
[Feature][Connector-V2][Slack] Update plugin-mapping.properties
lianghuan-xatu Nov 9, 2022
4a9c82d
[Feature][Connector-V2][Slack] Update pom.xml
lianghuan-xatu Nov 9, 2022
a76f4b3
[Feature][Connector-V2][Slack] Update pom.xml
lianghuan-xatu Nov 9, 2022
b6ab1b4
[Feature][Connector-V2][Slack] Update pom.xml
lianghuan-xatu Nov 9, 2022
1667aaa
Merge remote-tracking branch 'origin/connector-slack-branch' into con…
lianghuan-xatu Nov 9, 2022
8b337a7
[Feature][Connector-V2][Slack] Fix Code Style
lianghuan-xatu Nov 9, 2022
b9e72b3
Merge remote-tracking branch 'origin/connector-slack-branch' into con…
lianghuan-xatu Nov 9, 2022
7bde171
[Feature][Connector-V2][Slack] Fix Code Style
lianghuan-xatu Nov 9, 2022
8ec3500
Merge branch 'connector-slack-branch' of https://github.com/183917134…
lianghuan-xatu Nov 9, 2022
8d0cd31
[Feature][Connector-V2][Slack] Add slack dependency
lianghuan-xatu Nov 11, 2022
3104840
[Feature][Connector-V2][Slack] Remove duplicate parameters check
lianghuan-xatu Nov 15, 2022
369172c
Merge branch 'dev' of https://github.com/apache/incubator-seatunnel i…
lianghuan-xatu Nov 15, 2022
3900bb7
[Feature][Connector-V2][Slack] fix code style
lianghuan-xatu Nov 15, 2022
108c9dc
Merge branch 'dev' of https://github.com/apache/incubator-seatunnel i…
lianghuan-xatu Nov 16, 2022
68dac35
[Feature][Connector-V2][Slack] Implement Factory for connectors
lianghuan-xatu Nov 19, 2022
7ff9f3d
Merge branch 'dev' of https://github.com/apache/incubator-seatunnel i…
lianghuan-xatu Nov 19, 2022
6de19f1
[Feature][Connector-V2][Slack] Fix code style
lianghuan-xatu Nov 19, 2022
7ec9f5f
[Feature][Connector-V2][Slack] Fix code style
lianghuan-xatu Nov 19, 2022
a8c6423
[Feature][Connector-V2][Slack] Add Slack Error-Quick-Reference-Manual
lianghuan-xatu Nov 19, 2022
d67353c
Merge branch 'dev' into connector-slack-branch
EricJoy2048 Nov 21, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions docs/en/connector-v2/sink/Slack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Slack

> Slack sink connector

## Description

Used to send data to Slack Channel. Both support streaming and batch mode.
> For example, if the data from upstream is [`age: 12, name: huan`], the content send to socket server is the following: `{"name":"huan","age":17}`


## Key features

- [ ] [exactly-once](../../concept/connector-v2-features.md)
- [ ] [schema projection](../../concept/connector-v2-features.md)

## Options

| name | type | required | default value |
| -------------- |--------|----------|---------------|
| webhooks_url | String | Yes | - |
| oauth_token | String | Yes | - |
| slack_channel | String | Yes | - |
| common-options | | no | - |

### webhooks_url [string]

Slack webhook url

### oauth_token [string]

Slack oauth token used for the actual authentication

### slack_channel [string]

slack channel for data write

### common options

Sink plugin common parameters, please refer to [Sink Common Options](common-options.md) for details

## Example

```hocon
sink {
SlackSink {
webhooks_url = "https://hooks.slack.com/services/xxxxxxxxxxxx/xxxxxxxxxxxx/xxxxxxxxxxxxxxxx"
oauth_token = "xoxp-xxxxxxxxxx-xxxxxxxx-xxxxxxxxx-xxxxxxxxxxx"
slack_channel = "channel name"
}
}
```

## Changelog

### new version

- Add Slack Sink Connector
3 changes: 2 additions & 1 deletion plugin-mapping.properties
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,5 @@ seatunnel.sink.MongoDB = connector-mongodb
seatunnel.source.Iceberg = connector-iceberg
seatunnel.source.InfluxDB = connector-influxdb
seatunnel.source.S3File = connector-file-s3
seatunnel.sink.S3File = connector-file-s3
seatunnel.sink.S3File = connector-file-s3
seatunnel.sink.Slack = connector-slack
69 changes: 69 additions & 0 deletions seatunnel-connectors-v2/connector-slack/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>seatunnel-connectors-v2</artifactId>
<groupId>org.apache.seatunnel</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>connector-slack</artifactId>

<properties>
<httpclient.version>4.5.13</httpclient.version>
<slack-api-client>1.25.0</slack-api-client>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.seatunnel</groupId>
<artifactId>connector-common</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>${httpclient.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.slack.api/slack-api-client -->
<dependency>
<groupId>com.slack.api</groupId>
<artifactId>slack-api-client</artifactId>
<version>${slack-api-client}</version>
<exclusions>
<exclusion>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-common</artifactId>
</exclusion>
<exclusion>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.seatunnel.connectors.seatunnel.client;

import org.apache.seatunnel.connectors.seatunnel.config.SlackConfig;

import com.slack.api.Slack;
import com.slack.api.methods.MethodsClient;
import com.slack.api.methods.SlackApiException;
import com.slack.api.methods.response.chat.ChatPostMessageResponse;
import com.slack.api.methods.response.conversations.ConversationsListResponse;
import com.slack.api.model.Conversation;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.List;

@Slf4j
public class SlackClient {
private SlackConfig slackConfig;
private MethodsClient methodsClient;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private SlackConfig slackConfig;
private MethodsClient methodsClient;
private final SlackConfig slackConfig;
private final MethodsClient methodsClient;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! PTAL


public SlackClient(SlackConfig slackConfig) {
this.slackConfig = slackConfig;
this.methodsClient = Slack.getInstance().methods();
}

/**
* Find conversation ID using the conversations.list method
*/
public String findConversation() {
String conversionId = "";
List<Conversation> channels;
try {
// Get Conversion List
ConversationsListResponse conversationsListResponse = methodsClient.conversationsList(r -> r
// The Token used to initialize app
.token(slackConfig.getOauthToken())
);
channels = conversationsListResponse.getChannels();
for (Conversation channel : channels) {
if (channel.getName().equals(slackConfig.getSlackChannel())) {
conversionId = channel.getId();
// Break from for loop
break;
}
}
} catch (IOException | SlackApiException e) {
log.warn("Find Slack Conversion Fail.", e);
throw new RuntimeException("Find Slack Conversion Fail.", e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
return conversionId;
}

/**
* Post a message to a channel using Channel ID and message text
*/
public boolean publishMessage(String channelId, String text) {
boolean publishMessageSuccess = false;
try {
ChatPostMessageResponse chatPostMessageResponse = methodsClient.chatPostMessage(r -> r
// The Token used to initialize app
.token(slackConfig.getOauthToken())
.channel(channelId)
.text(text)
);
publishMessageSuccess = chatPostMessageResponse.isOk();
} catch (IOException | SlackApiException e) {
log.error("error: {}", e.getMessage(), e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"error: {}" only can add one argu, e.getMessage(), e have two.

You can update reference this:

log.error("error: {}", ExceptionUtils.getMessage(e));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have fixed it! Thx

}
return publishMessageSuccess;
}

/**
* Close Conversion
*/
public void closeMethodClient() {
methodsClient = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a close() method in MethodsClient?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no close() method in Slack API.

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.seatunnel.connectors.seatunnel.config;

import org.apache.seatunnel.shade.com.typesafe.config.Config;

import lombok.Data;
import lombok.NonNull;

@Data
public class SlackConfig {

private static final String WEBHOOKS_URL = "webhooks_url";
private static final String OAUTH_TOKEN = "oauth_token";
private static final String SLACK_CHANNEL = "slack_channel";
private String webHooksUrl;
private String oauthToken;
private String slackChannel;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private String webHooksUrl;
private String oauthToken;
private String slackChannel;
private final String webHooksUrl;
private final String oauthToken;
private final String slackChannel;


public SlackConfig(@NonNull Config pluginConfig) {
if (pluginConfig.hasPath(WEBHOOKS_URL)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of webhooks_url and oauth_token and slack_channel is required. So you use CheckConfigUtil.checkAllExists to check them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your advice, I have done it!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These three parameters have already been checked in the prepare method, and this place does not need to check again.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have removed it.PTAL

this.webHooksUrl = pluginConfig.getString(WEBHOOKS_URL);
}
if (pluginConfig.hasPath(OAUTH_TOKEN)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your reminder.

this.oauthToken = pluginConfig.getString(OAUTH_TOKEN);
}
if (pluginConfig.hasPath(SLACK_CHANNEL)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx

this.slackChannel = pluginConfig.getString(SLACK_CHANNEL);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.seatunnel.connectors.seatunnel.sink;

import org.apache.seatunnel.api.common.PrepareFailException;
import org.apache.seatunnel.api.sink.SeaTunnelSink;
import org.apache.seatunnel.api.sink.SinkWriter;
import org.apache.seatunnel.api.table.type.SeaTunnelDataType;
import org.apache.seatunnel.api.table.type.SeaTunnelRow;
import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
import org.apache.seatunnel.connectors.seatunnel.common.sink.AbstractSimpleSink;
import org.apache.seatunnel.connectors.seatunnel.common.sink.AbstractSinkWriter;

import org.apache.seatunnel.shade.com.typesafe.config.Config;

import com.google.auto.service.AutoService;

import java.io.IOException;


/**
* Slack sink class
*/
@AutoService(SeaTunnelSink.class)
public class SlackSink extends AbstractSimpleSink<SeaTunnelRow, Void> {

private Config pluginConfig;
private SeaTunnelRowType seaTunnelRowType;

@Override
public void setTypeInfo(SeaTunnelRowType seaTunnelRowType) {
this.seaTunnelRowType = seaTunnelRowType;
}

@Override
public SeaTunnelDataType<SeaTunnelRow> getConsumedType() {
return this.seaTunnelRowType;
}

@Override
public AbstractSinkWriter<SeaTunnelRow, Void> createWriter(SinkWriter.Context context) throws IOException {
return new SlackWriter(seaTunnelRowType, pluginConfig);
}

@Override
public String getPluginName() {
return "SlackSink";
}

@Override
public void prepare(Config pluginConfig) throws PrepareFailException {
this.pluginConfig = pluginConfig;
}
}



Loading