首页 > 解决方案 > 从 Ajax 函数 Web 方法调用 BOT 监听 URL

问题描述

我创建了一个带有提交按钮的 Web 应用程序。当用户单击此按钮时,将调用 Ajax 调用与 BOT 进行通信。我将以下代码用于 Ajax 函数

<script>  
    $(document).ready(function () {
        $("#btnSend").click(function (e) {
                $.ajax({
                    type: "POST",
                    url: "GroupChat.aspx/GetData",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (response) {
                        alert("success!");
                    },
                    error: function (response) {
                        alert(response.d);  
                    }
                });
           
            return false;
        });
    });
</script>

我使用以下代码通过网络方法与 BOT 进行通信

[WebMethod]
public static async Task GetData()
{
    Task<TokenResponse> response = GetTokenAsync();
    response.Wait();
    string token = response.Result.access_token;
    List<BOTConstants> lstConstants = new List<BOTConstants>();

    lstConstants.Add(new BOTConstants
    {
        text = "test message",
        channelId = "webApp",
        serviceUrl = "https://smba.trafficmanager.net/in/",
        textFormat = "plain",
        type = "message"
    });

    string json = (new JavaScriptSerializer()).Serialize(lstConstants);

    var data = new StringContent(json, Encoding.UTF8, "application/json");
    
    using (var client = new HttpClient())
    {
        //client.BaseAddress = new Uri("https://mybot.azurewebsites.net");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
       
        var result = await client.PostAsync("https://mybot.azurewebsites.net/api/messages", data).ConfigureAwait(false);
        string resultContent = await result.Content.ReadAsStringAsync();
        Console.WriteLine(resultContent);
    }
}

但是,var result = await client.PostAsync("https://mybot.azurewebsites.net/api/messages", data).ConfigureAwait(false);总是返回 Bad request。

任何人都可以帮助我解决这个问题,如果上述代码中有任何错误,请纠正我。

标签: c#htmlasp.netajaxbotframework

解决方案


您应该使用直接的 API,而不是直接发布到您的“api/messages”端点。

根据 Microsoft文档,以下应该是步骤。对于同一最终用户,您应该缓存令牌和对话 ID,以便您的对话可以在多轮对话中正常继续。

  1. 开始对话。
POST https://directline.botframework.com/v3/directline/conversations
Authorization: Bearer SECRET

//Response
{
  "conversationId": "abc123",
  "token": "RCurR_XV9ZA.cwA.BKA.iaJrC8xpy8qbOF5xnR2vtCX7CZj0LdjAPGfiCpg4Fv0y8qbOF5xPGfiCpg4Fv0y8qqbOF5x8qbOF5xn",
  "expires_in": 1800,
  "streamUrl": "https://directline.botframework.com/v3/directline/conversations/abc123/stream?t=RCurR_XV9ZA.cwA..."
}
  1. 在您获得对话 ID 例如“abc123”后,您可以开始将您的消息发布到此对话。
POST https://directline.botframework.com/v3/directline/conversations/abc123/activities
Authorization: Bearer RCurR_XV9ZA.cwA.BKA.iaJrC8xpy8qbOF5xnR2vtCX7CZj0LdjAPGfiCpg4Fv0
Content-Type: application/json
[other headers]

//Request body
{
    "locale": "en-EN",
    "type": "message",
    "from": {
        "id": "user1"
    },
    "text": "hello"
}

推荐阅读