From 705ea6069f5bb945c03e24315aacf61b5839fb3c Mon Sep 17 00:00:00 2001 From: morfeusys Date: Wed, 11 Nov 2020 19:59:16 +0300 Subject: [PATCH] fix #108 fix #110 --- .../channel/telegram/TelegramBotRequest.kt | 15 ++-- .../channel/telegram/TelegramReactions.kt | 69 ++++++++++++++++--- .../examples/helloworld/HelloWorldScenario.kt | 12 +++- .../examples/helloworld/channel/Telegram.kt | 2 +- 4 files changed, 80 insertions(+), 18 deletions(-) diff --git a/channels/telegram/src/main/kotlin/com/justai/jaicf/channel/telegram/TelegramBotRequest.kt b/channels/telegram/src/main/kotlin/com/justai/jaicf/channel/telegram/TelegramBotRequest.kt index d593a4dc..b41cff3e 100644 --- a/channels/telegram/src/main/kotlin/com/justai/jaicf/channel/telegram/TelegramBotRequest.kt +++ b/channels/telegram/src/main/kotlin/com/justai/jaicf/channel/telegram/TelegramBotRequest.kt @@ -26,15 +26,15 @@ interface TelegramBotRequest: BotRequest { data class TelegramTextRequest( override val message: Message ): TelegramBotRequest, QueryBotRequest( - clientId = message.chat.id.toString(), - input = message.text!! + clientId = message.clientId, + input = message.text.toString() ) data class TelegramQueryRequest( override val message: Message, val data: String ): TelegramBotRequest, QueryBotRequest( - clientId = message.chat.id.toString(), + clientId = message.clientId, input = data ) @@ -42,7 +42,7 @@ data class TelegramLocationRequest( override val message: Message, val location: Location ): TelegramBotRequest, EventBotRequest( - clientId = message.chat.id.toString(), + clientId = message.clientId, input = TelegramEvent.LOCATION ) @@ -50,6 +50,9 @@ data class TelegramContactRequest( override val message: Message, val contact: Contact ): TelegramBotRequest, EventBotRequest( - clientId = message.chat.id.toString(), + clientId = message.clientId, input = TelegramEvent.CONTACT -) \ No newline at end of file +) + +internal val Message.clientId + get() = from?.id?.toString() ?: chat.id.toString() \ No newline at end of file diff --git a/channels/telegram/src/main/kotlin/com/justai/jaicf/channel/telegram/TelegramReactions.kt b/channels/telegram/src/main/kotlin/com/justai/jaicf/channel/telegram/TelegramReactions.kt index b2e564b4..635ed024 100644 --- a/channels/telegram/src/main/kotlin/com/justai/jaicf/channel/telegram/TelegramReactions.kt +++ b/channels/telegram/src/main/kotlin/com/justai/jaicf/channel/telegram/TelegramReactions.kt @@ -3,6 +3,7 @@ package com.justai.jaicf.channel.telegram import com.github.kotlintelegrambot.Bot import com.github.kotlintelegrambot.entities.* import com.github.kotlintelegrambot.entities.inputmedia.MediaGroup +import com.github.kotlintelegrambot.network.Response import com.justai.jaicf.logging.AudioReaction import com.justai.jaicf.logging.ButtonsReaction import com.justai.jaicf.logging.ImageReaction @@ -18,11 +19,34 @@ class TelegramReactions( ) : Reactions() { val chatId = request.chatId + private val messages = mutableListOf() + + private fun addResponse(pair: Pair?>?, Exception?>) { + pair.first?.body()?.result?.let { messages.add(it) } + } override fun say(text: String): SayReaction { return sendMessage(text) } + override fun buttons(vararg buttons: String): ButtonsReaction { + messages.lastOrNull()?.let { message -> + val keyboard = message.replyMarkup?.inlineKeyboard?.toMutableList() ?: mutableListOf() + keyboard.addAll(buttons.map { listOf(InlineKeyboardButton(it, callbackData = it)) }) + + api.editMessageReplyMarkup( + chatId, + message.messageId, + replyMarkup = InlineKeyboardMarkup(keyboard) + ).also { + messages.remove(message) + addResponse(it) + } + } + + return ButtonsReaction.create(buttons.asList()) + } + fun say(text: String, inlineButtons: List) = api.sendMessage( chatId, text, @@ -59,7 +83,8 @@ class TelegramReactions( disableNotification, replyToMessageId, replyMarkup - ) + ).also { addResponse(it) } + return SayReaction.create(text) } @@ -84,7 +109,16 @@ class TelegramReactions( replyToMessageId: Long? = null, replyMarkup: ReplyMarkup? = null ): ImageReaction { - api.sendPhoto(chatId, url, caption, parseMode, disableNotification, replyToMessageId, replyMarkup) + api.sendPhoto( + chatId, + url, + caption, + parseMode, + disableNotification, + replyToMessageId, + replyMarkup + ).also { addResponse(it) } + return ImageReaction.create(url) } @@ -97,7 +131,9 @@ class TelegramReactions( disableNotification: Boolean? = null, replyToMessageId: Long? = null, replyMarkup: ReplyMarkup? = null - ) = api.sendVideo(chatId, url, duration, width, height, caption, disableNotification, replyToMessageId, replyMarkup) + ) = api.sendVideo( + chatId, url, duration, width, height, caption, disableNotification, replyToMessageId, replyMarkup + ).also { addResponse(it) } fun sendVoice( url: String, @@ -105,7 +141,9 @@ class TelegramReactions( disableNotification: Boolean? = null, replyToMessageId: Long? = null, replyMarkup: ReplyMarkup? = null - ) = api.sendVoice(chatId, url, duration, disableNotification, replyToMessageId, replyMarkup) + ) = api.sendVoice( + chatId, url, duration, disableNotification, replyToMessageId, replyMarkup + ).also { addResponse(it) } override fun audio(url: String): AudioReaction { return sendAudio(url) @@ -120,7 +158,10 @@ class TelegramReactions( replyToMessageId: Long? = null, replyMarkup: ReplyMarkup? = null ): AudioReaction { - api.sendAudio(chatId, url, duration, performer, title, disableNotification, replyToMessageId, replyMarkup) + api.sendAudio( + chatId, url, duration, performer, title, disableNotification, replyToMessageId, replyMarkup + ).also { addResponse(it) } + return AudioReaction.create(url) } @@ -131,7 +172,9 @@ class TelegramReactions( disableNotification: Boolean? = null, replyToMessageId: Long? = null, replyMarkup: ReplyMarkup? = null - ) = api.sendDocument(chatId, url, caption, parseMode, disableNotification, replyToMessageId, replyMarkup) + ) = api.sendDocument( + chatId, url, caption, parseMode, disableNotification, replyToMessageId, replyMarkup + ).also { addResponse(it) } fun sendVenue( latitude: Float, @@ -154,7 +197,7 @@ class TelegramReactions( disableNotification, replyToMessageId, replyMarkup - ) + ).also { addResponse(it) } fun sendContact( phoneNumber: String, @@ -163,7 +206,9 @@ class TelegramReactions( disableNotification: Boolean? = null, replyToMessageId: Long? = null, replyMarkup: ReplyMarkup? = null - ) = api.sendContact(chatId, phoneNumber, firstName, lastName, disableNotification, replyToMessageId, replyMarkup) + ) = api.sendContact( + chatId, phoneNumber, firstName, lastName, disableNotification, replyToMessageId, replyMarkup + ).also { addResponse(it) } fun sendLocation( latitude: Float, @@ -172,7 +217,9 @@ class TelegramReactions( disableNotification: Boolean? = null, replyToMessageId: Long? = null, replyMarkup: ReplyMarkup? = null - ) = api.sendLocation(chatId, latitude, longitude, livePeriod, disableNotification, replyToMessageId, replyMarkup) + ) = api.sendLocation( + chatId, latitude, longitude, livePeriod, disableNotification, replyToMessageId, replyMarkup + ).also { addResponse(it) } fun sendMediaGroup( mediaGroup: MediaGroup, @@ -187,5 +234,7 @@ class TelegramReactions( disableNotification: Boolean? = null, replyToMessageId: Long? = null, replyMarkup: ReplyMarkup? = null - ) = api.sendVideoNote(chatId, url, duration, length, disableNotification, replyToMessageId, replyMarkup) + ) = api.sendVideoNote( + chatId, url, duration, length, disableNotification, replyToMessageId, replyMarkup + ).also { addResponse(it) } } \ No newline at end of file diff --git a/examples/hello-world/src/main/kotlin/com/justai/jaicf/examples/helloworld/HelloWorldScenario.kt b/examples/hello-world/src/main/kotlin/com/justai/jaicf/examples/helloworld/HelloWorldScenario.kt index ab5eaacb..f47e30af 100644 --- a/examples/hello-world/src/main/kotlin/com/justai/jaicf/examples/helloworld/HelloWorldScenario.kt +++ b/examples/hello-world/src/main/kotlin/com/justai/jaicf/examples/helloworld/HelloWorldScenario.kt @@ -44,6 +44,7 @@ object HelloWorldScenario: Scenario( reactions.run { image("https://www.bluecross.org.uk/sites/default/files/d8/assets/images/118809lprLR.jpg") sayRandom("Hello $name!", "Hi $name!", "Glad to hear you $name!") + buttons("Mew", "Wake up") aimybox?.endConversation() } } @@ -83,7 +84,7 @@ object HelloWorldScenario: Scenario( state("mew") { activators { - regex("/mew") + regex("mew") } action { @@ -102,5 +103,14 @@ object HelloWorldScenario: Scenario( } } } + + state("cancel") { + activators { + intent("cancel") + } + action { + reactions.say("Okay, canceling.") + } + } } } \ No newline at end of file diff --git a/examples/hello-world/src/main/kotlin/com/justai/jaicf/examples/helloworld/channel/Telegram.kt b/examples/hello-world/src/main/kotlin/com/justai/jaicf/examples/helloworld/channel/Telegram.kt index 1cfa51ae..bf465a53 100644 --- a/examples/hello-world/src/main/kotlin/com/justai/jaicf/examples/helloworld/channel/Telegram.kt +++ b/examples/hello-world/src/main/kotlin/com/justai/jaicf/examples/helloworld/channel/Telegram.kt @@ -4,5 +4,5 @@ import com.justai.jaicf.channel.telegram.TelegramChannel import com.justai.jaicf.examples.helloworld.helloWorldBot fun main() { - TelegramChannel(helloWorldBot, "580468601:AAHaMg4gOsN2A_zvIO6-PouVk3GcZ_WVrdI").run() + TelegramChannel(helloWorldBot, "580468601:AAFZaIXLiKbD7XRsGVHX74N-5nlXbttvEzc").run() } \ No newline at end of file