-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
51 lines (32 loc) · 1.37 KB
/
main.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
##@package main
# Файл с реализацией flask-приложения
from __future__ import unicode_literals
import logging
import json
from AliceClass import AliceRequest, AliceResponse
from alice import handle_dialog
from flask import Flask, request
app = Flask(__name__)
session_storage = {}
with open("sessions.json", "w", encoding="utf8") as file:
json.dump(session_storage, fp=file)
@app.route("/", methods=["POST"])
## Основная функция
# Функция для взаимодействия между пользователем и сайтом
#@return Функция получает тело запроса и возвращает ответ
def main():
with open("sessions.json", encoding="utf8") as file:
session_storage = json.loads(file.read())
alice_request = AliceRequest(request.json)
alice_response = AliceResponse(alice_request)
user_id = alice_request.user_id
alice_response, session_storage[user_id] = handle_dialog(
alice_request, alice_response, session_storage.get(user_id)
)
# Потому что оперативка плохо работает
with open("sessions.json", "w", encoding="utf8") as file:
json.dump(session_storage, fp=file)
logging.info("Response: {}".format(alice_response))
return alice_response.dumps()
if __name__ == '__main__':
app.run()