google 上的操作通过 ASP.NET C# 做出响应

Actions on google make response via ASP.NET C#

也许有人知道怎么做?为了更好地理解,我附上了屏幕截图。我正在使用 ASP.NET.

我成功收到了请求,但我不知道如何处理它以及如何回复。例如消息“Test123”

命名空间GoogleAsisstantServer.Controllers

{

[路线("api/[控制器]")]

[ApiController]

public class GoogleController : ControllerBase
{

    [HttpGet]
    public IActionResult Get()
    {
        var result = new Result();
        result.Value1 = 123;

        return Ok(result);
    }
    [HttpPost]
    public async Task<IActionResult> PostWebHook()
    {

        string body;
        using (var reader = new StreamReader(Request.Body))
        {
            body = await reader.ReadToEndAsync();

        }
        return Ok("Test123");
            
    }
}

public class Result
{
    public int Value1 { get; set; }
    public int Value2 { get; set; }

}

}

我应该如何处理这里的请求然后 return 什么?例如消息“Test123”

这是我得到的,但我不知道如何处理它并发回响应。

{"handler":{"name":"abc"},"intent":{"name":"actions.intent.MAIN","params":{},"query":"Mit Google sprechen"},"scene":{"name":"actions.scene.START_CONVERSATION","slotFillingStatus":"UNSPECIFIED","slots":{},"next":{"name":"Start_Frage"}},"session":{"id":"ABwppHHVumDrliLJaLSikS6KnIlN7yYv6Z4XJCOYzEZt8Fr08RH6r0wtM2-E0v40lS2p1YosTDfpSCd5Lw","params":{},"typeOverrides":[],"languageCode":""},"user":{"locale":"de-DE","params":{},"accountLinkingStatus":"ACCOUNT_LINKING_STATUS_UNSPECIFIED","verificationStatus":"VERIFIED","packageEntitlements":[],"gaiamint":"","permissions":[],"lastSeenTime":"2021-04-01T10:06:59Z"},"home":{"params":{}},"device":{"capabilities":["SPEECH","RICH_RESPONSE","LONG_FORM_AUDIO"]}}

请求以 JSON 格式发送给您。您需要将其解析为一个对象,以便您可以使用它。

您需要做的第一件事是定义要退休的对象。这记录在 request object format.

非常基本的示例。

public class Handler
{
    public string name { get; set; }
}

public class Params
{
    public string UserId { get; set; }
    public int NumVisits { get; set; }
}

public class Intent
{
    public string name { get; set; }
    public Params @params { get; set; }
    public string query { get; set; }
}

public class Slots
{
}

public class Next
{
    public string name { get; set; }
}

public class Scene
{
    public string name { get; set; }
    public string slotFillingStatus { get; set; }
    public Slots slots { get; set; }
    public Next next { get; set; }
}

public class Session
{
    public string id { get; set; }
    public Params @params { get; set; }
    public List<object> typeOverrides { get; set; }
    public string languageCode { get; set; }
}

public class User
{
    public string locale { get; set; }
    public Params @params { get; set; }
    public string accountLinkingStatus { get; set; }
    public string verificationStatus { get; set; }
    public List<object> packageEntitlements { get; set; }
    public string gaiamint { get; set; }
    public List<object> permissions { get; set; }
    public DateTime lastSeenTime { get; set; }
}

public class Home
{
    public Params @params { get; set; }
}

public class Device
{
    public List<string> capabilities { get; set; }
}

public class Response
{
    public Handler handler { get; set; }
    public Intent intent { get; set; }
    public Scene scene { get; set; }
    public Session session { get; set; }
    public User user { get; set; }
    public Home home { get; set; }
    public Device device { get; set; }
}

定义对象后,您就可以反序列化请求。

 var values = JsonSerializer.Deserialize<Response>(body);

由于还没有人为操作创建库,因此您需要自己创建。

同样,对于响应,您需要发回使用 response object format.

格式化的 JSON