-
Notifications
You must be signed in to change notification settings - Fork 4
/
apptest.py
107 lines (67 loc) · 3.15 KB
/
apptest.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
import translator_tools
import random
import telebot
from flask import Flask, request
import os
API_TOKEN = '1146428469:AAGbR6x3n-p-1QJmdQ71aQtoAsFH7lrcSZ8'
bot = telebot.TeleBot(API_TOKEN)
server = Flask(__name__)
PORT = int(os.environ.get('PORT', '8443'))
instructions = """Hello! I'm the translator bot!
I will translate your messages into all chosen languages. After u can reply my answers to your foreign friends!
Also u can reply messages here.
All u need is to choose necessary languages and start messaging!
WARNING: because of free deploy your settings are reset periodically. If u haven't got a translate in 10sec, don't worry, just choose languages again. Fortunately, if u will send message into language not from langlist, this one is added automatically.
Problems? Questions? Write an issue https://github.com/PasaOpasen/TranslatorBot"""
show_it = 'Show instructions again, bot!'
want_choose = 'Choose languages'
#default = 'Set English + Russian'
my_langs = ['ru','en']
present = "U haven't selected languages yet (default Russian+English)"
keyboard1 = telebot.types.ReplyKeyboardMarkup(True,True)
keyboard1.row(show_it, want_choose)
def choice(id):
mes = ['Supported languages:\n']
mes.extend([f'{n+1}. {lang}' for n, lang in enumerate(translator_tools.all_langs)])
inds = [76, 22, 71]#[random.randint(1,100), random.randint(1,100), random.randint(1,100)]
a, _ = translator_tools.get_langs_from_numbers(inds)
mes.append(f"""\nU should choose some languages' numbers and write them like '1 2 3' (without quotes).
For example, the answer '{" ".join([str(i) for i in inds])}' means {"+".join(a)}.
{present}""")
bot.send_message(id, '\n'.join(mes))
@bot.message_handler(commands=['start'])
def start_message(message):
bot.send_message(message.chat.id, instructions, reply_markup=keyboard1)
#bot.reply_to(message, "Hi there, I am EchoBot.")
@bot.message_handler(content_types=['text'])
def send_text(message):
txt = message.text
if txt[0].isdigit() and txt[-1].isdigit():
global my_langs, present
t, my_langs = translator_tools.get_langs_from_numbers([int(n) for n in txt.split()])
if len(t) < 2:
bot.reply_to(message, "No sense to choose only 1 language. Select more")
return
lgs = '+'.join([str(i) for i in t])
present = f"Your current langlist is {lgs}"
bot.send_message(message.chat.id,f"Good! Your langlist is {lgs}. Now try to send any message")
return
if txt == show_it:
start_message(message)
elif txt == want_choose:
choice(message.chat.id)
else:
res = translator_tools.log_text(txt, my_langs)
bot.send_message(message.chat.id, '\n'.join(res))
bot.polling()
@server.route('/' + API_TOKEN, methods=['POST'])
def getMessage():
bot.process_new_updates([telebot.types.Update.de_json(request.stream.read().decode("utf-8"))])
return "!", 200
@server.route("/")
def webhook():
bot.remove_webhook()
bot.set_webhook(url='https://multi-translator-bot.herokuapp.com/' + API_TOKEN)
return "!", 200
if __name__ == "__main__":
server.run(host="0.0.0.0", port=int(os.environ.get('PORT', 5000)))