translate telegram messages

This commit is contained in:
Josh Fraser 2020-01-27 18:24:25 -08:00
parent b1a57bb917
commit 0e94b860e4
3 changed files with 14 additions and 1 deletions

12
bot.py
View File

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

View File

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

View File

@ -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