Content-Length: 415854 | pFad | http://github.com/pengrad/java-telegram-bot-api/commit/6b9f2a139f3f2e53eeb6e8a96dcbdaf1a9bacf16

79 Rewrite SendMessage to Kotlin · pengrad/java-telegram-bot-api@6b9f2a1 · GitHub
Skip to content

Commit 6b9f2a1

Browse files
committed
Rewrite SendMessage to Kotlin
1 parent 2d77199 commit 6b9f2a1

File tree

3 files changed

+70
-32
lines changed

3 files changed

+70
-32
lines changed

library/src/main/java/com/pengrad/telegrambot/request/SendMessage.java

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.pengrad.telegrambot.request
2+
3+
import com.pengrad.telegrambot.model.LinkPreviewOptions
4+
import com.pengrad.telegrambot.model.MessageEntity
5+
import com.pengrad.telegrambot.model.request.ParseMode
6+
import com.pengrad.telegrambot.utility.kotlin.optionalRequestParameter
7+
import com.pengrad.telegrambot.utility.kotlin.requestParameter
8+
9+
class SendMessage private constructor(
10+
chatId: Long? = null,
11+
channelUsername: String? = null,
12+
13+
text: String
14+
) : KAbstractSendRequest<SendMessage>(
15+
chatId = chatId,
16+
channelUsername = channelUsername,
17+
) {
18+
19+
constructor(chatId: Long, text: String) : this(
20+
chatId = chatId,
21+
channelUsername = null,
22+
text = text
23+
)
24+
25+
constructor(channelUsername: String, text: String) : this(
26+
chatId = null,
27+
channelUsername = channelUsername,
28+
text = text
29+
)
30+
31+
@Deprecated("Use constructor with chatId or channelUsername instead", ReplaceWith("SendMessage(chatId, text)"))
32+
constructor(chatId: Any, text: String) : this(
33+
chatId = (chatId as? Number)?.toLong(),
34+
channelUsername = chatId as? String,
35+
text = text
36+
) {
37+
checkDeprecatedConstructorParameters()
38+
}
39+
40+
@Suppress("NOTHING_TO_INLINE")
41+
private inline fun checkDeprecatedConstructorParameters() {
42+
if (this.chatId == null && this.channelUsername == null) {
43+
throw IllegalArgumentException("chatId parameter must be either Long or String")
44+
}
45+
}
46+
47+
val text: String by requestParameter(text)
48+
49+
var parseMode: ParseMode? by optionalRequestParameter()
50+
var entities: List<MessageEntity>? by optionalRequestParameter()
51+
var linkPreviewOptions: LinkPreviewOptions? by optionalRequestParameter()
52+
53+
fun parseMode(parseMode: ParseMode) = applySelf { this.parseMode = parseMode }
54+
55+
fun entities(entities: List<MessageEntity>) = applySelf { this.entities = entities }
56+
57+
fun entities(vararg entities: MessageEntity) = entities(entities.toList())
58+
59+
fun linkPreviewOptions(linkPreviewOptions: LinkPreviewOptions) = applySelf { this.linkPreviewOptions = linkPreviewOptions}
60+
61+
}

library/src/main/java/com/pengrad/telegrambot/utility/kotlin/extension/request/SendMessageExtension.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ inline fun TelegramAware.sendMessage(
88
chatId: Long,
99
text: String,
1010
modifier: SendMessage.() -> Unit = {}
11-
) = this.execute(SendMessage(chatId, text), modifier)
11+
) = this.execute(SendMessage(
12+
chatId = chatId,
13+
text = text
14+
), modifier)
1215

1316
inline fun TelegramAware.sendMessage(
14-
chatId: String,
17+
channelUsername: String,
1518
text: String,
1619
modifier: SendMessage.() -> Unit = {}
17-
) = this.execute(SendMessage(chatId, text), modifier)
20+
) = this.execute(SendMessage(
21+
channelUsername = channelUsername,
22+
text = text
23+
), modifier)

0 commit comments

Comments
 (0)








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/pengrad/java-telegram-bot-api/commit/6b9f2a139f3f2e53eeb6e8a96dcbdaf1a9bacf16

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy