Skip to content

Commit 6406d64

Browse files
committed
echo bot code
1 parent 0345584 commit 6406d64

File tree

4 files changed

+62
-3
lines changed

4 files changed

+62
-3
lines changed

β€Žtelegram-bot/src/main/kotlin/by/jprof/coding/problems/bot/config/ApplicationConfiguration.kt

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package by.jprof.coding.problems.bot.config
22

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
39
import io.r2dbc.spi.ConnectionFactories
410
import io.r2dbc.spi.ConnectionFactory
511
import org.springframework.context.annotation.Bean
@@ -10,8 +16,18 @@ import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories
1016
@Configuration
1117
@EnableR2dbcRepositories
1218
class ApplicationConfiguration : AbstractR2dbcConfiguration() {
19+
companion object {
20+
val BOT_TOKEN = System.getenv("BOT_TOKEN")!!
21+
}
1322
@Bean
1423
override fun connectionFactory() : ConnectionFactory {
1524
return ConnectionFactories.get(System.getenv("DB_URL"))
1625
}
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
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+

β€Žtelegram-bot/src/main/resources/application.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
spring:
22
application:
33
name: coding-problems-bot
4-
5-
=DEBUG
64
logging:
75
level:
86
root: ${LOG_LEVEL:ERROR}

0 commit comments

Comments
Β (0)