예제 #1
0
        protected virtual async Task <Stream> ResolveFromSourceAsync(Attachment source)
        {
            if (!string.IsNullOrWhiteSpace(source.ContentUrl))
            {
                using (var client = new HttpClient())
                {
                    if (Microsoft​App​Credentials.IsTrustedServiceUrl(source.ContentUrl))
                    {
                        await client.AddAPIAuthorization();
                    }

                    var stream = await client.GetStreamAsync(source.ContentUrl);

                    var ms = new MemoryStream();
                    stream.CopyTo(ms);
                    ms.Position = 0;
                    return(ms);
                }
            }

            return(null);
        }
예제 #2
0
        /// <summary>
        /// POST: api/Messages
        /// Receive a message from a user and reply to it
        /// </summary>
        public async Task <HttpResponseMessage> Post([FromBody] Activity activity)
        {
            string conversationID = activity.Conversation.Id;

            StateClient stateClient      = activity.GetStateClient();
            BotData     conversationData = await stateClient.BotState.GetConversationDataAsync(activity.ChannelId, conversationID);

            // The URL of the currently active bot, either main or one of the sub bots.
            var forwardingUrl = conversationData.GetProperty <string>(Consts.ForwardingUrlKey);

            if (activity.Type == ActivityTypes.Message)
            {
                var message = activity as IMessageActivity;
                if (message != null && !string.IsNullOrEmpty(message.Text))
                {
                    dynamic client     = (dynamic)(activity.ChannelData);
                    string  clientName = (string)client.clientName;
                    var     commandUrl = (from cmd in DataSource.RegisteredBots
                                          where message.Text.Equals(cmd.Key, StringComparison.InvariantCultureIgnoreCase) ||
                                          (!string.IsNullOrEmpty(clientName) && clientName.Equals(cmd.Key, StringComparison.InvariantCultureIgnoreCase))
                                          select cmd.Value).FirstOrDefault();
                    if (commandUrl != null && !string.IsNullOrEmpty(commandUrl))
                    {
                        forwardingUrl = commandUrl;
                        conversationData.SetProperty <string>(Consts.ForwardingUrlKey, forwardingUrl);
                        await stateClient.BotState.SetConversationDataAsync(activity.ChannelId, conversationID, conversationData);
                    }
                }
            }

            if (string.IsNullOrEmpty(forwardingUrl) || Request.RequestUri.ToString().Equals(forwardingUrl, StringComparison.InvariantCultureIgnoreCase))
            {
                if (activity.Type == ActivityTypes.Message)
                {
                    await Conversation.SendAsync(activity, () => new RootDialog());
                }
                else
                {
                    HandleSystemMessage(activity);
                }
                var response = Request.CreateResponse(HttpStatusCode.OK);
                return(response);
            }
            else
            {
#if false
                var http = new HttpClient();
                await http.AddAPIAuthorization("3db271c5-3562-49da-b3a3-0a1ff69876a6", "jcWCIBZ7(pxdncNQ6742*;~");

                var json    = JsonConvert.SerializeObject(activity);
                var content = new StringContent(json, Encoding.UTF8, "application/json");
                var resp    = await http.PostAsync(new Uri(forwardingUrl), content);

                return(resp);
#else
                var http = new HttpClient();

                //In order to post message to expert bot, we need to pass Bot Authentication
                //Here, we first check if we have token cached, if no, we will request a new one
                //Since we are here, we must have below BotName
                //var botName = DataSource.RegisteredBots.Where(b => b.Value == forwardingUrl).Select(o => o.Key).SingleOrDefault();
                //var botBearerToken = await BotTokenCache.Get(botName);

                var request = new HttpRequestMessage
                {
                    RequestUri = new Uri(forwardingUrl),
                    Method     = HttpMethod.Post
                };

                try
                {
                    foreach (var header in Request.Headers)
                    {
                        //if (header.Key == "Authorization")
                        //    continue;
                        request.Headers.Add(header.Key, header.Value);
                    }
                    request.Headers.Host = request.RequestUri.Host;
                    var json    = JsonConvert.SerializeObject(activity);
                    var content = new StringContent(json, Encoding.UTF8, "application/json");
                    //request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", botBearerToken);
                    request.Content = content;
                    //return await http.PostAsync(request.RequestUri,content);
                    var resp = await http.SendAsync(request);

                    return(resp);
                }
                catch (Exception exp)
                {
                    throw;
                    //var log = $"[Exception]{exp.Message}";
                    //var inner = exp.InnerException != null ? exp.InnerException.Message : "";
                    //log += $"\n\n{inner}";
                    //activity.Text = log;
                    //await Conversation.SendAsync(activity, () => new RootDialog());
                    //return await http.SendAsync(request);
                }
#endif
            }
        }