This repository has been archived by the owner on Aug 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_intents.py
73 lines (58 loc) · 1.7 KB
/
load_intents.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
import logging
import os
import requests
from dotenv import load_dotenv
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
logger = logging.getLogger(__name__)
def download_phrases():
url = "https://dvmn.org/media/filer_public/a7/db/a7db66c0-1259-4dac-9726-2d1fa9c44f20/questions.json"
try:
response = requests.get(url)
response.raise_for_status()
if response.ok:
return response.json()
except requests.exceptions.HTTPError as e:
logger.error(f'HTTPError: {e}')
def post_intent(data):
baseurl = "https://api.dialogflow.com/v1/"
api_command = "intents"
access_token = os.getenv("DIALOGFLOW_DEV_TOKEN")
headers = {
"Authorization": f"Bearer {access_token}"
}
response = requests.post(
f"{baseurl}{api_command}",
headers=headers,
json=data
)
if response.ok:
return response.json()
def main():
load_dotenv()
phrases = download_phrases()
for phrase in phrases:
name = phrase
speech = phrases[phrase]['answer']
template = {
"name": name,
"auto": True,
"responses": [
{
"speech": speech
}
],
"userSays": []
}
for question in phrases[phrase]['questions']:
user_says_data = {
"data": [
{
"text": question
}
]
}
template['userSays'].append(user_says_data)
logger.info(post_intent(template))
if __name__ == '__main__':
main()