Skip to content

Commit c3e8006

Browse files
committed
Dev updates
1 parent 3932ca7 commit c3e8006

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+833
-106
lines changed

pom.xml

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66

77
<groupId>io.cutebot</groupId>
88
<artifactId>kotlin-telegram-bot</artifactId>
9-
<version>0.0.1-SNAPSHOT</version>
9+
<version>1.0.0-SNAPSHOT</version>
1010

1111
<properties>
1212
<kotlin.version>1.3.72</kotlin.version>
13+
<jackson.version>2.11.1</jackson.version>
14+
<apache.http.version>4.5.12</apache.http.version>
1315
</properties>
1416

1517
<dependencies>
@@ -27,30 +29,34 @@
2729
<dependency>
2830
<groupId>com.fasterxml.jackson.core</groupId>
2931
<artifactId>jackson-databind</artifactId>
30-
<version>2.11.1</version>
32+
<version>${jackson.version}</version>
3133
</dependency>
3234
<dependency>
3335
<groupId>com.fasterxml.jackson.module</groupId>
3436
<artifactId>jackson-module-kotlin</artifactId>
35-
<version>2.11.1</version>
37+
<version>${jackson.version}</version>
3638
</dependency>
3739
<dependency>
3840
<groupId>org.apache.httpcomponents</groupId>
3941
<artifactId>httpclient</artifactId>
40-
<version>4.5.12</version>
42+
<version>${apache.http.version}</version>
4143
</dependency>
4244
<dependency>
4345
<groupId>org.apache.httpcomponents</groupId>
4446
<artifactId>httpmime</artifactId>
45-
<version>4.5.12</version>
47+
<version>${apache.http.version}</version>
4648
</dependency>
4749
<dependency>
4850
<groupId>org.slf4j</groupId>
4951
<artifactId>slf4j-api</artifactId>
5052
<version>1.7.30</version>
5153
</dependency>
52-
53-
54+
<dependency>
55+
<groupId>org.junit.jupiter</groupId>
56+
<artifactId>junit-jupiter</artifactId>
57+
<version>5.6.2</version>
58+
<scope>test</scope>
59+
</dependency>
5460
</dependencies>
5561

5662
<build>
@@ -80,6 +86,20 @@
8086
<jvmTarget>1.8</jvmTarget>
8187
</configuration>
8288
</plugin>
89+
<plugin>
90+
<groupId>org.apache.maven.plugins</groupId>
91+
<artifactId>maven-source-plugin</artifactId>
92+
<version>3.2.0</version>
93+
<executions>
94+
<execution>
95+
<id>attach-sources</id>
96+
<phase>verify</phase>
97+
<goals>
98+
<goal>jar-no-fork</goal>
99+
</goals>
100+
</execution>
101+
</executions>
102+
</plugin>
83103
</plugins>
84104
</build>
85105

src/main/kotlin/io/cutebot/telegram/BotBlock.kt

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package io.cutebot.telegram
22

3-
import io.cutebot.telegram.client.model.TgMessage
3+
import io.cutebot.telegram.bot.model.RawMessage
44
import io.cutebot.telegram.interaction.model.ChatAnswer
55

66
interface BotHandler {
7-
fun handleMessage(message: TgMessage): ChatAnswer
7+
fun handleMessage(message: RawMessage): ChatAnswer
88
}
Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
package io.cutebot.telegram.bot
22

33
import io.cutebot.telegram.BotHandler
4-
import io.cutebot.telegram.client.model.TgCallbackQuery
5-
import io.cutebot.telegram.client.model.TgSendMessageUpdate
4+
import io.cutebot.telegram.client.model.TgBotCommands
65
import io.cutebot.telegram.client.model.inline.TgAnswerInlineQuery
76
import io.cutebot.telegram.client.model.inline.TgInlineQuery
87

9-
abstract class Bot: BotHandler {
10-
abstract fun getToken(): String
8+
interface Bot: BotHandler {
9+
fun getToken(): String
1110

1211
fun handleInlineQuery(inlineQuery: TgInlineQuery): TgAnswerInlineQuery {
1312
return TgAnswerInlineQuery("")
1413
}
1514

16-
fun handleCallbackQuery(callbackQuery: TgCallbackQuery): TgSendMessageUpdate {
17-
TODO()
15+
/**
16+
* Commands to setup bot's system menu on startup.
17+
* Pass null to not change it
18+
*/
19+
fun getCommands(): TgBotCommands? {
20+
return TgBotCommands(emptyList())
1821
}
1922

20-
}
23+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package io.cutebot.telegram.bot
2+
3+
import io.cutebot.telegram.bot.block.BotBlock
4+
import io.cutebot.telegram.bot.command.Command
5+
import io.cutebot.telegram.bot.commandextractor.CommandExtractor
6+
import io.cutebot.telegram.bot.commandextractor.DefaultCommandExtractor
7+
import io.cutebot.telegram.bot.model.RawMessage
8+
import io.cutebot.telegram.client.model.TgBotCommand
9+
import io.cutebot.telegram.client.model.TgBotCommands
10+
import io.cutebot.telegram.interaction.model.ChatAnswer
11+
import org.slf4j.LoggerFactory
12+
13+
abstract class CommandsStatefulBot(
14+
currentBlock: BotBlock,
15+
commands: List<Command>,
16+
private val commandExtractor: CommandExtractor = DefaultCommandExtractor()
17+
): StatefulBot(currentBlock) {
18+
19+
val commandsMap: Map<String, Command> = commands
20+
.map { it.getCommand() to it }
21+
.toMap()
22+
23+
override fun handleMessage(message: RawMessage): ChatAnswer {
24+
if (message.text != null && commandExtractor.isCommand(message.text)) {
25+
val (command, query) = commandExtractor.extractCommand(message.text)
26+
27+
val commandBlock = commandsMap[command]
28+
29+
if (commandBlock != null) {
30+
currentBlock = commandBlock.handleCommand(query, message)
31+
return currentBlock.getAnswer()
32+
}
33+
}
34+
35+
return chatAnswer(message)
36+
}
37+
38+
override fun getCommands(): TgBotCommands? {
39+
var commands = commandsMap.values
40+
.filter { it.isSystemCommand() }
41+
42+
commands.map { it.getCommand() }
43+
.filter { it.isEmpty() || it[0] != '/' }
44+
.forEach { warnReason(it, "system command must begin on '/' symbol") }
45+
46+
commands.map { it.getCommand() }
47+
.filter { it.length == 1 }
48+
.forEach { warnReason(it, "system command cannot have just 1 symbol") }
49+
50+
commands.filter { it.getCommandDescription().length < 3 }
51+
.forEach { warnReason(it.getCommand(), "description length less than 3 symbols") }
52+
53+
commands = commands
54+
.filter { it.getCommand().length > 1 && it.getCommand()[0] == '/' }
55+
.filter { it.getCommandDescription().length >= 3 }
56+
57+
if (commands.size > 100) {
58+
log.warn("Count of system commands more than 100 and will be truncated")
59+
commands = commands.subList(0, 100)
60+
}
61+
62+
val tgCommands = commands.map {
63+
TgBotCommand(it.getCommand().substring(1), it.getCommandDescription())
64+
}
65+
66+
return TgBotCommands(tgCommands)
67+
}
68+
69+
companion object {
70+
private val log = LoggerFactory.getLogger(CommandsStatefulBot::class.java)
71+
72+
fun warnReason(command: String, reason: String) {
73+
log.warn("command {} cannot be system: {}", command, reason)
74+
}
75+
}
76+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.cutebot.telegram.bot
2+
3+
import io.cutebot.telegram.bot.block.BotBlock
4+
import io.cutebot.telegram.bot.command.Command
5+
6+
class DefaultCommandsStatefulBot(
7+
private val token: String,
8+
currentBlock: BotBlock,
9+
commands: List<Command>
10+
): CommandsStatefulBot(currentBlock, commands) {
11+
12+
override fun getToken(): String {
13+
return token
14+
}
15+
16+
}

src/main/kotlin/io/cutebot/telegram/bot/SimpleTextBot.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package io.cutebot.telegram.bot
22

3+
import io.cutebot.telegram.bot.model.RawMessage
34
import io.cutebot.telegram.client.model.TgChat
4-
import io.cutebot.telegram.client.model.TgMessage
55
import io.cutebot.telegram.client.model.TgUser
66
import io.cutebot.telegram.interaction.model.ChatAnswer
77

8-
abstract class SimpleTextBot: Bot() {
8+
abstract class SimpleTextBot: Bot {
99

1010
abstract fun handleText(text: String, from: TgUser, chat: TgChat): String
1111

12-
override fun handleMessage(message: TgMessage): ChatAnswer {
12+
override fun handleMessage(message: RawMessage): ChatAnswer {
1313
if (message.text != null && message.from != null) {
1414
val answerText = handleText(message.text, message.from, message.chat)
1515
return ChatAnswer.text(answerText)

src/main/kotlin/io/cutebot/telegram/bot/StatefulBot.kt

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
11
package io.cutebot.telegram.bot
22

3-
import io.cutebot.telegram.BotBlock
4-
import io.cutebot.telegram.client.model.TgMessage
3+
import io.cutebot.telegram.bot.block.BotBlock
4+
import io.cutebot.telegram.bot.model.DocumentMessage
5+
import io.cutebot.telegram.bot.model.PhotoMessage
6+
import io.cutebot.telegram.bot.model.RawMessage
7+
import io.cutebot.telegram.bot.model.TextMessage
58
import io.cutebot.telegram.interaction.model.ChatAnswer
69

710
abstract class StatefulBot(
8-
private var currentBlock: BotBlock
9-
): Bot() {
11+
protected var currentBlock: BotBlock
12+
): Bot {
1013

11-
override fun handleMessage(message: TgMessage): ChatAnswer {
14+
override fun handleMessage(message: RawMessage): ChatAnswer {
15+
return chatAnswer(message)
16+
}
17+
18+
internal fun chatAnswer(message: RawMessage): ChatAnswer {
1219
if (message.text != null) {
13-
currentBlock = currentBlock.handleText(message)
14-
return currentBlock.getMessage()
20+
currentBlock = currentBlock.handleText(TextMessage(message, message.text))
21+
return currentBlock.getAnswer()
22+
}
23+
24+
if (message.photo != null) {
25+
currentBlock = currentBlock.handlePhoto(PhotoMessage(message, message.photo))
26+
return currentBlock.getAnswer()
27+
}
28+
29+
if (message.document != null) {
30+
currentBlock = currentBlock.handleDocument(DocumentMessage(message, message.document))
31+
return currentBlock.getAnswer()
1532
}
1633

1734
return ChatAnswer.noAnswer()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.cutebot.telegram.bot.block
2+
3+
import io.cutebot.telegram.bot.model.DocumentMessage
4+
import io.cutebot.telegram.bot.model.PhotoMessage
5+
import io.cutebot.telegram.bot.model.TextMessage
6+
import io.cutebot.telegram.interaction.model.ChatAnswer
7+
8+
interface BotBlock {
9+
10+
fun getAnswer(): ChatAnswer
11+
12+
fun handleText(message: TextMessage): BotBlock
13+
fun handlePhoto(message: PhotoMessage): BotBlock
14+
fun handleDocument(message: DocumentMessage): BotBlock
15+
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package io.cutebot.telegram.bot.block
2+
3+
import io.cutebot.telegram.bot.model.DocumentMessage
4+
import io.cutebot.telegram.bot.model.PhotoMessage
5+
6+
interface BotTextBlock: BotBlock {
7+
override fun handlePhoto(message: PhotoMessage): BotBlock {
8+
return this
9+
}
10+
11+
override fun handleDocument(message: DocumentMessage): BotBlock {
12+
return this
13+
}
14+
}

0 commit comments

Comments
 (0)