SlackMessage createSlackMessage(EventNotificationContext ctx, SlackEventNotificationConfig config) throws PermanentEventNotificationException {

        String customMessage = null;
        String template = config.customMessage();
        if (!isNullOrEmpty(template)) {
            // If the title is not included but the channel/here still needs to be notified, add a @channel tag to the custom message
            if (!config.includeTitle() && (config.notifyChannel() || config.notifyHere())) {
                String tag = config.notifyChannel() ? "channel" : "here";
                template = StringUtils.f("@%s\n%s", tag, template);
            }
            customMessage = buildCustomMessage(ctx, config, template);
        }

        SlackMessage.Attachment attachment = SlackMessage.Attachment.builder()
                .color(config.color())
                .text(customMessage)
                .build();

        //Note: Link names if notify channel or else the channel tag will be plain text.
        boolean linkNames = config.linkNames() || config.notifyChannel() || config.notifyHere();
        String templatedChannel = buildTemplatedChannel(ctx, config, config.channel());
        String emoji = config.iconEmoji() != null ? ensureEmojiSyntax(config.iconEmoji()) : "";
        return SlackMessage.builder()
                .iconEmoji(emoji)
                .iconUrl(config.iconUrl())
                .username(config.userName())
                .text(config.includeTitle() ? buildDefaultMessage(ctx, config) : null)
                .channel(templatedChannel)
                .linkNames(linkNames)
                .attachments(isNullOrEmpty(template) ? Collections.emptySet() : Collections.singleton(attachment))
                .build();
    }
@Test
    public void testChannelAlertWithTitle() throws EventNotificationException {
        SlackEventNotificationConfig slackConfig = SlackEventNotificationConfig.builder()
                .includeTitle(true)
                .notifyChannel(true)
                .customMessage("A custom message")
                .iconEmoji("")
                .iconUrl("")
                .userName("")
                .build();

        SlackMessage message = slackEventNotification.createSlackMessage(eventNotificationContext, slackConfig);
        assertThat(message.attachments().iterator().next().text()).doesNotStartWith("@channel");
        assertThat(message.text()).startsWith("@channel");
    }