Spring Boot Webhook Bot
This example demonstrates how to integrate a Telebof bot with Spring Boot using webhooks instead of long polling. Webhooks provide a more efficient and scalable approach for production environments by having Telegram push updates to your server.
This implementation shows:
- Spring Boot integration with Telebof
- Webhook configuration instead of long polling
- REST controller for receiving updates
- Basic echo bot functionality
Project Structure and Dependencies
First, ensure you have the necessary Spring Boot dependencies in your pom.xml
:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>LATEST_VERSION</version>
</dependency>
<dependency>
<groupId>io.github.natanimn</groupId>
<artifactId>telebof</artifactId>
<version>LATEST_VERSION</version>
</dependency>
</dependencies>
Main Application Class
package io.github.natanimn;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebhookBot {
public static void main(String[] args) {
SpringApplication.run(WebhookBot.class, args);
}
}
Webhook Controller Class
package io.github.natanimn;
import io.github.natanimn.telebof.BotClient;
import io.github.natanimn.telebof.BotLog;
import io.github.natanimn.telebof.filters.Filter;
import io.github.natanimn.telebof.types.updates.Update;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.logging.Level;
@RestController
class WebhookController {
private final BotClient bot;
private final String TOKEN = "BOT_TOKEN"; // Replace with your actual token
private final String URL = "https://your-domain.com"; // Replace with your actual domain
public WebhookController() {
// Set logging level
BotLog.setLevel(Level.INFO);
// Initialize bot client
this.bot = new BotClient(TOKEN);
// Clean up any existing webhook
bot.context.deleteWebhook();
// Set new webhook URL
bot.context.setWebhook(String.format("%s/webhook", URL)).exec();
// Add message handlers
bot.onMessage(filter -> filter.commands("start"), (ctx, message) -> {
ctx.sendMessage(message.chat.id, "Hello, I am Spring Boot echo bot").exec();
});
bot.onMessage(Filter::text, (ctx, message) -> {
ctx.sendMessage(message.chat.id, message.text).exec();
});
}
/**
* Webhook endpoint that receives updates from Telegram
* @param updates List of updates sent by Telegram
* @return "OK" response to acknowledge receipt
*/
@PostMapping("/webhook")
public String getUpdates(@RequestBody List<Update> updates) {
// Process incoming updates
bot.processUpdates(updates);
return "OK";
}
}
Key Components Explained
1. Spring Boot Configuration
@SpringBootApplication
public class WebhookBot {
public static void main(String[] args) {
SpringApplication.run(WebhookBot.class, args);
}
}
@SpringBootApplication
enables auto-configuration and component scanning- Main method starts the embedded Tomcat server
2. Webhook Setup
- deleteWebhook(): Removes any existing webhook configuration
- setWebhook(): Configures Telegram to send updates to your server URL
3. REST Controller
@RestController
class WebhookController {
@PostMapping("/webhook")
public String getUpdates(@RequestBody List<Update> updates) {
bot.processUpdates(updates);
return "OK";
}
}
@RestController
marks the class as a web request handler@PostMapping("/webhook")
maps POST requests to the /webhook endpoint@RequestBody
captures the JSON payload from Telegram- Returns "OK" to acknowledge successful receipt of updates
4. Bot Handlers
bot.onMessage(filter -> filter.commands("start"), (ctx, message) -> {
ctx.sendMessage(message.chat.id, "Hello, I am Spring Boot echo bot").exec();
});
bot.onMessage(Filter::text, (ctx, message) -> {
ctx.sendMessage(message.chat.id, message.text).exec();
});
- Start command handler: Responds to /start command
- Text message handler: Echoes any text message back to the user
Configuration Requirements
Webhooks require HTTPS in production:
- Obtain SSL certificate for your domain
- Configure Spring Boot for HTTPS
- Ensure your domain is accessible publicly
The full source code can be found on examples/webhook_bot