c# - 从 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。
任何人都可以帮助我解决这个问题,如果上述代码中有任何错误,请纠正我。
解决方案
您应该使用直接的 API,而不是直接发布到您的“api/messages”端点。
根据 Microsoft文档,以下应该是步骤。对于同一最终用户,您应该缓存令牌和对话 ID,以便您的对话可以在多轮对话中正常继续。
- 开始对话。
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..."
}
- 在您获得对话 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"
}
推荐阅读
- arrays - 使用 go 中的范围选择 2D 切片的 2D 子切片
- vagrant - 关于我的 vagrant vm 的“No space left on device”错误的两个问题
- sql - sql查询函数中的变量参数
- python - 如何为不连续的照片颜色蒙版区域生成单独的边界框
- r - 代码未在指定条件下过滤数据
- c - GCC 对 const 限定符的警告是否正确?
- c++ - 强制 g++ 指示何时不包含库
- android - Gradle 3.3.0 和 Android Studio 3.3.0 上的 Kotlin Android 扩展无法解析 id
- javascript - 如何使用javascript根据另一个单元格上的单选按钮更改表格单元格颜色?
- javascript - expressjs - 没有支持格式和 mime 类型的视频,为破折号视频找到错误