From 0e94b860e41ddf50f236dbccb392829ce8d87b58 Mon Sep 17 00:00:00 2001 From: Josh Fraser Date: Mon, 27 Jan 2020 18:24:25 -0800 Subject: [PATCH] translate telegram messages --- bot.py | 12 +++++++++++- model.py | 2 ++ requirements.txt | 1 + 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/bot.py b/bot.py index 5980f4b..6303b98 100644 --- a/bot.py +++ b/bot.py @@ -18,6 +18,7 @@ from time import strftime import re import unidecode from mwt import MWT +from googletrans import Translator class TelegramMonitorBot: @@ -306,7 +307,16 @@ class TelegramMonitorBot: def log_message(self, user_id, user_message, chat_id): try: s = session() - msg1 = Message(user_id=user_id, message=user_message, chat_id=chat_id) + language_code = english_message = "" + try: + translator = Translator() + translated = translator.translate(user_message) + language_code = translated.src + english_message = translated.text + except Exception as e: + print(e.message) + msg1 = Message(user_id=user_id, message=user_message, + chat_id=chat_id, language_code=language_code, english_message=english_message) s.add(msg1) s.commit() s.close() diff --git a/model.py b/model.py index d49c715..98a96d5 100644 --- a/model.py +++ b/model.py @@ -27,6 +27,8 @@ class Message(Base): id = Column(Integer, primary_key=True) user_id = Column(Integer, ForeignKey('telegram_users.id'), nullable=False) message = Column(String) + language_code = Column(String) + english_message = Column(String) chat_id = Column(BigInteger) time = Column(DateTime, default=func.now()) diff --git a/requirements.txt b/requirements.txt index 6c0c630..f5a5e0e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ python-telegram-bot==9.0.0 SQLAlchemy==1.2.2 configparser==3.5.0 Unidecode==1.0.22 +googletrans==2.4.0