A Telegram bot built using Spring Boot that translates text between English, Russian, and Uzbek using the Google Translate API.
- Translate text between the supported languages: English, Russian, and Uzbek.
- Seamless integration with the Google Translate API for accurate translations.
- User-friendly Telegram commands and keyboard options.
- Customizable webhook and bot configurations via
application.yml
.
This bot was developed using the tools and configurations described below. However, technologies and APIs may evolve, and some information may become outdated. It’s highly recommended to refer to the official documentation for the most up-to-date details.
-
Google Cloud Platform Setup
- Register on Google Cloud Console: Visit Google Cloud Console. Enable a billing account and create a new project.
- Enable the Translate API: Navigate to the API Library. Enable the Google Translate API for your project.
- Set Up OAuth Consent Screen: Go to the OAuth consent screen section in the Google Cloud Console and configure it for your project.
- Install Google Cloud CLI (gcloud): Follow the instructions to install the CLI: Install gcloud CLI.
- Generate and Inject Access Token: Use the following commands to generate and inject the access token into the
local environment:
This ensures the access token isn’t hardcoded in the source code.
gcloud init gcloud auth application-default print-access-token
-
Telegram Bot Setup
- Open the BotFather bot on Telegram.
- Create a new bot using
/newbot
and follow the instructions. - Save the bot token provided by BotFather for later use.
- Java: The bot is developed in Java for robustness and scalability.
- Spring Boot: A framework used to simplify development with dependency injection, RESTful APIs, and more.
- Google Translate API: Integrated for reliable and accurate translations.
- Telegram Bot API: Used to connect and manage the bot’s interactions on Telegram.
Update the following configuration file with your bot's details:
spring:
application:
name: app-translator
profiles:
active: dev
server:
port: 80
telegram:
webhook-path: "YOUR_BOT_WEBHOOK"
bot-name: "YOUR_BOT_USERNAME"
bot-token: "YOUR_BOT_TOKEN"
chat-id:
log: YOUR_LOGGING_CHAT_ID
logging:
level:
root: warn
file:
name: app-translator.log
logback:
rollingpolicy:
max-file-size: 10MB
max-history: 10
pattern:
console: "%d{yyyy-MM-dd HH:mm:ss} - %logger{36} - %msg%n"
Replace the placeholders (YOUR_BOT_WEBHOOK
, YOUR_BOT_USERNAME
, and YOUR_BOT_TOKEN
) with your bot's information.
- Create a new Telegram group for logging. This group will be used to capture bot logs.
- Find the group ID for this group:
- Open the group in Telegram and use a bot like
@userinfobot
to get the group ID. - Add this group ID to the
application.yml
undertelegram.chat-id.log
.
- Open the group in Telegram and use a bot like
- You can find your project ID in the Google Cloud Console:
- Go to Google Cloud Console.
- Click on the project drop-down menu at the top and select the appropriate project.
- The project ID is listed in the project details section.
Clone the project repository to your local machine:
git clone https://github.com/dilshodlatipov/translator-bot.git
cd translator-bot
Ensure you have Java 17+ and Maven installed. Then build and run the project:
mvn clean install
java -jar target/translator-bot.jar
- /start: Check if the bot is active and get a welcome message.
- /help: Get instructions on how to use the bot.
- /english: Set the source language to English.
- /russian: Set the source language to Russian.
- /uzbek: Set the source language to Uzbek.
- /language: Change the bot's interface language.
- Use a command to set the source language (/english, /russian, or /uzbek).
- Send the text to be translated.
- Select the target language using the keyboard provided.
- Receive the translated text.
For the latest updates, refer to these official resources:
- Spring Boot Documentation: https://spring.io/projects/spring-boot
- **Google Translate API Documentation **: https://cloud.google.com/translate/docs
- Google Cloud CLI Documentation: https://cloud.google.com/sdk/docs
- Telegram Bot API Documentation: https://core.telegram.org/bots/api
Logs are saved in app-translator.log
. Customize logging settings in application.yml
under the logging
section.
Ensure you set up the logging group as described above.
Contributions are welcome! Feel free to fork the repository and submit pull requests.
This project is licensed under the MIT License.