Skip to content

Commit

Permalink
Merge pull request #2 from simple-robot/dev/merge-events
Browse files Browse the repository at this point in the history
优化消息事件相关的类型继承关系结构;增加部分对发送文本消息以及MessageEntity的元素支持
  • Loading branch information
ForteScarlet authored Apr 11, 2024
2 parents 5cbc4cc + 39f8490 commit 3e02e0c
Show file tree
Hide file tree
Showing 19 changed files with 855 additions and 278 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/publish-snapshot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ jobs:
-Porg.gradle.jvmargs="-Xmx8g -Xms2g -XX:MaxMetaspaceSize=1g -Dfile.encoding=UTF-8"
-Porg.gradle.daemon=false
- name: Upload test reports
uses: actions/upload-artifact@v4
if: ${{ always() }}
with:
name: test-reports-${{ runner.os }}
path: '**/build/reports/tests'
retention-days: 7

deploy-doc:
name: Deploy-doc
runs-on: ubuntu-latest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package love.forte.simbot.telegram.api.message
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.SerializationStrategy
import love.forte.simbot.telegram.api.SimpleBodyTelegramApi
import love.forte.simbot.telegram.api.TelegramApiResult
import love.forte.simbot.telegram.api.message.ReplyMarkupWrapper.Companion.wrapper
Expand Down Expand Up @@ -78,9 +77,6 @@ public class SendMessageApi private constructor(body: Body) : SimpleBodyTelegram

override val body: Any = body

override val bodySerializationStrategy: SerializationStrategy<Any>?
get() = super.bodySerializationStrategy

override val responseDeserializer: DeserializationStrategy<Message>
get() = Message.serializer()

Expand Down Expand Up @@ -174,10 +170,13 @@ public class SendMessageApi private constructor(body: Body) : SimpleBodyTelegram
* @see Body.text
*/
public var text: StringBuilder? = null
private set

public val textOffset: Int
get() = text?.length ?: 0

public fun text(append: CharSequence) {
val apd = text ?: StringBuilder().also { text = it }
apd.append(append)
public fun appendText(append: CharSequence) {
text?.append(append) ?: StringBuilder(append).also { text = it }
}

// Optional
Expand All @@ -202,7 +201,20 @@ public class SendMessageApi private constructor(body: Body) : SimpleBodyTelegram
/**
* @see Body.entities
*/
public var entities: Collection<MessageEntity>? = null
public var entities: MutableCollection<MessageEntity>? = null
private set

/**
* @see Body.entities
*/
public fun addEntity(entity: MessageEntity) {
val c = entities
?: mutableListOf<MessageEntity>().also {
entities = it
}

c.add(entity)
}

/**
* @see Body.linkPreviewOptions
Expand Down Expand Up @@ -306,6 +318,6 @@ public inline fun buildSendMessageApi(
block: Builder.() -> Unit = {}
): SendMessageApi = buildSendMessageApi {
this.chatId = chatId
this.text = StringBuilder(text)
appendText(text)
block()
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SendMessageApiTests {
with(Telegram.DefaultJson) {
val body = SendMessageApi.builder().apply {
chatId(1)
text("forte")
appendText("forte")
replyMarkup(
ForceReply(
forceReply = true,
Expand All @@ -57,7 +57,7 @@ class SendMessageApiTests {
with(Telegram.DefaultJson) {
val body = SendMessageApi.builder().apply {
chatId(1)
text("forte")
appendText("forte")
replyMarkup(
ReplyKeyboardMarkup(
keyboard = listOf(listOf(KeyboardButton(text = "button"))),
Expand All @@ -80,7 +80,7 @@ class SendMessageApiTests {
with(Telegram.DefaultJson) {
val body = SendMessageApi.builder().apply {
chatId(1)
text("forte")
appendText("forte")
replyMarkup(
ReplyKeyboardRemove(
removeKeyboard = true,
Expand All @@ -99,7 +99,7 @@ class SendMessageApiTests {
with(Telegram.DefaultJson) {
val body = SendMessageApi.builder().apply {
chatId(1)
text("forte")
appendText("forte")
replyMarkup(
InlineKeyboardMarkup(
inlineKeyboard = listOf(listOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,12 @@ import kotlin.coroutines.CoroutineContext


/**
* A type that is aware of [Chat].
*
* @author ForteScarlet
*/
public interface TelegramChatAware {
public val source: Chat


}

/**
Expand Down Expand Up @@ -112,14 +111,22 @@ public interface TelegramChatGroupActor : TelegramChatAware, ChatGroup {
override suspend fun send(messageContent: MessageContent): TelegramMessageReceipt
}

/**
* A Telegram [Chat] representing a group or a channel.
*
* @see TelegramChatGroup
* @see TelegramChannel
*/
public interface TelegramGroup : TelegramChatGroupActor

// TODO SuperGroup?

/**
* A Telegram [Chat] representing a group ([Chat.type] == [ChatType.GROUP] or [ChatType.SUPERGROUP]).
*
* @author ForteScarlet
*/
public interface TelegramChatGroup : TelegramChatGroupActor

public interface TelegramChatGroup : TelegramGroup

/**
*
Expand All @@ -129,4 +136,4 @@ public interface TelegramChatGroup : TelegramChatGroupActor
*
* @author ForteScarlet
*/
public interface TelegramChannel : TelegramChatGroupActor
public interface TelegramChannel : TelegramGroup
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ package love.forte.simbot.component.telegram.core.event
import love.forte.simbot.common.id.ID
import love.forte.simbot.common.id.LongID.Companion.ID
import love.forte.simbot.common.time.Timestamp
import love.forte.simbot.component.telegram.core.actor.TelegramChannel
import love.forte.simbot.component.telegram.core.actor.TelegramChatGroup
import love.forte.simbot.component.telegram.core.actor.TelegramContact
import love.forte.simbot.component.telegram.core.actor.TelegramMember
import love.forte.simbot.component.telegram.core.actor.*
import love.forte.simbot.component.telegram.core.message.TelegramMessageContent
import love.forte.simbot.component.telegram.core.message.TelegramMessageReceipt
import love.forte.simbot.component.telegram.core.time.unixDateTimestamp
Expand Down Expand Up @@ -57,18 +54,22 @@ public interface TelegramMessageRelatedEvent : TelegramEvent, TypeBasedTelegramM
public interface TelegramMessageEvent : TelegramMessageRelatedEvent, BasicTelegramMessageEvent

/**
* An event about [Message] from a [TelegramChatGroup] (chat.type == `"group"`)
* An event about [Message] from [TelegramChannel] or [TelegramChatGroup].
*
* @see TelegramChatGroupMessageEvent
* @see TelegramSuperGroupMessageEvent
* @see TelegramChannelMessageEvent
*
* @author ForteScarlet
*/
public interface TelegramChatGroupMessageEvent : TelegramMessageEvent, ChatGroupMessageEvent {
public interface TelegramGroupMessageEvent : TelegramMessageEvent, ChatGroupMessageEvent {
override val messageContent: TelegramMessageContent

/**
* The [TelegramChatGroup].
* The [TelegramGroup].
*/
@STP
override suspend fun content(): TelegramChatGroup
override suspend fun content(): TelegramGroup

/**
* The [sender][Message.from]'s [id][User.id]
Expand All @@ -78,82 +79,64 @@ public interface TelegramChatGroupMessageEvent : TelegramMessageEvent, ChatGroup

@STP
override suspend fun author(): TelegramMember
// TODO chat group member?

/**
* Reply to this message with [text].
*/
@ST
override suspend fun reply(text: String): TelegramMessageReceipt

/**
* Reply to this message with [message].
*/
@ST
override suspend fun reply(message: love.forte.simbot.message.Message): TelegramMessageReceipt

/**
* Reply to this message with [messageContent].
*/
@ST
override suspend fun reply(messageContent: MessageContent): TelegramMessageReceipt
}

/**
* An event about [Message] from a [TelegramChatGroup] (chat.type == `"supergroup"`)
* An event about [Message] from a [TelegramChatGroup] (chat.type == `"group"`)
*
* @author ForteScarlet
*/
public interface TelegramSuperGroupMessageEvent : TelegramMessageEvent, ChatGroupMessageEvent {
override val messageContent: TelegramMessageContent

public interface TelegramChatGroupMessageEvent : TelegramGroupMessageEvent {
/**
* The [TelegramChatGroup].
*/
@STP
override suspend fun content(): TelegramChatGroup

}

/**
* An event about [Message] from a [TelegramChatGroup] (chat.type == `"supergroup"`)
*
* @author ForteScarlet
*/
public interface TelegramSuperGroupMessageEvent : TelegramGroupMessageEvent {
/**
* The [sender][Message.from]'s [id][User.id]
* The [TelegramGroup].
*/
override val authorId: ID
get() = sourceContent.from!!.id.ID

@STP
override suspend fun author(): TelegramMember

@ST
override suspend fun reply(text: String): TelegramMessageReceipt

@ST
override suspend fun reply(message: love.forte.simbot.message.Message): TelegramMessageReceipt

@ST
override suspend fun reply(messageContent: MessageContent): TelegramMessageReceipt
override suspend fun content(): TelegramGroup
}

/**
* An event about [Message] from a [TelegramChannel] (chat.type == `"channel"`)
*
* @author ForteScarlet
*/
public interface TelegramChannelMessageEvent : TelegramMessageEvent, ChatGroupMessageEvent {
override val messageContent: TelegramMessageContent

public interface TelegramChannelMessageEvent : TelegramGroupMessageEvent {
/**
* The [TelegramChannel].
*/
@STP
override suspend fun content(): TelegramChannel

/**
* The [sender][Message.from]'s [id][User.id]
*/
override val authorId: ID
get() = sourceContent.from!!.id.ID

@STP
override suspend fun author(): TelegramMember

@ST
override suspend fun reply(text: String): TelegramMessageReceipt

@ST
override suspend fun reply(message: love.forte.simbot.message.Message): TelegramMessageReceipt

@ST
override suspend fun reply(messageContent: MessageContent): TelegramMessageReceipt
}

/**
Expand All @@ -162,6 +145,8 @@ public interface TelegramChannelMessageEvent : TelegramMessageEvent, ChatGroupMe
* @author ForteScarlet
*/
public interface TelegramPrivateMessageEvent : TelegramMessageEvent, ContactMessageEvent {
override val messageContent: TelegramMessageContent

/**
* The [TelegramContact].
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (c) 2024. ForteScarlet.
*
* This file is part of simbot-component-telegram.
*
* simbot-component-telegram is free software: you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* simbot-component-telegram is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License along with simbot-component-telegram.
* If not, see <https://www.gnu.org/licenses/>.
*/

package love.forte.simbot.component.telegram.core.event.internal

import love.forte.simbot.component.telegram.core.actor.TelegramMember
import love.forte.simbot.component.telegram.core.actor.internal.toTelegramMember
import love.forte.simbot.component.telegram.core.bot.internal.TelegramBotImpl
import love.forte.simbot.component.telegram.core.bot.requestDataBy
import love.forte.simbot.component.telegram.core.event.TelegramGroupMessageEvent
import love.forte.simbot.component.telegram.core.message.TelegramMessageContent
import love.forte.simbot.component.telegram.core.message.TelegramMessageReceipt
import love.forte.simbot.component.telegram.core.message.internal.TelegramMessageContentImpl
import love.forte.simbot.component.telegram.core.message.internal.toTelegramMessageReceipt
import love.forte.simbot.component.telegram.core.message.send
import love.forte.simbot.message.MessageContent
import love.forte.simbot.telegram.api.message.SendMessageApi
import love.forte.simbot.telegram.api.message.buildSendMessageApi
import love.forte.simbot.telegram.type.ChatId
import love.forte.simbot.telegram.type.Message
import love.forte.simbot.telegram.type.ReplyParameters


/**
*
* @author ForteScarlet
*/
internal abstract class AbstractTelegramGroupMessageEventImpl(
final override val bot: TelegramBotImpl,
final override val sourceContent: Message
) : TelegramGroupMessageEvent {
override val messageContent: TelegramMessageContent = TelegramMessageContentImpl(bot, sourceContent)

override suspend fun author(): TelegramMember {
// TODO from!!? check senderChat?
return sourceContent.from!!.toTelegramMember(bot)
}

override suspend fun reply(text: String): TelegramMessageReceipt {
return buildSendMessageApi(ChatId(sourceContent.chat.id), text) {
replyParameters = ReplyParameters(sourceContent.messageId)
}.requestDataBy(bot).toTelegramMessageReceipt(bot)
}

override suspend fun reply(message: love.forte.simbot.message.Message): TelegramMessageReceipt {
return bot.send(message, sourceContent.chat.id) {
SendMessageApi.builder().also {
it.replyParameters = ReplyParameters(messageId = sourceContent.messageId)
}
}
}

override suspend fun reply(messageContent: MessageContent): TelegramMessageReceipt {
return bot.send(
messageContent,
sourceContent.chat.id,
copyApiBlock = {
replyParameters = ReplyParameters(messageId = sourceContent.messageId)
},
builderFactory = {
SendMessageApi.builder().also {
it.replyParameters = ReplyParameters(messageId = sourceContent.messageId)
}
}
)
}
}
Loading

0 comments on commit 3e02e0c

Please sign in to comment.