-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
49 lines (40 loc) · 1.39 KB
/
server.py
File metadata and controls
49 lines (40 loc) · 1.39 KB
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
from flask import Flask, request, json, render_template
from answerSearch import bot_answer
from config import *
import vk
app = Flask(__name__)
def text_for_vk(matrix):
text = "Результаты поиска\n\n"
for row in matrix:
text += "Расстояние: {0}\nСхожий вопрос: {1}\n Ответ: {2}".format(*row) + "\n\n"
return text
@app.route('/')
def page():
from answerSearch import bot_answer
question = request.args.get("question")
try:
count = int(request.args.get("count"))
answers = bot_answer(question, count)
except:
if question is None:
return render_template("info.html")
answers = bot_answer(question)
return render_template("answer.html", answers=answers)
@app.route('/', methods=['POST'])
def processing():
data = json.loads(request.data.decode('utf8'))
print(data)
if data["secret"] != secret_key:
return 'not vk'
elif data['type'] == 'confirmation':
return confirm_key
elif data['type'] == 'message_new':
session = vk.Session()
api = vk.API(session, v=5.0)
user_id = data['object']['user_id']
message = data['object']['body']
answer = bot_answer(message)
api.messages.send(access_token=token, user_id=str(user_id), message=text_for_vk(answer))
return "ok"
if __name__ == '__main__':
app.run()