Image

(마지막 변경: 06.11.2024)

Python으로 작성된 텔레그램 봇 번역기의 성능을 테스트하기 위해 사전 구성된 텔레그램 봇 서버를 사용했습니다. . 이 봇은 러시아어 텍스트를 영어로, 다른 언어의 텍스트를 러시아어로 번역할 수 있으며, 이 경우 입력 언어는 자동으로 결정됩니다.

설치를 진행해 보겠습니다

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

데모 봇을 기존 로봇으로 교체해 보겠습니다:

/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())

완료되었습니다. 스크립트에 키 토큰을 지정하고 서비스를 다시 시작하는 것을 잊지 마세요:

service my-tel-bot restart



No Comments Yet