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

fr.vsct.tock.bot.connector.twitter.TwitterBuilders.kt Maven / Gradle / Ivy

There is a newer version: 19.3.3
Show newest version
/*
 * Copyright (C) 2019 VSCT
 *
 * 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 fr.vsct.tock.bot.connector.twitter

import fr.vsct.tock.bot.connector.ConnectorType
import fr.vsct.tock.bot.connector.twitter.model.AttachmentData
import fr.vsct.tock.bot.connector.twitter.model.CTA
import fr.vsct.tock.bot.connector.twitter.model.MediaCategory
import fr.vsct.tock.bot.connector.twitter.model.MessageCreate
import fr.vsct.tock.bot.connector.twitter.model.MessageData
import fr.vsct.tock.bot.connector.twitter.model.Option
import fr.vsct.tock.bot.connector.twitter.model.Options
import fr.vsct.tock.bot.connector.twitter.model.Recipient
import fr.vsct.tock.bot.connector.twitter.model.TwitterConnectorMessage
import fr.vsct.tock.bot.connector.twitter.model.TwitterPublicConnectorMessage
import fr.vsct.tock.bot.connector.twitter.model.WebUrl
import fr.vsct.tock.bot.connector.twitter.model.outcoming.DirectMessageOutcomingEvent
import fr.vsct.tock.bot.connector.twitter.model.outcoming.OutcomingEvent
import fr.vsct.tock.bot.connector.twitter.model.outcoming.Tweet
import fr.vsct.tock.bot.definition.IntentAware
import fr.vsct.tock.bot.definition.StoryHandlerDefinition
import fr.vsct.tock.bot.definition.StoryStep
import fr.vsct.tock.bot.engine.BotBus
import fr.vsct.tock.bot.engine.Bus
import fr.vsct.tock.bot.engine.action.ActionVisibility
import fr.vsct.tock.bot.engine.action.SendChoice
import mu.KotlinLogging

private val logger = KotlinLogging.logger {}

internal const val TWITTER_CONNECTOR_TYPE_ID = "twitter"

/**
 * The Twitter connector type.
 */
val twitterConnectorType = ConnectorType(TWITTER_CONNECTOR_TYPE_ID)

private const val MAX_OPTION_LABEL = 36
private const val MAX_OPTION_DESCRIPTION = 72
private const val MAX_METADATA = 1000

internal fun CharSequence.truncateIfLongerThan(maxCharacter: Int): String =
    if (maxCharacter >= 0 && this.length > maxCharacter) {
        if (maxCharacter > 3) this.substring(0, maxCharacter - 3) + "..."
        else this.substring(0, maxCharacter)
    } else this.toString()

/**
 * Creates a direct message with only text
 */
fun > T.directMessage(message: CharSequence): OutcomingEvent =
    OutcomingEvent(
        DirectMessageOutcomingEvent(
            MessageCreate(
                target = Recipient(userId.id),
                sourceAppId = applicationId,
                senderId = botId.id,
                messageData = MessageData(translate(message).toString())
            )
        )
    )

/**
 * Creates a direct message with Buttons
 * @see https://developer.twitter.com/en/docs/direct-messages/buttons/api-reference/buttons
 */
fun > T.directMessageWithButtons(message: CharSequence, ctas: List): OutcomingEvent =
    OutcomingEvent(
        DirectMessageOutcomingEvent(
            MessageCreate(
                target = Recipient(userId.id),
                sourceAppId = applicationId,
                senderId = botId.id,
                messageData = MessageData(
                    translate(message).toString(),
                    ctas = if (ctas.isNotEmpty()) ctas else null
                )
            )
        )
    )

/**
 * Creates a direct message with Buttons
 * @see https://developer.twitter.com/en/docs/direct-messages/buttons/api-reference/buttons
 */
fun > T.directMessageWithButtons(message: CharSequence, vararg ctas: CTA): OutcomingEvent =
    directMessageWithButtons(message, ctas.toList())

/**
 * Creates a direct message with quick replies
 * @see https://developer.twitter.com/en/docs/direct-messages/quick-replies/overview
 */
fun > T.directMessageWithOptions(message: CharSequence, options: List




© 2015 - 2024 Weber Informatics LLC | Privacy Policy