Skip to content

Commit

Permalink
fix #108
Browse files Browse the repository at this point in the history
fix #110
  • Loading branch information
morfeusys committed Nov 13, 2020
1 parent 8a61fc5 commit 80ec7e3
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,33 @@ 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
)

data class TelegramLocationRequest(
override val message: Message,
val location: Location
): TelegramBotRequest, EventBotRequest(
clientId = message.chat.id.toString(),
clientId = message.clientId,
input = TelegramEvent.LOCATION
)

data class TelegramContactRequest(
override val message: Message,
val contact: Contact
): TelegramBotRequest, EventBotRequest(
clientId = message.chat.id.toString(),
clientId = message.clientId,
input = TelegramEvent.CONTACT
)
)

internal val Message.clientId
get() = from?.id?.toString() ?: chat.id.toString()
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -18,11 +19,34 @@ class TelegramReactions(
) : Reactions() {

val chatId = request.chatId
private val messages = mutableListOf<Message>()

private fun addResponse(pair: Pair<retrofit2.Response<Response<Message>?>?, 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<String>) = api.sendMessage(
chatId,
text,
Expand Down Expand Up @@ -59,7 +83,8 @@ class TelegramReactions(
disableNotification,
replyToMessageId,
replyMarkup
)
).also { addResponse(it) }

return SayReaction.create(text)
}

Expand All @@ -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)
}

Expand All @@ -97,15 +131,19 @@ 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,
duration: Int? = null,
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)
Expand All @@ -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)
}

Expand All @@ -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,
Expand All @@ -154,7 +197,7 @@ class TelegramReactions(
disableNotification,
replyToMessageId,
replyMarkup
)
).also { addResponse(it) }

fun sendContact(
phoneNumber: String,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -83,7 +84,7 @@ object HelloWorldScenario: Scenario(

state("mew") {
activators {
regex("/mew")
regex("mew")
}

action {
Expand All @@ -102,5 +103,14 @@ object HelloWorldScenario: Scenario(
}
}
}

state("cancel") {
activators {
intent("cancel")
}
action {
reactions.say("Okay, canceling.")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

0 comments on commit 80ec7e3

Please sign in to comment.