EricJoy2048 commented on code in PR #3226: URL: https://github.com/apache/incubator-seatunnel/pull/3226#discussion_r1014977402
########## seatunnel-connectors-v2/connector-slack/src/main/java/org/apache/seatunnel/connectors/seatunnel/client/SlackClient.java: ########## @@ -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; + + 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: {}", e.getMessage(), e); Review Comment: "error: {}" only can add one argu, ` e.getMessage(), e` have two. You can update reference this: ``` log.error("error: {}", ExceptionUtils.getMessage(e)); ``` ########## seatunnel-connectors-v2/connector-slack/src/main/java/org/apache/seatunnel/connectors/seatunnel/config/SlackConfig.java: ########## @@ -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; + + public SlackConfig(@NonNull Config pluginConfig) { + if (pluginConfig.hasPath(WEBHOOKS_URL)) { Review Comment: All of `webhooks_url ` and `oauth_token ` and `slack_channel ` is required. So you use `CheckConfigUtil.checkAllExists` to check them. ########## seatunnel-connectors-v2/connector-slack/src/main/java/org/apache/seatunnel/connectors/seatunnel/sink/SlackWriter.java: ########## @@ -0,0 +1,70 @@ +/* + * 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.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.", e); Review Comment: ``` log.warn("Write to Slack Fail: {}", ExceptionUtils.getMessage(e)); ``` -- 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]
