Skip to main content

Translategram is a Python library for translating messages are sent by your Bot in Telegram

Project description

Translategram

Translategram is a Python package that provides translation capabilities for Telegram bots. It uses and supports multiple translation services.

Installation

You can install Translategram using pip:

pip install translategram

Usage

First you need to add a parameter to your handler called whatever you want (in this example we called it message) and its type should be the string.

@translator.handler_translator(message="Welcome to our community!")
async def login(update: Update, context: ContextTypes.DEFAULT_TYPE, message: str) -> None:
    await context.bot.send_message(
            chat_id=update.effective_chat.id if update.effective_chat else 0,
            text=message
            )

And then just register your handler in handler class.

login_handler = CommandHandler('login', login)

As well as you should create translator instance based on the framework you are using (in this case python-telegram-bot).

from translategram import PythonTelegramBotTranslator, MtranslateTranslatorService

translator = PythonTelegramBotTranslator(MtranslateTranslatorService)

And finally your file should looks like this:

import logging
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler
from telegram import Update
from translategram import PythonTelegramBotTranslator, MtranslateTranslatorService

translator = PythonTelegramBotTranslator(MtranslateTranslatorService)
TOKEN = 'YOUR_TOKEN'
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
    level=logging.INFO
)

logger = logging.getLogger(__name__)


@translator.handler_translator(message="Welcome to our community!")
async def login(update: Update, context: ContextTypes.DEFAULT_TYPE, message: str) -> None:
    await context.bot.send_message(
            chat_id=update.effective_chat.id if update.effective_chat else 0,
            text=message
            )


@translator.handler_translator(message="This bot is very simple. You can just login with the /login command and that is it!")
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE, message: str) -> None:
    await context.bot.send_message(
            chat_id=update.effective_chat.id if update.effective_chat else 0,
            text=message
            )

if __name__ == '__main__':
    application = ApplicationBuilder().token(TOKEN).build()

    login_handler = CommandHandler('login', login)
    start_handler = CommandHandler('start', start)
    application.add_handler(login_handler)
    application.add_handler(start_handler)

    application.run_polling()

TODO

  • Implement cache system
    • Cache System with Memcache.
    • Cache System with Redis.
  • Add aiogram framework adapter.
  • Add pyTelegramBotApi framework adapter.
  • Add support for more translation services.

License

This project is licensed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

translategram-0.4.0.tar.gz (7.7 kB view details)

Uploaded Source

Built Distribution

translategram-0.4.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file translategram-0.4.0.tar.gz.

File metadata

  • Download URL: translategram-0.4.0.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.11

File hashes

Hashes for translategram-0.4.0.tar.gz
Algorithm Hash digest
SHA256 9ee9c9aaac62940976de9600cf8b1fa24a5daecc83222a395a8e2ebafa7c95b1
MD5 f439f64296c373f01680a460d9cf4dcf
BLAKE2b-256 ac4103494f67bbaaddbb4bfcaf1b96318de3db6c1578a9b67115f87c1fd00544

See more details on using hashes here.

File details

Details for the file translategram-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for translategram-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3f60b02e875dd62d3ec49a67137168f2222c6020aac68a062ed6ce8860b562aa
MD5 6aab84d91a4a1391ca2745b57fc613df
BLAKE2b-256 7a63663e7c4dc40a237abe3c1c46e1f661afc786773273366741bfd4a5309252

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page