我正在为托管在 IIS 中并在 Azure 中注册为 BOT 通道注册 BOT 的 BOT 处理主动消息。
我得到以下错误,
消息:操作返回无效状态代码“未授权”
StackTrace:在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) 在 Microsoft.Bot.Connector.ConversationsExtensions.d__17.MoveNext()
代码如下所示,
-- 通知.cs
public class Notify
{
private readonly IBotFrameworkHttpAdapter _adapter;
private readonly string _appId;
private readonly ConcurrentDictionary<string, ConversationReference> _conversationReferences;
public Notify(IBotFrameworkHttpAdapter adapter, string appId, ConcurrentDictionary<string, ConversationReference> conversationReferences)
{
_adapter = adapter;
_appId = appId;
_conversationReferences = conversationReferences;
}
public async Task<IActionResult> Get()
{
try
{
foreach (var conversationReference in _conversationReferences.Values)
{
await ((BotAdapter)_adapter).ContinueConversationAsync(_appId, conversationReference, BotCallback, default(CancellationToken));
}
// Let the caller know proactive messages have been sent
return new ContentResult()
{
Content = "<html><body><h1>Proactive messages have been sent.</h1></body></html>",
ContentType = "text/html",
StatusCode = (int)HttpStatusCode.OK,
};
}
catch (Exception ex)
{
throw ex;
}
}
private async Task BotCallback(ITurnContext turnContext, CancellationToken cancellationToken)
{
await turnContext.SendActivityAsync("proactive hello");
}
}
编辑 1:
我正在尝试将主动消息发送到系统托盘图标中显示的机器人。我们创建了一个 windows 应用程序,通过它我们向用户发送 windows 通知。当用户单击通知时,它会打开系统托盘图标中存在的机器人,并且它应该在 BOT 中显示与主动消息相同的消息通知消息。
我们使用直线 API 来创建对话参考。我们正在传递相同的对话引用并使用 BotApadater 的 ContinueConversationAsync 从打开的机器人中填充相同的消息。form.cs 和 Notify.cs 的整个代码在 windows 应用程序中返回,bot 托管在 IIS 上并在 Azure 中注册为 BOT Channel 注册 BOT。
目前,我们没有收到未经授权的错误并且代码已成功执行,但我们没有收到来自机器人的任何主动消息。为了打开机器人,我们在 Windows 应用程序中使用 iframe URL。
请帮助我在这里做错了什么或提出其他一些方法。
窗口系统托盘应用 Form.cs 方法
using (HttpClient httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri("https://directline.botframework.com/api/tokens");
httpClient.DefaultRequestHeaders.Accept.Clear();
//httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("BotConnector", "Directline secret");
HttpResponseMessage response = httpClient.GetAsync(httpClient.BaseAddress).Result;
if (response.IsSuccessStatusCode)
{
var jsonString = response.Content.ReadAsStringAsync();
var token = jsonString.Result;
var finaltoken = token.Replace(@"\", string.Empty);
finaltoken = finaltoken.Replace('"', ' ').Trim();
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://directline.botframework.com/v3/directline/conversations");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(finaltoken);
var httptokenResponse = httpClient.PostAsync("/api/conversations/", new StringContent(string.Empty, System.Text.Encoding.UTF8, "application/json"));
var tokenResponse = httptokenResponse.Result;
string convId = string.Empty;
string activityId = string.Empty;
HttpClient finalclient = new HttpClient();
Microsoft.Bot.Schema.Activity act = new Microsoft.Bot.Schema.Activity();
ConversationReference convRef = new ConversationReference();
if (tokenResponse.IsSuccessStatusCode)
{
var tokenjsonString = tokenResponse.Content.ReadAsStringAsync().Result;
var conversationToken = Newtonsoft.Json.JsonConvert.DeserializeObject<Conversation>(tokenjsonString.ToString());
HttpClient lastclient = new HttpClient();
lastclient.BaseAddress = new Uri("https://directline.botframework.com/v3/directline/conversations/" + conversationToken.conversationId + "/activities");
lastclient.DefaultRequestHeaders.Accept.Clear();
lastclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
lastclient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "Directline secret");
string conversationUrl = conversationToken.conversationId + "/activities/";
string msg2 = "User Message";
Rootobject rootobject = new Rootobject();
rootobject.type = "message";
rootobject.text = msg2;
From from = new From();
from.id = "User ID";
from.user = "User Name";
rootobject.from = from;
string json = "";
json = Newtonsoft.Json.JsonConvert.SerializeObject(rootobject);
var httpconvResponse = lastclient.PostAsync(lastclient.BaseAddress, new StringContent(json, System.Text.Encoding.UTF8, "application/json"));
var tokenconvResponse = httpconvResponse.Result;
if (tokenconvResponse.IsSuccessStatusCode)
{
var tokenconvjsonString = tokenconvResponse.Content.ReadAsStringAsync().Result;
var conversationconvToken = Newtonsoft.Json.JsonConvert.DeserializeObject<ConversationActivityInfo>(tokenconvjsonString.ToString());
convId = conversationToken.conversationId;
activityId = conversationconvToken.id.Split('|')[1];
finalclient.BaseAddress = new Uri("https://directline.botframework.com/v3/directline/conversations/" + conversationToken.conversationId + "/activities");
finalclient.DefaultRequestHeaders.Accept.Clear();
finalclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
finalclient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "Directline Secret");
var httpconvfinalResponse = finalclient.GetAsync(finalclient.BaseAddress).Result;
var jsonResponseString = httpconvfinalResponse.Content.ReadAsStringAsync();
var finalresponse = jsonResponseString.Result;
var activitiesObject = Newtonsoft.Json.JsonConvert.DeserializeObject<ActivitiesRO>(finalresponse);
act = activitiesObject.activities[1];
convRef = act.GetConversationReference();
}
}
string MicroSoftAppId = "YOUR Microsoft APP ID";
string MicroSoftAppPassword = "YOUR Microsoft APP PASSWORD";
ICredentialProvider credentialProvider = new SimpleCredentialProvider(MicroSoftAppId, MicroSoftAppPassword);
MicrosoftAppCredentials.TrustServiceUrl(convRef.ServiceUrl);
IBotFrameworkHttpAdapter adapter = new BotFrameworkHttpAdapter(credentialProvider);
ConcurrentDictionary<string, ConversationReference> conversationReferences = new ConcurrentDictionary<string, ConversationReference>();
convRef.Bot = new ChannelAccount("Bot ID", "Bot Name");
conversationReferences.GetOrAdd("test", convRef);
Notify notify = new Notify(adapter, MicroSoftAppId, conversationReferences);
Task<IActionResult> obj = notify.Get();
}
}
**编辑2:**请参考下面的截图,我们在向以下URL发送请求后得到三个活动ID, https://directline.botframework.com/v3/directline/conversations/ “+ conversationToken.conversationId +”/活动”
其中我们使用的是索引1活动,因为它包含服务 URL 和需要主动向其发送消息的用户详细信息,并且它没有 BOT 详细信息(Bot 属性),因此我们手动分配它。