我正在使用 Twilio Autopilot 的收集功能通過電話或短信收集用戶輸入。在收集任務結束時,我通過 POST 重定向到 Spring Boot 應用程式中的端點。我希望我的 Spring Boot 應用程式驗證用戶的輸入,然后將預期的 Action JSON 回傳給 Twilio,讓用戶知道他們的資料已成功記錄和驗證。這是我的收集任務的樣子:
{
"actions": [
{
"say": "Hello!"
},
{
"collect": {
"name": "get_prices",
"questions": [
{
"question": " Please enter the current price for 100 low led using the dial pad. When you are finished, press pound.",
"voice_digits": {
"finish_on_key": "#"
},
"name": "price_100ll",
"type": "Twilio.NUMBER",
"validate": {
"on_failure": {
"messages": [
{
"say": "Sorry, I didn't quite get that."
}
],
"repeat_question": true
},
"on_success": {
"say": "Great, we have successfully recorded your 100 low led price."
}
}
}
],
"on_complete": {
"redirect": {
"method": "POST",
"uri": "https://<ngrok url pointing at my Spring Boot app>/report"
}
}
}
}
]
}
這是 Spring Boot 中我的控制器中的 POST 端點:
@PostMapping(value = "/report", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<Void> report(Object body) {
return ResponseEntity.ok().build();
}
我相信我的 POST 端點設定不正確。body始終是一個空物件。我將 Twilio SDK 作為依賴項,但我不確定使用哪些類Memory從 POST 請求中獲取包含用戶輸入資料的正文。有使用 Twilio 函式來使用它的 Twilio 檔案,但不是外部應用程式。有沒有人對如何做到這一點有任何想法或建議?
uj5u.com熱心網友回復:
我沒有使用 Autopilot,但據我所知,它會使用“記憶體”引數將答案發送到您的端點。
假設傳入的請求是這樣的:
https://<Ngrok URL>/report?Memory={"twilio": {...}}
您可以按如下方式獲取此請求,然后將其轉換為模型或鍵值存盤。
@PostMapping(value = "/report")
@CrossOrigin(origins = "*", allowedHeaders = "*")
public ResponseEntity<Void> report(@RequestParam(value = "Memory") String memory) {
try {
Map<String, Object> answer = new ObjectMapper()
.readValue(memory, Map.class);
return ResponseEntity.ok().build();
} catch (Exception e) {
return ResponseEntity.internalServerError().build();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/485713.html
