Skip to content

miladrostami-devjava/Java-Telegram-Bot-

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Milad Java Telegram Bot 🤖

Java Spring Boot Telegram Bots API

🔹 Project Overview

This project is a Telegram Bot built using Java 21 and Spring Boot 3.5.7.
The bot uses Long Polling to receive messages from Telegram servers and respond to them automatically.
It also supports Proxy (SOCKS/HTTP) connections for restricted or filtered network environments.

Key Features:

  • Receive and reply to user text messages
  • Proxy support for internet restrictions
  • Full logging with SLF4J
  • Spring Boot structure with Dependency Injection
  • Easy to extend with new commands and logic

🔹 Prerequisites

  1. Java 21 or higher
  2. Maven or Gradle for building the project
  3. Spring Boot
  4. Telegram Bot Token (from BotFather)
  5. (Optional) Proxy for filtered network environments

src/main/java ├── com.bot.bot │ └── TelegramBot.java # Main bot class ├── com.bot.config │ └── TelegramConfig.java # Bot and Proxy configuration └── resources └── application.properties # Bot and Proxy settings

bot.name=Milad_Java_Assistant_bot bot.token=YOUR_BOT_TOKEN_HERE

Proxy configuration (optional)

proxy.host=127.0.0.1 proxy.port=1080 proxy.type=SOCKS

2. Bot Workflow

Spring Boot starts the application.

TelegramConfig creates and registers the TelegramBot bean.

If a Proxy is configured, the bot connects via Proxy.

The bot receives messages from Telegram using Long Polling.

Messages are processed in onUpdateReceived.

Responses are sent with execute(new SendMessage(...)).

All messages and errors are logged for debugging.

🔹 Key Concepts & Learning Points

Long Polling: Fetch messages continuously without using a webhook

Dependency Injection: Bot name, token, and options are injected via Spring

Proxy Handling: Supports SOCKS or HTTP for filtered internet environments

Logging: SLF4J logs all incoming messages and errors

Error Handling: Handles message sending errors gracefully

🔹 Bot Commands

Command Function /start Sends a welcome message Any other text Echoes back the text with "You said: " prefix

🔹 Extending the Bot

Add new commands using switch-case in onUpdateReceived

Support images, files, stickers

Connect to external APIs for smart responses

Handle network and Telegram API errors using retries and timeouts

🔹 Troubleshooting

Port 8080 already in use?

Change the server port in application.properties:

server.port=8081

Unable to connect to Telegram in filtered networks?

Enable Proxy:

proxy.host=127.0.0.1 proxy.port=1080 proxy.type=SOCKS

Telegram API Timeout errors?

Ensure internet connection is stable and Proxy works properly.

Consider using Psiphon or another VPN/proxy tool.

🔹 Installation & Running

  1. Clone the project:
git clone https://github.com/miladrostami-devjava/Java-Telegram-Bot-.git
cd MiladJavaTelegramBot


🔹 Contact

Developer: Milad Rostami

Email: miladrostami24@gmail.com

About

This project is a **Telegram Bot** built using **Java 21** and **Spring Boot 3.5.7**.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages