All Downloads are FREE. Search and download functionalities are using the official Maven repository.

bot-connector-slack.19.9.3.source-code.SlackBuilders.kt Maven / Gradle / Ivy

There is a newer version: 24.9.4
Show newest version
/*
 * Copyright (C) 2017/2019 e-voyageurs technologies
 *
 * 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.
 */

package ai.tock.bot.connector.slack

import ai.tock.bot.connector.ConnectorMessage
import ai.tock.bot.connector.ConnectorType
import ai.tock.bot.connector.slack.model.AttachmentField
import ai.tock.bot.connector.slack.model.Button
import ai.tock.bot.connector.slack.model.SlackConnectorMessage
import ai.tock.bot.connector.slack.model.SlackEmoji
import ai.tock.bot.connector.slack.model.SlackMessageAttachment
import ai.tock.bot.connector.slack.model.SlackMessageOut
import ai.tock.bot.definition.IntentAware
import ai.tock.bot.definition.Parameters
import ai.tock.bot.definition.StoryHandlerDefinition
import ai.tock.bot.definition.StoryStep
import ai.tock.bot.engine.Bus
import ai.tock.bot.engine.I18nTranslator
import ai.tock.bot.engine.action.SendChoice

internal const val SLACK_CONNECTOR_TYPE_ID = "slack"

/**
 * The Slack connector type.
 */
val slackConnectorType = ConnectorType(SLACK_CONNECTOR_TYPE_ID)

/**
 * Sends a Slack message only if the [ConnectorType] of the current [BotBus] is [slackConnectorType].
 */
fun > T.sendToSlack(
    delay: Long = defaultDelay(currentAnswerIndex),
    messageProvider: T.() -> SlackConnectorMessage
): T {
    if (targetConnectorType == slackConnectorType) {
        withMessage(messageProvider(this))
        send(delay)
    }
    return this
}

/**
 * Sends a Slack message as last bot answer, only if the [ConnectorType] of the current [BotBus] is [slackConnectorType].
 */
fun > T.endForSlack(
    delay: Long = defaultDelay(currentAnswerIndex),
    messageProvider: T.() -> SlackConnectorMessage
): T {
    if (targetConnectorType == slackConnectorType) {
        withMessage(messageProvider(this))
        end(delay)
    }
    return this
}

/**
 * Adds a Slack [ConnectorMessage] if the current connector is Slack.
 * You need to call [BotBus.send] or [BotBus.end] later to send this message.
 */
fun > T.withSlack(messageProvider: () -> SlackConnectorMessage): T {
    return withMessage(slackConnectorType, messageProvider)
}

fun I18nTranslator.textMessage(message: CharSequence): SlackMessageOut {
    return SlackMessageOut(translate(message).toString())
}

fun I18nTranslator.multiLineMessage(lines: List, channel: String? = null): SlackMessageOut =
    SlackMessageOut(lines.joinToString("\n") { translate(it).toString() }, channel)

fun I18nTranslator.slackMessage(
    message: CharSequence,
    vararg attachments: SlackMessageAttachment
): SlackMessageOut {
    return slackMessage(message, null, attachments = *attachments)
}

fun I18nTranslator.slackMessage(
    message: CharSequence,
    channel: String? = null,
    vararg attachments: SlackMessageAttachment
): SlackMessageOut {
    return SlackMessageOut(translate(message).toString(), channel, attachments.toList())
}

fun I18nTranslator.slackAttachment(
    text: CharSequence? = null,
    vararg buttons: Button
): SlackMessageAttachment =
    slackAttachment(text, buttons.toList())

fun I18nTranslator.slackAttachment(
    vararg buttons: Button
): SlackMessageAttachment =
    slackAttachment(buttons = buttons.toList())

fun I18nTranslator.slackAttachment(
    text: CharSequence? = null,
    buttons: List




© 2015 - 2025 Weber Informatics LLC | Privacy Policy