【问题标题】:Call BOT listening URL from Ajax function Web method从 Ajax 函数 Web 方法调用 BOT 监听 URL
【发布时间】:2020-12-29 22:49:50
【问题描述】:

我创建了一个带有提交按钮的 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); 总是返回错误请求。

谁能帮我解决这个问题,如果上面的代码有任何错误,请纠正我。

【问题讨论】:

  • 你从哪里得到的 C# 代码?您可以链接到您遵循的任何示例或文档吗?您的机器人的 api/messages 端点是否设计为接收机器人常量对象数组而不是活动?你能从你的机器人发布相关代码吗?
  • 你还在做这个吗?

标签: c# html asp.net ajax botframework


【解决方案1】:

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

基于 Microsoft documentation,以下应该是步骤。对于同一个最终用户,您应该缓存令牌和对话 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"
}

【讨论】:

    猜你喜欢
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-30
    • 1970-01-01
    相关资源
    最近更新 更多