-
Notifications
You must be signed in to change notification settings - Fork 0
/
rica_translatebot.py
140 lines (87 loc) · 3.5 KB
/
rica_translatebot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# -*- coding: utf-8 -*-
import logging
from random import randrange
from telegram.ext import (Updater, CommandHandler, MessageHandler, Filters, ConversationHandler)
# from googletrans import Translator
from google_trans_new import google_translator
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
logger = logging.getLogger(__name__)
translator = google_translator()
def start(update, context):
context.bot.send_message(chat_id=update.effective_chat.id,
text='''translator bot started. type /help for help.''')
return
def help(update, context):
context.bot.send_message(chat_id=update.effective_chat.id,
text='''
/t [SOURCE] [TARGET] [MESSAGE]
translates a message from source language to target language.
[SOURCE]: source language coded in 2 characters: EN - english, ES - spanish, TR - turkish, FR - french...
[TARGET]: target language coded in 2 characters: EN - english, ES - spanish, TR - turkish, FR - french...
[MESSAGE]: message in SOURCE language to be translated to TARGET language.
\n/tr MESSAGE
translates a message from english to turkish.
[MESSAGE]: message in turkish to be translated to english.
\n/en MESSAGE
translates a message from turkish to english.
[MESSAGE]: message in english to be translated to turkish.
''')
return
def t(update, context):
logger.info("%s", update.message.text)
dst = context.args[1]
src = context.args[0]
input=""
for i in range(2, len(context.args)):
input += context.args[i] + " "
print(src)
print(dst)
print(input)
output = translator.translate(input,lang_src=src, lang_tgt=dst)
print(output)
context.bot.send_message(chat_id=update.effective_chat.id, text=output)
return
def tr(update, context):
logger.info("%s", update.message.text)
dest = "tr"
input=""
for i in range(0, len(context.args)):
input += context.args[i] + " "
print(dest)
print(input)
output = translator.translate(input,lang_tgt=dest)
print(output)
context.bot.send_message(chat_id=update.effective_chat.id, text=output)
return
def en(update, context):
logger.info("%s", update.message.text)
dest = "en"
input=""
for i in range(0, len(context.args)):
input += context.args[i] + " "
print(dest)
print(input)
output = translator.translate(input,lang_tgt=dest)
print(output)
context.bot.send_message(chat_id=update.effective_chat.id, text=output)
return
def error(update, context):
logger.warning('Update "%s" caused error "%s"', update, context.error)
context.bot.send_message(chat_id=update.effective_chat.id,
text='what? what is ' + update.message.text + ' 😣😖🤯?????')
def main():
token = open('TOKEN', 'r').readline()
updater = Updater(token, use_context=True)
dp = updater.dispatcher
dp.add_handler(CommandHandler('start', start))
dp.add_handler(CommandHandler('help', help))
dp.add_handler(CommandHandler('t', t, pass_args=True))
dp.add_handler(CommandHandler('tr', tr, pass_args=True))
dp.add_handler(CommandHandler('en', en, pass_args=True))
dp.add_handler(MessageHandler(Filters.regex('^(/[a-zA-Z]+)'), error))
dp.add_error_handler(error)
updater.start_polling()
updater.idle()
if __name__ == '__main__':
main()