0

我正在为托管在 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 属性),因此我们手动分配它。

在此处输入图像描述

4

1 回答 1

0

我相信您的问题可能是您正在尝试为ConversationReference不存在的对话创建一个对话(尽管我不知道从何convoId而来)。

如果这是针对用户尚未与机器人交互的全新对话,您可以使用CreateConversationAsync你可以在这里看到我们如何在测试中做到这一点:

var activity = new Activity()
{
    Type = ActivityTypes.Message,
    Recipient = User,
    From = Bot,
    Text = "TEST Create Conversation",
};

var param = new ConversationParameters()
{
    Members = new ChannelAccount[] { User },
    Bot = Bot,
    Activity = activity,
};

var convoId = await client.Conversations.CreateConversationAsync(param);

注意:当用户从未与机器人交谈(主要是电子邮件/短信相关的渠道)时,某些客户端不允许主动消息

如果用户与机器人进行交互,您可以使用Proactive Sample作为参考。特别是,这里

var conversationReference = activity.GetConversationReference();

或者,它可能是一个TrustServiceUrl 问题

您可以通过将频道的 ServiceUrls 添加到受信任的 URL 列表来修复它:

var serviceUrl = <GetThisFrom Activity.ServiceUrl>;

MicrosoftAppCredentials.TrustServiceUrl(serviceUrl);

这是方法定义。注意:您也可以为此添加过期时间。否则,浏览这些问题应该会有所帮助。

您的机器人也可能正在睡觉。您可以通过访问来解决问题Azure Portal > Your App Service > Configuration > General Settings > Always On <enable>

给其他人的注意事项:

这种“信任服务 URL 问题”很常见。在尝试使用主动消息传递时,许多其他 URL 都会发生这种情况。只需替换serviceUrl适合您的用例的任何内容。是的,如果您使用多个渠道,您可以在使用时MicrosoftAppCredentials.TrustServiceUrl()通过多次调用来添加多个 URL。

于 2019-06-17T17:35:24.220 回答