Repository navigation
Expand file tree
/
Copy pathserver.py
More file actions
49 lines (44 loc) · 1.95 KB
/
Copy pathserver.py
File metadata and controls
49 lines (44 loc) · 1.95 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, make_response, jsonify
import json
import requests
'''
Functions of the imported modules =>
1. Flask - to initialise and use the flask app
2. request - to handle the requests sent to this flask server
3. make_response - creates a response object for the flask server
4. jsonify - creates a json response object, with header set to json
5. json - JSON is a syntax for storing and exchanging data.
6. requests - to make requests ('GET','POST',...) from python files
'''
app = Flask(__name__)
url = 'https://api.funtranslations.com/translate/'
@app.route('/webhook', methods=['POST'])
def webhook():
req = request.get_json(silent=True, force=True)
print(req)
query_result = req.get('queryResult')
try:
intent = # extact intent name ( displayName ) attribute from query_result
except AttributeError:
return 'json error'
print('Intent: ' + intent)
if intent == 'Translation Init':
fulfillment = # extact text ( fulfillmentText ) attribute from query_result
language = # extract language attribute from query_result
return make_response(jsonify({'fulfillmentText': fulfillment, 'followupEventInput': {'name': 'Translate', 'parameters': {'language': language}, 'languageCode': 'en-US'}}))
elif intent == 'Translate':
query = # extact quert text ( queryText ) attribute from query_result
language = # extract language attribute passed as context from query_result
payload = {'text': query}
final_url = url + language + '.json'
res = # make a post request to final_url using given data ( payload )
if res.status_code == 200:
text = # extract result ( translated text ) from response object
else:
print(res.status_code)
text = "request overlimit"
return # return a response object in json with fulfillmentText as attribute mapped with retreived results from API
else:
return # return a response object in json with fulfillmentText as attribute
if __name__ == '__main__':
app.run()