-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
56 lines (48 loc) · 1.2 KB
/
bot.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
import telebot
import translate
import json
import sys
token=''
try:
f = open('token.txt', 'r')
token = f.read()
except FileNotFoundError:
print('ERROR: token.txt does not exist. Create file token.txt in the same directory and'\
'place Telegram Bot\'s token there. To get a token write to @botfather')
sys.exit()
else:
f.close()
bot = telebot.TeleBot(token)
def detect_language(text):
avg = 0
cnt = 0
mx = 0
for i in text:
if i.isalpha():
avg += ord(i)
cnt += 1
if mx < ord(i):
mx = ord(i)
if cnt > 0:
avg /= cnt
if abs(avg - 100) > abs(avg - 1000):
if mx > 1104:
return 'kk'
else:
return 'ru'
return 'en'
def pr(sonj):
print(json.dumps(sonj, indent=2))
@bot.message_handler(commands=['start', 'help'])
def send_welcome(message):
#print(message.keys())
bot.send_message(message.chat.id, "Send text in english, russian or kazakh, I will paraphrase it")
@bot.message_handler(func=lambda message: True)
def echo_all(message):
x = message.text
langs = [detect_language(x), 'pt', 'tr']
for i in range(len(langs)):
x = translate.google(x, langs[i], langs[(i+1) % len(langs)])
bot.send_message(message.chat.id, 'Please wait, paraphrasing...')
bot.reply_to(message, x)
bot.polling()