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

问题描述 投票:0回答:1

我正在使用 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 作为依赖项,但我不确定使用哪些类来获取包含 POST 请求中的用户输入数据的
Memory
主体。有使用 Twilio 函数来使用此功能的 Twilio 文档,但没有外部应用程序。有人对如何做到这一点有任何想法或建议吗?

java spring spring-boot twilio
1个回答
3
投票

我没有使用 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();
    }
}

© www.soinside.com 2019 - 2024. All rights reserved.