L
L
Lexvalyaev2021-07-21 13:30:57
Spring
Lexvalyaev, 2021-07-21 13:30:57

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

And even a separate class appeared:

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);
   }


I can roughly imagine how this can be used in Java, but I’m just learning Spring and Kotlin for now ...

Maybe someone has a simple echo bot that uses all of the above?
And even the echo bot doesn’t want to work for me. I’m even

ready to pay for mentoring to clarify this issue ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lexvalyaev, 2021-07-21
@Lexvalyaev

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)
}

OtBot.kt
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
}

well, application.properties
server.port=8443

telegrambot.bottoken=
telegrambot.botname=test_my_super_puper_bot
telegrambot.webhookpath=https://26e9b1e5ff85.ngrok.io

accordingly, removed the bottoken and changed the botname.
60f8415ea193d540583355.jpeg
the bot does not work.. where am I stupid?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question