如何在 Spring 中从 Twilio 自动驾驶仪获取 POST 数据

How to get POST data from Twilio autopilot in Spring

我正在使用 Twilio Autopilot 的收集功能通过 phone 或短信收集用户输入。在收集任务结束时,我通过 POST 重定向到我的 Spring 引导应用程序中的端点。我想让我的 Spring 启动应用程序验证用户的输入,然后 return 预期的操作 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 启动时我的控制器中的 POST 端点:

@PostMapping(value = "/report", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public ResponseEntity<Void> report(Object body) {
    return ResponseEntity.ok().build();
}

我认为我的 POST 端点设置不正确。 body 始终是一个空对象。我有 Twilio SDK 作为依赖项,但我不确定使用哪个 类 从 POST 请求中获取包含用户输入数据的 Memory 主体。有关于使用 Twilio 函数来使用它的 Twilio 文档,但不是外部应用程序。有没有人对如何做到这一点有任何想法或建议?

我没有使用 Autopilot,但是 I see it will send the answers to your endpoint with the "Memory" parameter.

假设传入请求是这样的:

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();
    }
}