TyrantLucifer commented on code in PR #3226:
URL: 
https://github.com/apache/incubator-seatunnel/pull/3226#discussion_r1023949465


##########
seatunnel-connectors-v2/connector-slack/src/main/java/org/apache/seatunnel/connectors/seatunnel/client/SlackClient.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.common.utils.ExceptionUtils;
+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;
+
+    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);

Review Comment:
   You should create a `SlackConnectorException` that extended 
`SeaTunnelRuntimeException` instead of it. You can refer to 
https://github.com/apache/incubator-seatunnel/tree/dev/seatunnel-connectors-v2/connector-cassandra/src/main/java/org/apache/seatunnel/connectors/seatunnel/cassandra/exception



##########
seatunnel-connectors-v2/connector-slack/src/main/java/org/apache/seatunnel/connectors/seatunnel/client/SlackClient.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.common.utils.ExceptionUtils;
+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;
+
+    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);
+        }
+        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: {}", ExceptionUtils.getMessage(e));
+        }
+        return publishMessageSuccess;
+    }
+
+    /**
+     * Close Conversion
+     */
+    public void closeMethodClient() {
+        methodsClient = null;

Review Comment:
   Is there a `close()` method in `MethodsClient`?



##########
seatunnel-connectors-v2/connector-slack/src/main/java/org/apache/seatunnel/connectors/seatunnel/sink/SlackWriter.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.table.type.SeaTunnelRow;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import org.apache.seatunnel.common.utils.ExceptionUtils;
+import org.apache.seatunnel.connectors.seatunnel.client.SlackClient;
+import 
org.apache.seatunnel.connectors.seatunnel.common.sink.AbstractSinkWriter;
+import org.apache.seatunnel.connectors.seatunnel.config.SlackConfig;
+
+import org.apache.seatunnel.shade.com.typesafe.config.Config;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.IOException;
+import java.util.StringJoiner;
+
+@Slf4j
+public class SlackWriter extends AbstractSinkWriter<SeaTunnelRow, Void> {
+    private SlackConfig slackConfig;

Review Comment:
   ```suggestion
       private final SlackConfig slackConfig;
   ```



##########
seatunnel-connectors-v2/connector-slack/src/main/java/org/apache/seatunnel/connectors/seatunnel/sink/SlackWriter.java:
##########
@@ -0,0 +1,71 @@
+/*
+ * 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.table.type.SeaTunnelRow;
+import org.apache.seatunnel.api.table.type.SeaTunnelRowType;
+import org.apache.seatunnel.common.utils.ExceptionUtils;
+import org.apache.seatunnel.connectors.seatunnel.client.SlackClient;
+import 
org.apache.seatunnel.connectors.seatunnel.common.sink.AbstractSinkWriter;
+import org.apache.seatunnel.connectors.seatunnel.config.SlackConfig;
+
+import org.apache.seatunnel.shade.com.typesafe.config.Config;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.IOException;
+import java.util.StringJoiner;
+
+@Slf4j
+public class SlackWriter extends AbstractSinkWriter<SeaTunnelRow, Void> {
+    private SlackConfig slackConfig;
+    private final String conversationId;
+    private final SlackClient slackClient;
+    private final SeaTunnelRowType seaTunnelRowType;
+    private static final long POST_MSG_WAITING_TIME = 1500L;
+
+    public SlackWriter(SeaTunnelRowType seaTunnelRowType, Config pluginConfig) 
{
+        this.slackConfig = new SlackConfig(pluginConfig);
+        this.seaTunnelRowType = seaTunnelRowType;
+        this.slackClient = new SlackClient(slackConfig);
+        this.conversationId = slackClient.findConversation();
+    }
+
+    @Override
+    public void write(SeaTunnelRow element) throws IOException {
+        Object[] fields = element.getFields();
+        StringJoiner stringJoiner = new StringJoiner(",", "", "\n");
+        for (Object field : fields) {
+            stringJoiner.add(String.valueOf(field));
+        }
+        String message = stringJoiner.toString();
+        try {
+            slackClient.publishMessage(conversationId, message);
+            // Slack has a limit on the frequency of sending messages
+            // One message can be sent as soon as one second
+            Thread.sleep(POST_MSG_WAITING_TIME);
+        } catch (Exception e) {
+            log.warn("Write to Slack Fail.", ExceptionUtils.getMessage(e));
+            throw new RuntimeException("Write to Slack Fail.", e);

Review Comment:
   Same as above, please refer to 
https://github.com/apache/incubator-seatunnel/blob/dev/seatunnel-connectors-v2/connector-cassandra/src/main/java/org/apache/seatunnel/connectors/seatunnel/cassandra/source/CassandraSource.java#L78



##########
seatunnel-connectors-v2/connector-slack/src/main/java/org/apache/seatunnel/connectors/seatunnel/sink/SlackSink.java:
##########
@@ -0,0 +1,84 @@
+/*
+ * 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 static 
org.apache.seatunnel.connectors.seatunnel.config.SlackConfig.OAUTH_TOKEN;
+import static 
org.apache.seatunnel.connectors.seatunnel.config.SlackConfig.SLACK_CHANNEL;
+import static 
org.apache.seatunnel.connectors.seatunnel.config.SlackConfig.WEBHOOKS_URL;
+
+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.common.config.CheckConfigUtil;
+import org.apache.seatunnel.common.config.CheckResult;
+import org.apache.seatunnel.common.constants.PluginType;
+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 {
+        CheckResult checkResult = CheckConfigUtil.checkAllExists(pluginConfig, 
WEBHOOKS_URL, OAUTH_TOKEN, SLACK_CHANNEL);
+        if (!checkResult.isSuccess()) {
+            throw new PrepareFailException("Slack", PluginType.SINK, 
checkResult.getMsg());

Review Comment:
   The same as above, please refer to 
https://github.com/apache/incubator-seatunnel/blob/dev/seatunnel-connectors-v2/connector-cassandra/src/main/java/org/apache/seatunnel/connectors/seatunnel/cassandra/source/CassandraSource.java#L65



##########
seatunnel-connectors-v2/connector-slack/src/main/java/org/apache/seatunnel/connectors/seatunnel/config/SlackConfig.java:
##########
@@ -0,0 +1,40 @@
+/*
+ * 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 {
+
+    public static final String WEBHOOKS_URL = "webhooks_url";
+    public static final String OAUTH_TOKEN = "oauth_token";
+    public static final String SLACK_CHANNEL = "slack_channel";
+    private String webHooksUrl;
+    private String oauthToken;
+    private String slackChannel;

Review Comment:
   ```suggestion
       private final String webHooksUrl;
       private final String oauthToken;
       private final String slackChannel;
   ```



##########
seatunnel-connectors-v2/connector-slack/src/main/java/org/apache/seatunnel/connectors/seatunnel/client/SlackClient.java:
##########
@@ -0,0 +1,96 @@
+/*
+ * 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.common.utils.ExceptionUtils;
+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;

Review Comment:
   ```suggestion
       private final SlackConfig slackConfig;
       private final MethodsClient methodsClient;
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to