File tree Expand file tree Collapse file tree 4 files changed +62
-3
lines changed
kotlin/by/jprof/coding/problems/bot Expand file tree Collapse file tree 4 files changed +62
-3
lines changed Original file line number Diff line number Diff line change 1
1
package by.jprof.coding.problems.bot.config
2
2
3
+ import dev.inmo.tgbotapi.bot.Ktor.telegramBot
4
+ import dev.inmo.tgbotapi.bot.RequestsExecutor
5
+ import dev.inmo.tgbotapi.bot.TelegramBot
6
+ import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
7
+ import dev.inmo.tgbotapi.types.message.abstracts.Message
8
+ import dev.inmo.tgbotapi.types.message.content.TextContent
3
9
import io.r2dbc.spi.ConnectionFactories
4
10
import io.r2dbc.spi.ConnectionFactory
5
11
import org.springframework.context.annotation.Bean
@@ -10,8 +16,18 @@ import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories
10
16
@Configuration
11
17
@EnableR2dbcRepositories
12
18
class ApplicationConfiguration : AbstractR2dbcConfiguration () {
19
+ companion object {
20
+ val BOT_TOKEN = System .getenv(" BOT_TOKEN" )!!
21
+ }
13
22
@Bean
14
23
override fun connectionFactory () : ConnectionFactory {
15
24
return ConnectionFactories .get(System .getenv(" DB_URL" ))
16
25
}
17
- }
26
+
27
+ @Bean
28
+ fun tgBot () : TelegramBot {
29
+ return telegramBot(BOT_TOKEN )
30
+ }
31
+ }
32
+
33
+ val Message .text get() = ((this as ? ContentMessage <* >)?.content as ? TextContent )?.text
Original file line number Diff line number Diff line change
1
+ package by.jprof.coding.problems.bot.hook
2
+
3
+ import by.jprof.coding.problems.bot.service.TgUpdateHandlingService
4
+ import dev.inmo.tgbotapi.types.update.abstracts.Update
5
+ import org.springframework.http.HttpStatus
6
+ import org.springframework.http.MediaType
7
+ import org.springframework.web.bind.annotation.RequestMapping
8
+ import org.springframework.web.bind.annotation.RequestMethod
9
+ import org.springframework.web.bind.annotation.ResponseStatus
10
+ import org.springframework.web.bind.annotation.RestController
11
+ import reactor.core.publisher.Mono
12
+
13
+ @RestController
14
+ @RequestMapping(" /" )
15
+ class WebhookListener (private val tgUpdateHandlingService : TgUpdateHandlingService ) {
16
+
17
+ @RequestMapping(method = [RequestMethod .POST ], produces = [MediaType .APPLICATION_JSON_VALUE ])
18
+ @ResponseStatus(HttpStatus .OK )
19
+ suspend fun processUpdate (update : Update ) : Mono <String > {
20
+ tgUpdateHandlingService.handleUpdate(update);
21
+ return Mono .just(" {}" )
22
+ }
23
+
24
+ }
Original file line number Diff line number Diff line change
1
+ package by.jprof.coding.problems.bot.service
2
+
3
+ import by.jprof.coding.problems.bot.config.text
4
+ import dev.inmo.tgbotapi.bot.TelegramBot
5
+ import dev.inmo.tgbotapi.extensions.api.send.sendMessage
6
+ import dev.inmo.tgbotapi.types.update.MessageUpdate
7
+ import dev.inmo.tgbotapi.types.update.abstracts.Update
8
+ import org.springframework.stereotype.Service
9
+
10
+ @Service
11
+ class TgUpdateHandlingService (private val telegramBot : TelegramBot ) {
12
+ suspend fun handleUpdate (update : Update ) {
13
+ if (update is MessageUpdate ) {
14
+ update.data.text?.let {
15
+ telegramBot.sendMessage(update.data.chat.id, it)
16
+ }
17
+ }
18
+ }
19
+ }
20
+
21
+
Original file line number Diff line number Diff line change 1
1
spring :
2
2
application :
3
3
name : coding-problems-bot
4
-
5
- =DEBUG
6
4
logging :
7
5
level :
8
6
root : ${LOG_LEVEL:ERROR}
You canβt perform that action at this time.
0 commit comments