<font size = 6>**Google Dialogflow API**</font>
# <font size = 5>VoiceTalk運用</font>
1. 安裝DialogFlow python套件
```
# python >= 3.7
pip install google-cloud-dialogflow
```
2. 因DialogFlow音檔只接受單聲道,利用ffmpeg進行轉換,須確認ffmpeg是否存在
```
ffmpeg -version
```
官網:https://ffmpeg.org/download.html
(panettone中有,不需另外安裝)
# <font size = 5>API使用</font>
```python=
import os
import config
from google.cloud import dialogflow_v2 as dialogflow
from google.api_core.exceptions import InvalidArgument
# DialogFlow 相關環境
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "金鑰.json"
# project_id : DialogFlow Essential設定 General頁面有顯示ID名稱
# session_id : 序號、可自訂
# audio_file_path : 音檔路徑
# language_code : "en-US"
def detect_intent_audio(project_id, session_id, audio_file_path, language_code):
"""Returns the result of detect intent with an audio file as input.
Using the same `session_id` between requests allows continuation
of the conversation."""
session_client = dialogflow.SessionsClient()
# Note: hard coding audio_encoding and sample_rate_hertz for simplicity.
audio_encoding = dialogflow.AudioEncoding.AUDIO_ENCODING_LINEAR_16
sample_rate_hertz = 48000
session = session_client.session_path(project_id, session_id)
print("Session path: {}\n".format(session))
with open(audio_file_path, "rb") as audio_file:
input_audio = audio_file.read()
audio_config = dialogflow.InputAudioConfig(
audio_encoding=audio_encoding,
language_code=language_code,
sample_rate_hertz=sample_rate_hertz,
)
query_input = dialogflow.QueryInput(audio_config=audio_config)
request = dialogflow.DetectIntentRequest(
session=session,
query_input=query_input,
input_audio=input_audio,
)
response = session_client.detect_intent(request=request)
input_ = response.query_result.query_text
output_ = response.query_result.fulfillment_text
return input_
```
# <font size = 5>DialogFlow設定與獲得API金鑰</font>
* Google Cloud Platform 可免費試用90天並提供US$300抵免額,需填寫住址、信用卡等資訊,到期後手動升級才開始扣款。
## <font size = 4> DialogFlow Essentail </font>
登入Google帳號進入[DialogFlow Essential](https://dialogflow.cloud.google.com/#/login)
->點擊**Create Agent**
->設定如下,點擊"CREATE"
* DEFAULT LANGUAGE: **English-en**
* DEFAULT TIME ZONE:**(GMT+8:00) Asia/Hong_Kong**
* GOOGLE PROJECT:**Create a new Google project**

點選齒輪⚙️進入設定
->到General
->設定 agent name (圖中設定為"demo0920")
->點擊 Project ID 的連結進入 Google Cloud Platform **(config中更改的Project ID)**

## <font size = 4> Google Cloud Platform設定 </font>
>
側邊欄:「IAM 與管理」-> 「服務帳戶」

建立「服務帳戶」

設定「服務帳戶名稱」:隨意取
設定「服務帳戶 ID」:(會自動產生)

「Dialogflow」->「Dialogflow API 用戶端」

直接按「完成」即可!
---
## <font size = 4> 產生金鑰 </font>
「管理金鑰」,進入金鑰管理頁面

「新增金鑰」,選擇 JSON,並按下建立。


