Skip to content

Commit

Permalink
消息事件相关的实现和调整
Browse files Browse the repository at this point in the history
  • Loading branch information
ForteScarlet committed Apr 10, 2024
1 parent b6129e2 commit 5ba68b4
Show file tree
Hide file tree
Showing 19 changed files with 623 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,12 @@ public class SendMessageApi private constructor(body: Body) : SimpleBodyTelegram
/**
* @see Body.text
*/
public var text: String? = null
public var text: StringBuilder? = null

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

// Optional

Expand Down Expand Up @@ -264,7 +269,7 @@ public class SendMessageApi private constructor(body: Body) : SimpleBodyTelegram

return Body(
chatId = chatId,
text = text,
text = text.toString(),
messageThreadId = messageThreadId,
parseMode = parseMode,
entities = entities,
Expand Down Expand Up @@ -301,6 +306,6 @@ public inline fun buildSendMessageApi(
block: Builder.() -> Unit = {}
): SendMessageApi = buildSendMessageApi {
this.chatId = chatId
this.text = text
this.text = StringBuilder(text)
block()
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public interface TelegramChatAware {
}

/**
* A Telegram [Chat] representing a group ([Chat.type] == [ChatType.GROUP].value)
* A Telegram [Chat] representing a group ([Chat.type] == [ChatType.GROUP] or [ChatType.SUPERGROUP])
* or a channel ([Chat.type] == [ChatType.CHANNEL].value).
*
* @see TelegramChatGroup
Expand Down Expand Up @@ -114,8 +114,7 @@ public interface TelegramChatGroupActor : TelegramChatAware, ChatGroup {


/**
*
* A Telegram [Chat] representing a group ([Chat.type] == [ChatType.GROUP].value).
* A Telegram [Chat] representing a group ([Chat.type] == [ChatType.GROUP] or [ChatType.SUPERGROUP]).
*
* @author ForteScarlet
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ public interface TelegramMember : TelegramUser, Member {
sendToMemberIsUnsupported()
}

internal const val SEND_TO_MEMBER_IS_UNSUPPORTED = "Telegram bot cannot proactively send a message to a member " +
"because the private chat id is unknown."
internal const val SEND_TO_MEMBER_IS_UNSUPPORTED =
"Telegram bot cannot proactively send a message to a member " +
"because the private chat id is unknown."

internal fun sendToMemberIsUnsupported(): Nothing =
throw UnsupportedOperationException(SEND_TO_MEMBER_IS_UNSUPPORTED)
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,19 @@
package love.forte.simbot.component.telegram.core.actor.internal

import love.forte.simbot.common.collectable.Collectable
import love.forte.simbot.common.collectable.emptyCollectable
import love.forte.simbot.common.id.ID
import love.forte.simbot.common.id.toInt
import love.forte.simbot.component.telegram.core.actor.TelegramChatGroupActor
import love.forte.simbot.component.telegram.core.actor.TelegramMember
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.message.TelegramMessageReceipt
import love.forte.simbot.component.telegram.core.message.internal.toTelegramMessageReceipt
import love.forte.simbot.component.telegram.core.message.send
import love.forte.simbot.definition.Role
import love.forte.simbot.message.Message
import love.forte.simbot.message.MessageContent
import love.forte.simbot.telegram.api.chat.GetChatMemberApi
import love.forte.simbot.telegram.api.chat.GetChatMemberCountApi
import love.forte.simbot.telegram.api.message.SendMessageApi
import love.forte.simbot.telegram.stdlib.bot.requestDataBy
import love.forte.simbot.telegram.type.*


Expand All @@ -46,7 +43,7 @@ internal abstract class AbstractTelegramChatGroupActor : TelegramChatGroupActor
abstract override val source: Chat

override val roles: Collectable<Role>
get() = emptyCollectable() // TODO("Not yet implemented")
get() = TODO("Not yet implemented")

override suspend fun botAsMember(): TelegramMember {
return bot.queryUserInfo().toTelegramMember(bot)
Expand All @@ -70,15 +67,14 @@ internal abstract class AbstractTelegramChatGroupActor : TelegramChatGroupActor
}

override suspend fun send(text: String): TelegramMessageReceipt {
val sent = SendMessageApi.create(ChatId(source.id), text).requestDataBy(bot.source)
return sent.toTelegramMessageReceipt(bot)
return bot.send(text, source.id)
}

override suspend fun send(message: Message): TelegramMessageReceipt {
TODO("Not yet implemented")
return bot.send(message, source.id)
}

override suspend fun send(messageContent: MessageContent): TelegramMessageReceipt {
TODO("Not yet implemented")
return bot.send(messageContent, source.id)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@ package love.forte.simbot.component.telegram.core.actor.internal

import love.forte.simbot.component.telegram.core.bot.internal.TelegramBotImpl
import love.forte.simbot.component.telegram.core.message.TelegramMessageReceipt
import love.forte.simbot.component.telegram.core.message.internal.toTelegramMessageReceipt
import love.forte.simbot.component.telegram.core.message.send
import love.forte.simbot.message.Message
import love.forte.simbot.message.MessageContent
import love.forte.simbot.telegram.api.message.SendMessageApi
import love.forte.simbot.telegram.stdlib.bot.requestDataBy
import love.forte.simbot.telegram.type.Chat
import love.forte.simbot.telegram.type.ChatId
import love.forte.simbot.telegram.type.User
import kotlin.coroutines.CoroutineContext

Expand All @@ -42,16 +39,15 @@ internal class TelegramUserContactImpl(
override val coroutineContext: CoroutineContext = bot.subContext

override suspend fun send(messageContent: MessageContent): TelegramMessageReceipt {
TODO("Not yet implemented")
return bot.send(messageContent, sourceChat.id)
}

override suspend fun send(message: Message): TelegramMessageReceipt {
TODO("Not yet implemented")
return bot.send(message, sourceChat.id)
}

override suspend fun send(text: String): TelegramMessageReceipt {
val sent = SendMessageApi.create(ChatId(sourceChat.id), text).requestDataBy(bot.source)
return sent.toTelegramMessageReceipt(bot)
return bot.send(text, sourceChat.id)
}

override fun toString(): String =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,12 @@ public interface TelegramBot : Bot {


override val groupRelation: GroupRelation?
get() = null // TODO
get() = null // TODO?

override val guildRelation: GuildRelation?
get() = null // TODO
get() = null // TODO?

override val contactRelation: ContactRelation?
get() = null // TODO
get() = null // TODO?

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import love.forte.simbot.component.telegram.core.event.TelegramUnsupportedEvent
import love.forte.simbot.component.telegram.core.event.internal.TelegramChannelMessageEventImpl
import love.forte.simbot.component.telegram.core.event.internal.TelegramChatGroupMessageEventImpl
import love.forte.simbot.component.telegram.core.event.internal.TelegramPrivateMessageEventImpl
import love.forte.simbot.component.telegram.core.event.internal.TelegramSuperGroupMessageEventImpl
import love.forte.simbot.event.Event
import love.forte.simbot.event.EventDispatcher
import love.forte.simbot.event.onEachError
Expand Down Expand Up @@ -170,8 +171,16 @@ internal fun subscribeInternalProcessor(
}

// supergroup?
ChatType.SUPERGROUP -> {
pushEvent(
TelegramSuperGroupMessageEventImpl(
bot = bot,
sourceEvent = context,
sourceContent = value
)
)
}

// TODO others
else -> onMismatchUpdateEvent(name, value, update, context)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ 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.message.TelegramMessageContent
import love.forte.simbot.component.telegram.core.message.TelegramMessageReceipt
import love.forte.simbot.component.telegram.core.time.unixDateTimestamp
Expand Down Expand Up @@ -76,7 +77,7 @@ public interface TelegramChatGroupMessageEvent : TelegramMessageEvent, ChatGroup
get() = sourceContent.from!!.id.ID

@STP
override suspend fun author(): love.forte.simbot.component.telegram.core.actor.TelegramMember
override suspend fun author(): TelegramMember
// TODO chat group member?

@ST
Expand All @@ -89,6 +90,39 @@ public interface TelegramChatGroupMessageEvent : TelegramMessageEvent, ChatGroup
override suspend fun reply(messageContent: MessageContent): TelegramMessageReceipt
}

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

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

/**
* 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
}

/**
* An event about [Message] from a [TelegramChannel] (chat.type == `"channel"`)
*
Expand All @@ -110,8 +144,7 @@ public interface TelegramChannelMessageEvent : TelegramMessageEvent, ChatGroupMe
get() = sourceContent.from!!.id.ID

@STP
override suspend fun author(): love.forte.simbot.component.telegram.core.actor.TelegramMember
// TODO chat group member?
override suspend fun author(): TelegramMember

@ST
override suspend fun reply(text: String): TelegramMessageReceipt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,14 @@ import love.forte.simbot.component.telegram.core.actor.TelegramMember
import love.forte.simbot.component.telegram.core.actor.internal.toTelegramChannel
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.StdlibEvent
import love.forte.simbot.component.telegram.core.event.TelegramChannelMessageEvent
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.toCopyApi
import love.forte.simbot.component.telegram.core.message.send
import love.forte.simbot.message.MessageContent
import love.forte.simbot.telegram.api.message.buildSendMessageApi
import love.forte.simbot.telegram.type.ChatId
import love.forte.simbot.telegram.api.message.SendMessageApi
import love.forte.simbot.telegram.type.Message
import love.forte.simbot.telegram.type.ReplyParameters

Expand All @@ -55,28 +52,37 @@ internal class TelegramChannelMessageEventImpl(
}

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

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

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

override suspend fun reply(messageContent: MessageContent): TelegramMessageReceipt {
if (messageContent is TelegramMessageContent) {
return messageContent.source.toCopyApi(ChatId(sourceContent.chat.id)) {
return bot.send(
messageContent,
sourceContent.chat.id,
copyApiBlock = {
replyParameters = ReplyParameters(messageId = sourceContent.messageId)
}.requestDataBy(bot).toTelegramMessageReceipt(bot, sourceContent.chat.id)
}

return reply(messageContent.messages)
},
builderFactory = {
SendMessageApi.builder().also {
it.replyParameters = ReplyParameters(messageId = sourceContent.messageId)
}
}
)
}

override fun toString(): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ 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.toCopyApi
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
Expand Down Expand Up @@ -63,17 +64,26 @@ internal class TelegramChatGroupMessageEventImpl(
}

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

override suspend fun reply(messageContent: MessageContent): TelegramMessageReceipt {
if (messageContent is TelegramMessageContent) {
return messageContent.source.toCopyApi(ChatId(sourceContent.chat.id)) {
return bot.send(
messageContent,
sourceContent.chat.id,
copyApiBlock = {
replyParameters = ReplyParameters(messageId = sourceContent.messageId)
}.requestDataBy(bot).toTelegramMessageReceipt(bot, sourceContent.chat.id)
}

return reply(messageContent.messages)
},
builderFactory = {
SendMessageApi.builder().also {
it.replyParameters = ReplyParameters(messageId = sourceContent.messageId)
}
}
)
}

override fun toString(): String {
Expand Down
Loading

0 comments on commit 5ba68b4

Please sign in to comment.