~mendelmaleh/tg_translator

ff51b823380627a75ca08a969225dcc6f98b8def — Mendel E 2 years ago
Initial commit
2 files changed, 44 insertions(+), 0 deletions(-)

A main.py
A requirements.txt
A  => main.py +40 -0
@@ 1,40 @@
import googletrans
import toml

from pyrogram import Client, filters, idle
from pyrogram.types import Message


tr = googletrans.Translator()

config = toml.load("config.toml")
channels = {int(k): config["channels"][k] for k in config["channels"]}
api_id, api_hash = config["api"]["id"], config["api"]["hash"]

user = Client("user", api_id, api_hash, phone_number=config["user"]["number"])
bot = Client("bot", api_id, api_hash, bot_token=config["bot"]["token"])


@user.on_message(filters.chat(chats=list(channels.keys())))
async def translate(cl: Client, msg: Message):
    if not (msg.text or msg.caption):
        return

    await bot.send_message(
        chat_id=channels[msg.chat.id],
        text='\n\n'.join([msg.link, tr.translate(msg.text or msg.caption).text]),
        disable_web_page_preview=bool(msg.text)
    )


bot.start()
print(f"bot '{bot.get_me().username}' started")

user.start()
print(f"user '{user.get_me().username}' started")

idle()

user.stop()
bot.stop()
print("exit")

A  => requirements.txt +4 -0
@@ 1,4 @@
googletrans==3.1.0a0
pyrogram
tgcrypto
toml