Image

(Last change: 04.05.2024)

To test the performance of a telegram bot translator written in Python, we used preconfigured telegram bot server. This bot can translate text from Russian into English and from other languages into Russian, while the input language in this case will be determined automatically.

Let's proceed with the installation:

source my-tel-bot/bin/activate
pip3 install googletrans==3.1.0a0 -it is important to install this particular version of the library
pip3 install pyTelegramBotAPI
pip3 install asyncio
pip3 install aiohttp

Let's replace our demo bot with the existing one:

/root/my-tel-bot/bot.py

from googletrans import Translator
from telebot.async_telebot import AsyncTeleBot
import asyncio
from telebot.types import InlineQuery, InputTextMessageContent
from telebot import types

bot = AsyncTeleBot("Key received from @BotFather", parse_mode=None)

# Handling the /start command greeting.
@bot.message_handler(commands=['start'])
async def send_welcome(message):
await bot.reply_to(message,'------\n'
+ 'Hello, '
+ message.from_user.first_name
+ ' \nI will translate from Russian into English \nAnd from other languages into Russian '
+'\n------')

# Processing the /help command.
@bot.message_handler(commands=['help'])
async def send_welcome(message):
await bot.reply_to(message,'------\n'
+ 'Just enter text and hit send\n'
+ 'I will determine what language it is\n'
+ 'If not translated, try again\n'
+ 'google translate'
+'\n------')

# Message text processing, if input is in Russian, then translation into English,
# if another language, then translation into Russian.
@bot.message_handler()
async def user_text(message):
translator = Translator()

# Determining the input language.
lang = translator.detect(message.text)
lang = lang.lang

# If the input is in Russian, then translate into English by default.
# If you need another language, change to .
if lang == 'ru':
send = translator.translate(message.text)
await bot.reply_to(message, '------\n'+ send.text +'\n------')

# Otherwise, translate another language into Russian {dest='ru'}.
else:
send = translator.translate(message.text, dest='ru')
await bot.reply_to(message, '------\n'+ send.text +'\n------')

# Handling pictures with captions
@bot.message_handler(content_types=['photo'])
async def handle_image(message):
translator = Translator()

#Image message handler
chat_id = message.chat.id
photo = message.photo[-1].file_id
caption = message.caption

# Determining the input language.
lang = translator.detect(caption)
lang = lang.lang

# If the signature is in Russian, then translate into English by default.
if lang == 'ru':
send = translator.translate(caption)

# Otherwise, translate another language into Russian {dest='ru'}.
else:
send = translator.translate(caption, dest='ru')
await bot.send_photo(chat_id, photo, caption=send.text)

# Processing online requests. Inline mode must be enabled in the bot settings @BotFather.
@bot.inline_handler(lambda query: True)
async def inline_query(query):
results = []
translator = Translator()
text = query.query.strip()

# If the request is empty, we do not transfer
if not text:
return

# Determining the input language.
lang = translator.detect(text)
lang = lang.lang

# If the input is in Russian, then translate into English by default.
if lang == 'ru':
send = translator.translate(text)
results.append(types.InlineQueryResultArticle(
id='1', title=send.text, input_message_content=types.InputTextMessageContent(
message_text=send.text)))

# Otherwise, translate another language into Russian {dest='ru'}.
else:
send = translator.translate(text, dest='ru')
results.append(types.InlineQueryResultArticle(
id='1', title=send.text, input_message_content=types.InputTextMessageContent(
message_text=send.text)))

await bot.answer_inline_query(query.id, results)

# Startup and retry on failure.
asyncio.run(bot.infinity_polling())

Done, don't forget to specify your key token in the script and restart the service:

service my-tel-bot restart



No Comments Yet