Answer the question
In order to leave comments, you need to log in
Telegram bot on Kotlin + Spring via webhook, is there a fresh manual or an example of a mirror bot?
Now I am trying to rewrite my old pet-bot from java to Kotlin using webhook and Spring Boot (mainly for self-development) I
encountered the following:
1. In the internet, there are only TelegramLongPollingBot examples
2. Everywhere in the examples from spring, ApiContextInitializer.init() is used for initialization bot.
However, in the library, starting from version 5.0 , this class was cut out
and I don’t understand how to run it through Spring now ...
ApiContextInitializer.init(); has been removed and is not required anymore, instead:
TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class);
// When using webhook, create your own version of DefaultWebhook with all your parameters set.
TelegramBotsApi telegramBotsApi = new TelegramBotsApi(DefaultBotSession.class, defaultWebhookInstance);
b When using Spring with a webhook bot, make your bot inherit form SpringWebhookBot instead of WebhookBot and provide your SetWebhook method in the constructor:
// Extend correct class
public class TestSpringWebhookBot extends SpringWebhookBot {
public TestSpringWebhookBot(SetWebhook setWebhook) {
super(setWebhook);
}
public TestSpringWebhookBot(DefaultBotOptions options, SetWebhook setWebhook) {
super(options, setWebhook);
}
@Override
public String getBotUsername() {
return null;
}
@Override
public String getBotToken() {
return null;
}
@Override
public BotApiMethod onWebhookUpdateReceived(Update update) {
return null;
}
@Override
public String getBotPath() {
return null;
}
}
// Create your SetWebhook method
@Bean
public SetWebhook setWebhookInstance() {
return SetWebhook.builder()....build();
}
// Create it as
@Bean
public TestSpringWebhookBot testSpringWebhookBot(SetWebhook setWebhookInstance) {
return new TestSpringWebhookBot(setWebhookInstance);
}
Answer the question
In order to leave comments, you need to log in
OtBotSpringApplication.kt
package ru.lex.OtBotSpring
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
class OtBotSpringApplication
fun main(args: Array<String>) {
runApplication<OtBotSpringApplication>(*args)
}
package ru.lex.OtBotSpring.service
import org.springframework.beans.factory.annotation.Value
import org.springframework.stereotype.Component
import org.telegram.telegrambots.bots.TelegramWebhookBot
import org.telegram.telegrambots.meta.api.methods.BotApiMethod
import org.telegram.telegrambots.meta.api.methods.send.SendMessage
import org.telegram.telegrambots.meta.api.objects.Update
@Component
class OtBot : TelegramWebhookBot() {
@Value("\${telegrambot.botname}")
private val botName: String= ""
@Value("\${telegrambot.bottoken}")
private val botToken : String =""
@Value("\${telegrambot.webhookpath}")
private val botWebHook : String=""
override fun getBotToken(): String = botToken
override fun getBotUsername(): String = botName
override fun onWebhookUpdateReceived(update: Update?): BotApiMethod<*>? {
if (update != null) {
if (update.hasMessage())
{
val sendMsg = SendMessage()
val message = update.message
val chatId = message.chatId
val responseText = if (message.hasText()) {
val messageText = message.text
when {
messageText == "/start" -> "Добро пожаловать!"
else -> "Вы написали: *$messageText*"
}
} else {
"Я понимаю только текст"
}
sendMsg.text = responseText
sendMsg.chatId = chatId.toString()
return sendMsg
}
}
return null;
}
override fun getBotPath(): String = botWebHook
}
server.port=8443
telegrambot.bottoken=
telegrambot.botname=test_my_super_puper_bot
telegrambot.webhookpath=https://26e9b1e5ff85.ngrok.io
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question