-
Notifications
You must be signed in to change notification settings - Fork 0
/
dialogflow.py
55 lines (41 loc) · 1.6 KB
/
dialogflow.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
from google.cloud import dialogflow
def get_reply_by_intent(project_id: str, session_id: str,
message: str, language_code: str):
session_client = dialogflow.SessionsClient()
session = session_client.session_path(project_id, session_id)
text_input = dialogflow.TextInput(
text=message, language_code=language_code
)
query_input = dialogflow.QueryInput(text=text_input)
response = session_client.detect_intent(
request={'session': session, 'query_input': query_input}
)
return (
response.query_result.intent.is_fallback,
response.query_result.fulfillment_text
)
def create_intent(project_id: str, display_name: str,
training_phrases_parts: list[str],
message_texts: list[str]) -> dialogflow.Intent:
intents_client = dialogflow.IntentsClient()
parent = dialogflow.AgentsClient.agent_path(project_id)
training_phrases = []
for training_phrases_part in training_phrases_parts:
part = dialogflow.Intent.TrainingPhrase.Part(
text=training_phrases_part
)
training_phrase = dialogflow.Intent.TrainingPhrase(parts=[part])
training_phrases.append(training_phrase)
text = dialogflow.Intent.Message.Text(text=message_texts)
message = dialogflow.Intent.Message(text=text)
intent = dialogflow.Intent(
display_name=display_name,
training_phrases=training_phrases,
messages=[message]
)
intents_client.create_intent(
request={
'parent': parent,
'intent': intent
}
)