Skip to content

Commit

Permalink
Add API methods for forum group topics
Browse files Browse the repository at this point in the history
  • Loading branch information
frececroka committed Mar 26, 2023
1 parent c41efdf commit 44971cc
Show file tree
Hide file tree
Showing 11 changed files with 734 additions and 0 deletions.
118 changes: 118 additions & 0 deletions telegram/src/main/kotlin/com/github/kotlintelegrambot/Bot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.github.kotlintelegrambot.entities.ChatAction
import com.github.kotlintelegrambot.entities.ChatId
import com.github.kotlintelegrambot.entities.ChatMember
import com.github.kotlintelegrambot.entities.ChatPermissions
import com.github.kotlintelegrambot.entities.ForumTopic
import com.github.kotlintelegrambot.entities.InlineKeyboardMarkup
import com.github.kotlintelegrambot.entities.Message
import com.github.kotlintelegrambot.entities.MessageEntity
Expand Down Expand Up @@ -245,6 +246,8 @@ class Bot private constructor(
* @param disableNotification sends the message silently - users will receive a notification
* with no sound.
* @param replyToMessageId if the message is a reply, ID of the original message.
* @param messageThreadId to send the message to a specific topic. Works only for forum
* supergroups.
* @param replyMarkup additional options - inline keyboard, custom reply keyboard,
* instructions to remove reply keyboard or to force a reply from the user.
*
Expand All @@ -257,6 +260,7 @@ class Bot private constructor(
disableWebPagePreview: Boolean? = null,
disableNotification: Boolean? = null,
replyToMessageId: Long? = null,
messageThreadId: Long? = null,
allowSendingWithoutReply: Boolean? = null,
replyMarkup: ReplyMarkup? = null
): TelegramBotResult<Message> = apiClient.sendMessage(
Expand All @@ -266,6 +270,7 @@ class Bot private constructor(
disableWebPagePreview,
disableNotification,
replyToMessageId,
messageThreadId,
allowSendingWithoutReply,
replyMarkup
)
Expand Down Expand Up @@ -1435,6 +1440,119 @@ class Bot private constructor(
chatId: ChatId
): TelegramBotResult<Boolean> = apiClient.deleteChatStickerSet(chatId)

/**
* Use this method to create a topic in a forum supergroup chat. The bot must be an
* administrator in the chat for this to work and must have the can_manage_topics administrator
* rights. Returns information about the created topic as a ForumTopic object.
*
* @param chatId Unique identifier for the target chat or username of the target supergroup (in
* the format @supergroupusername).
* @param name Topic name, 1-128 characters.
* @param iconColor Color of the topic icon in RGB format. Currently, must be one of 7322096
* (0x6FB9F0), 16766590 (0xFFD67E), 13338331 (0xCB86DB), 9367192 (0x8EEE98), 16749490
* (0xFF93B2), or 16478047 (0xFB6F5F).
* @param iconCustomEmojiId Unique identifier of the custom emoji shown as the topic icon. Use
* getForumTopicIconStickers to get all allowed custom emoji identifiers.
*/
fun createForumTopic(
chatId: ChatId,
name: String,
iconColor: Int? = null,
iconCustomEmojiId: String? = null,
): TelegramBotResult<ForumTopic> {
return apiClient.createForumTopic(
chatId,
name,
iconColor,
iconCustomEmojiId,
)
}

/**
* Use this method to edit name and icon of a topic in a forum supergroup chat. The bot must be
* an administrator in the chat for this to work and must have can_manage_topics administrator
* rights, unless it is the creator of the topic. Returns True on success.
*
* @param chatId Unique identifier for the target chat or username of the target supergroup (in
* the format @supergroupusername).
* @param messageThreadId Unique identifier for the target message thread of the forum topic.
* @param name New topic name, 0-128 characters. If not specified or empty, the current name of
* the topic will be kept.
* @param iconCustomEmojiId New unique identifier of the custom emoji shown as the topic icon.
* Use getForumTopicIconStickers to get all allowed custom emoji identifiers. Pass an empty
* string to remove the icon. If not specified, the current icon will be kept.
*/
fun editForumTopic(
chatId: ChatId,
messageThreadId: Long,
name: String? = null,
iconCustomEmojiId: String? = null,
): TelegramBotResult<Boolean> {
return apiClient.editForumTopic(
chatId,
messageThreadId,
name,
iconCustomEmojiId,
)
}

/**
* Use this method to close an open topic in a forum supergroup chat. The bot must be an
* administrator in the chat for this to work and must have the can_manage_topics administrator
* rights, unless it is the creator of the topic. Returns True on success.
*
* @param chatId Unique identifier for the target chat or username of the target supergroup (in
* the format @supergroupusername).
* @param messageThreadId Unique identifier for the target message thread of the forum topic.
*/
fun closeForumTopic(
chatId: ChatId,
messageThreadId: Long,
): TelegramBotResult<Boolean> {
return apiClient.closeForumTopic(
chatId,
messageThreadId,
)
}

/**
* Use this method to reopen a closed topic in a forum supergroup chat. The bot must be an
* administrator in the chat for this to work and must have the can_manage_topics administrator
* rights, unless it is the creator of the topic. Returns True on success.
*
* @param chatId Unique identifier for the target chat or username of the target supergroup (in
* the format @supergroupusername).
* @param messageThreadId Unique identifier for the target message thread of the forum topic.
*/
fun reopenForumTopic(
chatId: ChatId,
messageThreadId: Long,
): TelegramBotResult<Boolean> {
return apiClient.reopenForumTopic(
chatId,
messageThreadId,
)
}

/**
* Use this method to delete a forum topic along with all its messages in a forum supergroup
* chat. The bot must be an administrator in the chat for this to work and must have the
* can_delete_messages administrator rights. Returns True on success.
*
* @param chatId Unique identifier for the target chat or username of the target supergroup (in
* the format @supergroupusername).
* @param messageThreadId Unique identifier for the target message thread of the forum topic.
*/
fun deleteForumTopic(
chatId: ChatId,
messageThreadId: Long,
): TelegramBotResult<Boolean> {
return apiClient.deleteForumTopic(
chatId,
messageThreadId,
)
}

/**
* Use this method to send answers to callback queries sent from inline keyboards. The answer
* will be displayed to the user as a notification at the top of the chat screen or as an
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.kotlintelegrambot.entities

import com.google.gson.annotations.SerializedName as Name

data class ForumTopic(
@Name("message_thread_id") val messageThreadId: Long,
val name: String,
@Name("icon_color") val iconColor: Int,
@Name("icon_custom_emoji_id") val iconCustomEmojiId: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.github.kotlintelegrambot.entities.ChatAction
import com.github.kotlintelegrambot.entities.ChatId
import com.github.kotlintelegrambot.entities.ChatMember
import com.github.kotlintelegrambot.entities.ChatPermissions
import com.github.kotlintelegrambot.entities.ForumTopic
import com.github.kotlintelegrambot.entities.InlineKeyboardMarkup
import com.github.kotlintelegrambot.entities.Message
import com.github.kotlintelegrambot.entities.MessageEntity
Expand Down Expand Up @@ -191,6 +192,7 @@ internal class ApiClient(
disableWebPagePreview: Boolean?,
disableNotification: Boolean?,
replyToMessageId: Long?,
messageThreadId: Long?,
allowSendingWithoutReply: Boolean?,
replyMarkup: ReplyMarkup?
): TelegramBotResult<Message> = service.sendMessage(
Expand All @@ -200,6 +202,7 @@ internal class ApiClient(
disableWebPagePreview,
disableNotification,
replyToMessageId,
messageThreadId,
allowSendingWithoutReply,
replyMarkup
).runApiOperation()
Expand Down Expand Up @@ -1290,6 +1293,68 @@ internal class ApiClient(
)
}

/**
* Forum topics
*/

fun createForumTopic(
chatId: ChatId,
name: String,
iconColor: Int? = null,
iconCustomEmojiId: String? = null,
): TelegramBotResult<ForumTopic> {
return service.createForumTopic(
chatId,
name,
iconColor,
iconCustomEmojiId,
).runApiOperation()
}

fun editForumTopic(
chatId: ChatId,
messageThreadId: Long,
name: String? = null,
iconCustomEmojiId: String? = null,
): TelegramBotResult<Boolean> {
return service.editForumTopic(
chatId,
messageThreadId,
name,
iconCustomEmojiId,
).runApiOperation()
}

fun closeForumTopic(
chatId: ChatId,
messageThreadId: Long,
): TelegramBotResult<Boolean> {
return service.closeForumTopic(
chatId,
messageThreadId,
).runApiOperation()
}

fun reopenForumTopic(
chatId: ChatId,
messageThreadId: Long,
): TelegramBotResult<Boolean> {
return service.reopenForumTopic(
chatId,
messageThreadId,
).runApiOperation()
}

fun deleteForumTopic(
chatId: ChatId,
messageThreadId: Long,
): TelegramBotResult<Boolean> {
return service.deleteForumTopic(
chatId,
messageThreadId,
).runApiOperation()
}

fun answerInlineQuery(
inlineQueryId: String,
inlineQueryResults: List<InlineQueryResult>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ internal object ApiConstants {
const val DISABLE_CONTENT_TYPE_DETECTION = "disable_content_type_detection"
const val DISABLE_NOTIFICATION = "disable_notification"
const val REPLY_TO_MESSAGE_ID = "reply_to_message_id"
const val MESSAGE_THREAD_ID = "message_thread_id"
const val ALLOW_SENDING_WITHOUT_REPLY = "allow_sending_without_reply"
const val REPLY_MARKUP = "reply_markup"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.github.kotlintelegrambot.entities.Chat
import com.github.kotlintelegrambot.entities.ChatAction
import com.github.kotlintelegrambot.entities.ChatId
import com.github.kotlintelegrambot.entities.ChatMember
import com.github.kotlintelegrambot.entities.ForumTopic
import com.github.kotlintelegrambot.entities.Message
import com.github.kotlintelegrambot.entities.MessageId
import com.github.kotlintelegrambot.entities.ParseMode
Expand Down Expand Up @@ -115,6 +116,7 @@ internal interface ApiService {
@Field("disable_web_page_preview") disableWebPagePreview: Boolean?,
@Field(ApiConstants.DISABLE_NOTIFICATION) disableNotification: Boolean?,
@Field(ApiConstants.REPLY_TO_MESSAGE_ID) replyToMessageId: Long?,
@Field(ApiConstants.MESSAGE_THREAD_ID) messageThreadId: Long?,
@Field(ApiConstants.ALLOW_SENDING_WITHOUT_REPLY) allowSendingWithoutReply: Boolean?,
@Field(ApiConstants.REPLY_MARKUP) replyMarkup: ReplyMarkup?
): Call<Response<Message>>
Expand Down Expand Up @@ -833,6 +835,49 @@ internal interface ApiService {
@Field("error_message") errorMessage: String? = null
): Call<Response<Boolean>>

/**
* Forum topics
*/

@FormUrlEncoded
@POST("createForumTopic")
fun createForumTopic(
@Field(ApiConstants.CHAT_ID) chatId: ChatId,
@Field("name") name: String,
@Field("icon_color") iconColor: Int?,
@Field("icon_custom_emoji_id") iconCustomEmojiId: String?,
): Call<Response<ForumTopic>>

@FormUrlEncoded
@POST("editForumTopic")
fun editForumTopic(
@Field(ApiConstants.CHAT_ID) chatId: ChatId,
@Field(ApiConstants.MESSAGE_THREAD_ID) messageThreadId: Long,
@Field("name") name: String?,
@Field("icon_custom_emoji_id") iconCustomEmojiId: String?,
): Call<Response<Boolean>>

@FormUrlEncoded
@POST("closeForumTopic")
fun closeForumTopic(
@Field(ApiConstants.CHAT_ID) chatId: ChatId,
@Field(ApiConstants.MESSAGE_THREAD_ID) messageThreadId: Long,
): Call<Response<Boolean>>

@FormUrlEncoded
@POST("reopenForumTopic")
fun reopenForumTopic(
@Field(ApiConstants.CHAT_ID) chatId: ChatId,
@Field(ApiConstants.MESSAGE_THREAD_ID) messageThreadId: Long,
): Call<Response<Boolean>>

@FormUrlEncoded
@POST("deleteForumTopic")
fun deleteForumTopic(
@Field(ApiConstants.CHAT_ID) chatId: ChatId,
@Field(ApiConstants.MESSAGE_THREAD_ID) messageThreadId: Long,
): Call<Response<Boolean>>

@FormUrlEncoded
@POST("answerInlineQuery")
fun answerInlineQuery(
Expand Down

0 comments on commit 44971cc

Please sign in to comment.