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 out 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 out 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.3.0.tar.gz
(7.0 kB
view details)
Built Distribution
File details
Details for the file translategram-0.3.0.tar.gz
.
File metadata
- Download URL: translategram-0.3.0.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9de1df943e8ad8b1e7cbda5974f678b00fb77cbd0648d2b01838a1f54c517fc9 |
|
MD5 | 60bef47c79b9eddc2fbcea319a1beb95 |
|
BLAKE2b-256 | d48a6b9bd2db31d14b94ecdc28dd3ddf28076db865ce8d8494a77edbdcc4b2fa |
File details
Details for the file translategram-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: translategram-0.3.0-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.17
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7c13478f8e6fb9dc7b4c015a23e7f97bb7ba34e537087283a46fa6ccd36af56a |
|
MD5 | 89ae330684a61bc6f56e8fb8db3e4b43 |
|
BLAKE2b-256 | cad8f890358fddb0a8fda2056fd89824ad54d17fa75a9160c789da1e5a9d748f |