Skip to content

Commit

Permalink
Changed i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielRF committed Nov 5, 2020
1 parent 82c1bcd commit e08c68d
Showing 1 changed file with 65 additions and 58 deletions.
123 changes: 65 additions & 58 deletions pdftokindlebot.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ def open_file(file_url, chatid):


# Send e-mail function
def send_mail(chatid, send_from, send_to, subject, text, file_url, last_usage):
def send_mail(chatid, send_from, send_to, subject, text, file_url, last_usage, user_lang):
set_buttons(user_lang)
try:
interval = (datetime.datetime.now() - datetime.datetime.strptime(last_usage, "%Y-%m-%d %H:%M:%S.%f")).total_seconds()
except ValueError:
interval = 901
if len(send_from) < 5 or len(send_to) < 5:
bot.send_message(chatid, i18n.t('bot.error'), parse_mode='HTML')
bot.send_message(chatid, i18n.t('bot.error', locale=user_lang), parse_mode='HTML')
return 0
msg = MIMEMultipart()
msg['From'] = send_from
Expand All @@ -87,27 +88,27 @@ def send_mail(chatid, send_from, send_to, subject, text, file_url, last_usage):
if '.epub' in files or '.cbr' in files or '.cbz' in files or '.azw3' in files:
if interval < 900 and '9083329' not in chatid:
try:
bot.send_message(chatid, i18n.t('bot.slowmode'))
bot.send_message(chatid, i18n.t('bot.slowmode', locale=user_lang))
except:
bot.send_message(chatid, 'Wait 15 minutes')
os.remove(files)
return 0
else:
files = epub2mobi(files, chatid)
except:
bot.send_message(chatid, i18n.t('bot.filenotfound'))
bot.send_message(chatid, i18n.t('bot.filenotfound', locale=user_lang))
return 0

bot.send_chat_action(chatid, 'upload_document')
bot.send_message(chatid, str(u'\U0001F5DE')
+ i18n.t('bot.sendingfile'), parse_mode='HTML')
+ i18n.t('bot.sendingfile', locale=user_lang), parse_mode='HTML')
bot.send_chat_action(chatid, 'upload_document')
try:
part = MIMEBase('application', 'octet-stream')
part.set_payload(open(files, 'rb').read())
encoders.encode_base64(part)
except FileNotFoundError:
bot.send_message(chatid, i18n.t('bot.filenotfound'))
bot.send_message(chatid, i18n.t('bot.filenotfound', locale=user_lang))
part.add_header('Content-Disposition',
'attachment; filename="{0}"'.format(os.path.basename(files)))
msg.attach(part)
Expand All @@ -119,17 +120,25 @@ def send_mail(chatid, send_from, send_to, subject, text, file_url, last_usage):
except smtplib.SMTPSenderRefused:
print('Erro')
msg = bot.send_message(chatid,
str(u'\U000026A0') + i18n.t('bot.fsize'), parse_mode='HTML')
str(u'\U000026A0') + i18n.t('bot.fsize', locale=user_lang), parse_mode='HTML')
smtp.close()
logger_info.info(str(datetime.datetime.now()) + '\tError:\t'
+ str(chatid) + '\t' + send_from + '\t' + send_to)
try:
os.remove(files)
except FileNotFoundError:
pass
return 0
except smtplib.SMTPRecipientsRefused:
msg = bot.send_message(chatid,
str(u'\U000026A0') + i18n.t('bot.checkemail'), parse_mode='HTML')
str(u'\U000026A0') + i18n.t('bot.checkemail', locale=user_lang), parse_mode='HTML')
smtp.close()
logger_info.info(str(datetime.datetime.now()) + '\tError:\t'
+ str(chatid) + '\t' + send_from + '\t' + send_to)
try:
os.remove(files)
except FileNotFoundError:
pass
return 0

smtp.close()
Expand All @@ -150,9 +159,9 @@ def send_mail(chatid, send_from, send_to, subject, text, file_url, last_usage):
icon_x=u'\U0001F4EE',
icon_y=u'\U00002B50',
icon_z=u'\U0001F4B5',
msg_a=i18n.t('bot.filesent'),
msg_a=i18n.t('bot.filesent', locale=user_lang),
#msg_b=i18n.t('bot.rate'),
msg_c=i18n.t('bot.donate'),
msg_c=i18n.t('bot.donate', locale=user_lang),
)
bot.send_message(chatid, msg, parse_mode='HTML',
reply_markup=button, disable_web_page_preview=True)
Expand Down Expand Up @@ -227,26 +236,17 @@ def select_user(db, table, chatid, field):
conn.close()
return data

def user_lang(message):
try:
user_lang = message.from_user.language_code.lower()
except:
user_lang = 'en-us'
i18n.set('locale', user_lang)
i18n.set('fallback', 'en-us')
set_buttons()


def set_buttons():
def set_buttons(lang = 'en-us'):
global button
global button2
button = types.InlineKeyboardMarkup()
btn1 = types.InlineKeyboardButton(i18n.t('bot.btn1'), callback_data='/send')
btn2 = types.InlineKeyboardButton(i18n.t('bot.btn2'), callback_data='/email')
btn1 = types.InlineKeyboardButton(i18n.t('bot.btn1', locale=lang), callback_data='/send')
btn2 = types.InlineKeyboardButton(i18n.t('bot.btn2', locale=lang), callback_data='/email')
button.row(btn1, btn2)
button2 = types.InlineKeyboardMarkup()
btn3 = types.InlineKeyboardButton(i18n.t('bot.btn3'), callback_data='/as_is')
btn4 = types.InlineKeyboardButton(i18n.t('bot.btn4'), callback_data='/converted')
btn3 = types.InlineKeyboardButton(i18n.t('bot.btn3', locale=lang), callback_data='/as_is')
btn4 = types.InlineKeyboardButton(i18n.t('bot.btn4', locale=lang), callback_data='/converted')
button2.row(btn3, btn4)


Expand All @@ -273,27 +273,27 @@ def set_buttons():

@bot.message_handler(commands=['help'])
def help(message):
user_lang(message)
bot.send_message(message.from_user.id, i18n.t('bot.help'), parse_mode='HTML',
user_lang = (message.from_user.language_code or 'en-us').lower()
bot.send_message(message.from_user.id, i18n.t('bot.help', locale=user_lang), parse_mode='HTML',
disable_web_page_preview=True)

@bot.message_handler(commands=['tos'])
def tos(message):
user_lang(message)
bot.send_message(message.from_user.id, i18n.t('bot.tos'), parse_mode='HTML',
user_lang = (message.from_user.language_code or 'en-us').lower()
bot.send_message(message.from_user.id, i18n.t('bot.tos', locale=user_lang), parse_mode='HTML',
disable_web_page_preview=True)

@bot.message_handler(commands=['info'])
def help(message):
user_lang(message)
bot.send_message(message.from_user.id, i18n.t('bot.info'), parse_mode='HTML',
user_lang = (message.from_user.language_code or 'en-us').lower()
bot.send_message(message.from_user.id, i18n.t('bot.info', locale=user_lang), parse_mode='HTML',
disable_web_page_preview=True)


# select_user(db, table, sys.argv[1])
@bot.message_handler(commands=['start'])
def start(message):
user_lang(message)
user_lang = (message.from_user.language_code or 'en-us').lower()
data = select_user(db, table, message.from_user.id, '*')
try:
aux1 = data[2]
Expand All @@ -303,33 +303,33 @@ def start(message):
aux2 = ' '
if len(aux1) < 3 or len(aux2) < 3:
msg = bot.send_message(message.from_user.id,
i18n.t('bot.startnewuser'), parse_mode='HTML')
i18n.t('bot.startnewuser', locale=user_lang), parse_mode='HTML')
bot.register_next_step_handler(msg, add_email)
else:
bot.send_message(message.from_user.id,
i18n.t('bot.startolduser').format(
i18n.t('bot.startolduser', locale=user_lang).format(
str(u'\U0001F4E4'), data[2], str(u'\U0001F4E5'), data[3]
), parse_mode='HTML', reply_markup=button)

@bot.message_handler(commands=['email'])
def ask_email(message):
user_lang(message)
msg = bot.send_message(message.from_user.id, i18n.t('bot.askemail3'))
user_lang = (message.from_user.language_code or 'en-us').lower()
msg = bot.send_message(message.from_user.id, i18n.t('bot.askemail3', locale=user_lang))
bot.register_next_step_handler(msg, add_email)

def add_email(message):
user_lang = (message.from_user.language_code or 'en-us').lower()
if message.content_type != 'text':
msg = bot.send_message(message.from_user.id,
i18n.t('bot.askemail'), parse_mode='HTML')
i18n.t('bot.askemail', locale=user_lang), parse_mode='HTML')
bot.register_next_step_handler(msg, add_email)
return 0
user_lang(message)
try:
text = message.text.lower()
except AttributeError:
text = None
if text in cmds:
msg = bot.send_message(message.from_user.id, i18n.t('bot.askemail'))
msg = bot.send_message(message.from_user.id, i18n.t('bot.askemail', locale=user_lang))
bot.register_next_step_handler(msg, add_email)
return 0
elif '/' not in message.text:
Expand All @@ -338,34 +338,34 @@ def add_email(message):
str(message.text) + '"')
data = select_user(db, table, message.from_user.id, '*')
if '@' not in str(data[3]):
msg = bot.reply_to(message, i18n.t('bot.askemail'))
msg = bot.reply_to(message, i18n.t('bot.askemail', locale=user_lang))
bot.register_next_step_handler(msg, add_email)
return 0
if '@' not in str(data[2]):
msg = bot.reply_to(message, i18n.t('bot.askemail2'))
msg = bot.reply_to(message, i18n.t('bot.askemail2', locale=user_lang))
bot.register_next_step_handler(msg, add_email)
return 0
msg = bot.reply_to(message,
str(u'\U00002705') + i18n.t('bot.success'),
str(u'\U00002705') + i18n.t('bot.success', locale=user_lang),
parse_mode='HTML', reply_markup=button)
else:
msg = bot.send_message(message.from_user.id, str(u'\U000026A0')
+ i18n.t('bot.askemail'), parse_mode='HTML')
+ i18n.t('bot.askemail', locale=user_lang), parse_mode='HTML')
bot.register_next_step_handler(msg, add_email)
else:
msg = bot.send_message(message.from_user.id,
i18n.t('bot.askemail'), parse_mode='HTML')
i18n.t('bot.askemail', locale=user_lang), parse_mode='HTML')
bot.register_next_step_handler(msg, add_email)

@bot.message_handler(commands=['send'])
def ask_file(message):
user_lang(message)
user_lang = (message.from_user.language_code or 'en-us').lower()
msg = bot.send_message(message.from_user.id,
i18n.t('bot.askfile'))
i18n.t('bot.askfile', locale=user_lang))
# bot.register_next_step_handler(msg, get_file)

def get_file(message):
user_lang(message)
user_lang = (message.from_user.language_code or 'en-us').lower()
if str(message.from_user.id) in BLOCKED:
print(message)
try:
Expand All @@ -385,15 +385,15 @@ def get_file(message):
+ str(file_size) + ' bytes.')
bot.send_chat_action(message.from_user.id, 'upload_document')
if file_size > 20000000:
bot.send_message(message.from_user.id, i18n.t('bot.fsize'),
bot.send_message(message.from_user.id, i18n.t('bot.fsize', locale=user_lang),
parse_mode='HTML', disable_web_page_preview=True)
return 0
file_info = bot.get_file(message.document.file_id)
file_url = ('https://api.telegram.org/file/bot' + TOKEN + '/'
+ file_info.file_path)
elif message.content_type == 'text':
if message.text.lower() in cmds:
bot.send_message(message.from_user.id, i18n.t('bot.askfile'))
bot.send_message(message.from_user.id, i18n.t('bot.askfile', locale=user_lang))
return 0
file_url = message.text
try:
Expand All @@ -407,7 +407,7 @@ def get_file(message):
open(file_name, 'wb').write(pdf)
file_url = file_name
else:
msg = bot.send_message(message.from_user.id, i18n.t('bot.askfile'))
msg = bot.send_message(message.from_user.id, i18n.t('bot.askfile', locale=user_lang))
bot.register_next_step_handler(msg, get_file)
return 0

Expand All @@ -419,76 +419,83 @@ def get_file(message):
#print(f)
upd_user_file(db, table, message.from_user.id, file_url)
if '.pdf' in file_url.lower():
msg = bot.send_message(message.from_user.id, i18n.t('bot.askconvert'),
msg = bot.send_message(message.from_user.id, i18n.t('bot.askconvert', locale=user_lang),
parse_mode='HTML', reply_markup=button2)
else:
data = select_user(db, table, message.from_user.id, '*')
lang = (message.from_user.language_code or 'en-us').lower()
send_mail(str(message.from_user.id), data[2],
data[3], ' ', str(message.from_user.id), data[7], data[5])
data[3], ' ', str(message.from_user.id), data[7], data[5], lang)

# bot.register_next_step_handler(msg, ask_conv)

@bot.callback_query_handler(lambda q: q.data == '/converted')
def ask_conv(call):
user_lang = (call.from_user.language_code or 'en-us').lower()
try:
bot.answer_callback_query(call.id)
except:
pass
data = select_user(db, table, call.from_user.id, '*')
try:
send_mail(str(call.from_user.id), data[2],
data[3], 'Convert', str(call.from_user.id), data[7], data[5])
data[3], 'Convert', str(call.from_user.id), data[7], data[5], user_lang)
except IndexError:
bot.send_message(call.from_user.id, i18n.t('bot.error', locale=user_lang), parse_mode='HTML')
bot.register_next_step_handler(msg, add_email)
return 0

@bot.callback_query_handler(lambda q: q.data == '/as_is')
def ask_conv(call):
user_lang = (call.from_user.language_code or 'en-us').lower()
try:
bot.answer_callback_query(call.id)
except:
pass
data = select_user(db, table, call.from_user.id, '*')
send_mail(str(call.from_user.id), data[2],
data[3], ' ', str(call.from_user.id), data[7], data[5])
data[3], ' ', str(call.from_user.id), data[7], data[5], user_lang)

@bot.callback_query_handler(lambda q: q.data == '/email')
def email(call):
user_lang = (call.from_user.language_code or 'en-us').lower()
try:
bot.answer_callback_query(call.id)
except:
pass
msg = bot.send_message(call.from_user.id, i18n.t('bot.askemail3'))
msg = bot.send_message(call.from_user.id, i18n.t('bot.askemail3', locale=user_lang))
bot.register_next_step_handler(msg, add_email)

@bot.callback_query_handler(lambda q: q.data == '/send')
def ask_file(call):
user_lang = (call.from_user.language_code or 'en-us').lower()
try:
bot.answer_callback_query(call.id)
except:
pass
msg = bot.send_message(call.from_user.id,
i18n.t('bot.askfile'))
i18n.t('bot.askfile', locale=user_lang))
# bot.register_next_step_handler(msg, get_file)

@bot.message_handler(func=lambda m: True)
def generic_msg(message):
user_lang = (message.from_user.language_code or 'en-us').lower()
user_lang(message)
if ('@' not in message.text or '/' in message.text) and message.text not in cmds:
bot.send_chat_action(message.chat.id, 'typing')
try:
get_file(message)
except:
bot.send_message(message.chat.id, i18n.t('bot.filenotfound'))
bot.send_message(message.chat.id, i18n.t('bot.filenotfound', locale=user_lang))

@bot.message_handler(content_types=['document'])
def generic_file(message):
user_lang(message)
user_lang = (message.from_user.language_code or 'en-us').lower()
bot.send_chat_action(message.chat.id, 'typing')
try:
get_file(message)
except:
bot.send_message(message.chat.id, i18n.t('bot.filenotfound'))
bot.send_message(message.chat.id, i18n.t('bot.filenotfound', locale=user_lang))

sentry_url = config['SENTRY']['url']
if sentry_url:
Expand Down

0 comments on commit e08c68d

Please sign in to comment.