Commit 0f8f3208 authored by Noel Alonso's avatar Noel Alonso
Browse files

Merge branch 'feature-slackIntegration' into 'dev'

Feature slack integration

See merge request redmic-project/server/notification-manager!6
parents 9fc05eb1 1e548adc
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -28,7 +28,9 @@ variables:
    - >
      deploy.sh IMAGE_NAME=${IMAGE_NAME} IMAGE_TAG=${IMAGE_TAG} COMPOSE_FILE=${COMPOSE_FILE}
      SPRING_PROFILES_ACTIVE=${SPRING_PROFILES_ACTIVE} SPRING_MAIL_USERNAME=${SPRING_MAIL_USERNAME}
      SPRING_MAIL_PASSWORD=${SPRING_MAIL_PASSWORD} PUBLIC_HOSTNAME=${PUBLIC_HOSTNAME}
      SPRING_MAIL_PASSWORD=${SPRING_MAIL_PASSWORD} SLACK_WEBHOOK_URL=${SLACK_WEBHOOK_URL}
      SLACK_WEBHOOK_USERNAME=${SLACK_WEBHOOK_USERNAME} SLACK_WEBHOOK_CHANNEL=${SLACK_WEBHOOK_CHANNEL}
      PUBLIC_HOSTNAME=${PUBLIC_HOSTNAME}
  environment:
    url: https://${PUBLIC_HOSTNAME}/api/${CI_PROJECT_NAME}

+3 −0
Original line number Diff line number Diff line
@@ -9,6 +9,9 @@ services:
      - SPRING_PROFILES_ACTIVE
      - SPRING_MAIL_USERNAME
      - SPRING_MAIL_PASSWORD
      - SLACK_WEBHOOK_URL
      - SLACK_WEBHOOK_USERNAME
      - SLACK_WEBHOOK_CHANNEL
      - JAVA_OPTS=-XX:MaxRAMFraction=2
    networks:
      - traefik-net
+14 −3
Original line number Diff line number Diff line
@@ -13,14 +13,17 @@
	<modelVersion>4.0.0</modelVersion>
	<artifactId>notification-manager</artifactId>
	<packaging>jar</packaging>
	<version>0.9.0</version>
	<version>0.9.0-feature-slackIntegration</version>
	<name>Notification manager</name>
	<description>Microservice for send notifications</description>

	<properties>
		<!-- REDMIC -->
		<redmic.broker-lib.version>0.8.0</redmic.broker-lib.version>
		<redmic.broker-lib.version>0.9.0-feature-slackIntegration</redmic.broker-lib.version>
		<redmic.test-utils.version>0.8.0</redmic.test-utils.version>
		
		<!-- OTROS -->
		<jslack.version>1.5.0</jslack.version>
	</properties>

	<dependencies>
@@ -53,6 +56,14 @@
			<version>${redmic.broker-lib.version}</version>
		</dependency>

		<!-- Otros -->

		<dependency>
			<groupId>com.github.seratch</groupId>
			<artifactId>jslack</artifactId>
			<version>${jslack.version}</version>
		</dependency>

		<!-- test -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
+14 −5
Original line number Diff line number Diff line
@@ -27,8 +27,10 @@ import org.springframework.kafka.annotation.KafkaHandler;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Controller;

import es.redmic.brokerlib.alert.AlertUtil;
import es.redmic.brokerlib.alert.Message;
import es.redmic.notificationmanager.mail.service.EmailService;
import es.redmic.notificationmanager.mail.service.SlackService;

@Controller
@KafkaListener(topics = "${broker.topic.alert}")
@@ -36,18 +38,25 @@ public class NotificationController {

	protected static Logger logger = LogManager.getLogger();

	EmailService service;
	EmailService emailService;
	
	SlackService slackService;

	@Autowired
	public NotificationController(EmailService service) {
		this.service = service;
	public NotificationController(EmailService emailService, SlackService slackService) {
		this.emailService = emailService;
		this.slackService = slackService;
	}

	@KafkaHandler
	public void listen(Message event) {

		// TODO: decidir el canal para llamar al servicio adecuado.
		service.sendSimpleMessage(event.getTo(), event.getSubject(), event.getMessage());
		emailService.sendSimpleMessage(event.getTo(), event.getSubject(), event.getMessage());
		
		if (AlertUtil.isRealTimeType(event.getType())) {
			slackService.sendMessage(event.getSubject(), event.getMessage());
		}
		
		logger.info("Recibida notificación -> {}: {}", event.getSubject(), event.getMessage());
	}
}
+58 −0
Original line number Diff line number Diff line
package es.redmic.notificationmanager.mail.service;

import java.io.IOException;

/*-
 * #%L
 * Notification manager
 * %%
 * Copyright (C) 2019 REDMIC Project / Server
 * %%
 * Licensed 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.
 * #L%
 */

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.github.seratch.jslack.Slack;
import com.github.seratch.jslack.api.webhook.Payload;

@Service
public class SlackService {

	@Value("${slack.webhook.url}")
	String url;
	
	@Value("${slack.webhook.username}")
	String username;
	
	@Value("${slack.webhook.channel}")
	String channel;

	public void sendMessage(String subject, String text) {

		Payload payload = Payload.builder()
				.channel(channel)
				.username(username)
				.text(subject + ": " + text)
					.build();

		Slack slack = Slack.getInstance();
		try {
			slack.send(url, payload);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
Loading