Ejemplo n.º 1
0
        public static StreamingRequest GetStreamingRequestWithoutAttachments(string conversationId)
        {
            var conId = string.IsNullOrWhiteSpace(conversationId) ? Guid.NewGuid().ToString() : conversationId;

            var request = new StreamingRequest()
            {
                Verb = "POST",
                Path = $"/v3/directline/conversations/{conId}/activities",
            };

            var activity = new Schema.Activity()
            {
                Type       = "message",
                Text       = "hello",
                ServiceUrl = "urn:test:namedpipe:testPipes",
                From       = new Schema.ChannelAccount()
                {
                    Id   = "123",
                    Name = "Fred",
                },
                Conversation = new Schema.ConversationAccount(null, null, conId, null, null, null, null),
            };

            request.SetBody(activity);

            return(request);
        }
Ejemplo n.º 2
0
        public static StreamingRequest GetStreamingRequestWithAttachment(string conversationId)
        {
            var conId            = string.IsNullOrWhiteSpace(conversationId) ? Guid.NewGuid().ToString() : conversationId;
            var attachmentData   = "blah blah i am a stream!";
            var streamContent    = new MemoryStream(Encoding.UTF8.GetBytes(attachmentData));
            var attachmentStream = new AttachmentStream("botframework-stream", streamContent);

            var request = new StreamingRequest()
            {
                Verb = "POST",
                Path = $"/v3/directline/conversations/{conId}/activities",
            };
            var activity = new Schema.Activity()
            {
                Type       = "message",
                Text       = "hello",
                ServiceUrl = "urn:test:namedpipe:testPipes",
                From       = new Schema.ChannelAccount()
                {
                    Id   = "123",
                    Name = "Fred",
                },
                Conversation = new Schema.ConversationAccount(null, null, conId, null, null, null, null),
            };

            request.SetBody(activity);

            var contentStream = new StreamContent(attachmentStream.ContentStream);

            contentStream.Headers.TryAddWithoutValidation(HeaderNames.ContentType, attachmentStream.ContentType);
            request.AddStream(contentStream);

            return(request);
        }
        public async Task <Conversation> StartConversationAsync(TokenParameters tokenParameters = null, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (SocketClient == null)
            {
                throw new InvalidOperationException("Connection is not opened.");
            }

            var request = new StreamingRequest()
            {
                Verb = "POST",
                Path = "/v3/directline/conversations"
            };

            if (tokenParameters != null)
            {
                request.SetBody(tokenParameters);
            }

            var response = await SocketClient.SendAsync(request).ConfigureAwait(false);

            if (response.StatusCode != 200 && response.StatusCode != 201)
            {
                var body = response.ReadBodyAsStringAsync().ConfigureAwait(false);
                var ex   = new OperationException(
                    $"Operation returned an invalid status code '{response.StatusCode}'",
                    response.StatusCode,
                    body);
                throw ex;
            }

            var conversation = await response.ReadBodyAsJsonAsync <Conversation>().ConfigureAwait(false);

            return(conversation);
        }
Ejemplo n.º 4
0
        protected override async Task OnMembersAddedAsync(IList <ChannelAccount> membersAdded, ITurnContext <IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
        {
            foreach (var member in membersAdded)
            {
                if (member.Id != turnContext.Activity.Recipient.Id)
                {
                    if (!_turnContexts.ContainsKey(turnContext.Activity.Conversation.Id))
                    {
                        _turnContexts.TryAdd(turnContext.Activity.Conversation.Id, turnContext);
                    }
                    var request = new StreamingRequest()
                    {
                        Verb = "POST",
                        Path = "/api/messages"
                    };
                    turnContext.Activity.ServiceUrl = "urn:BridgeBot:ws://localhost";
                    request.SetBody(turnContext.Activity);

                    if (_useNamedPipes)
                    {
                        await _namedPipeClient.SendAsync(request).ConfigureAwait(false);
                    }

                    if (_useWebSockets)
                    {
                        await _webSocketClient.SendAsync(request).ConfigureAwait(false);
                    }
                }
            }
        }
        public async Task <ResourceResponse> UpdateActivityAsync(string conversationId, string activityId, Activity activity, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (SocketClient == null)
            {
                throw new InvalidOperationException("Connection is not opened.");
            }

            var request = new StreamingRequest()
            {
                Verb = "PUT",
                Path = $"/v3/directline/conversations/{conversationId}/activities/{activityId}"
            };

            request.SetBody(activity);

            var response = await SocketClient.SendAsync(request).ConfigureAwait(false);

            if (response.StatusCode != 200)
            {
                var body = await response.ReadBodyAsStringAsync().ConfigureAwait(false);

                var ex = new OperationException(
                    $"Operation returned an invalid status code '{response.StatusCode}'",
                    response.StatusCode,
                    body);
                throw ex;
            }

            var resourceResponse = await response.ReadBodyAsJsonAsync <ResourceResponse>().ConfigureAwait(false);

            return(resourceResponse);
        }
Ejemplo n.º 6
0
                    private async Task <StreamingRequest> CreateSteamingRequestAsync(HttpRequestMessage httpRequestMessage)
                    {
                        var streamingRequest = new StreamingRequest
                        {
                            Path = httpRequestMessage.RequestUri.OriginalString.Substring(httpRequestMessage.RequestUri.OriginalString.IndexOf("/v3", StringComparison.Ordinal)),
                            Verb = httpRequestMessage.Method.ToString(),
                        };

                        streamingRequest.SetBody(await httpRequestMessage.Content.ReadAsStringAsync().ConfigureAwait(false));
                        return(streamingRequest);
                    }
        public override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            var streamingRequest = new StreamingRequest
            {
                Path = request.RequestUri.OriginalString.Substring(request.RequestUri.OriginalString.IndexOf("/v3", StringComparison.Ordinal)),
                Verb = request.Method.ToString(),
            };

            streamingRequest.SetBody(request.Content);

            return(await SendRequestAsync <HttpResponseMessage>(streamingRequest, cancellationToken).ConfigureAwait(false));
        }
        public async Task RequestExtensions_SetBodyString_Success()
        {
            var r = new StreamingRequest();

            r.SetBody("123");

            Assert.IsNotNull(r.Streams);
            Assert.AreEqual(1, r.Streams.Count);
            Assert.AreEqual(typeof(StringContent), r.Streams[0].Content.GetType());

            var s = await r.Streams[0].Content.ReadAsStringAsync().ConfigureAwait(false);

            Assert.AreEqual("123", s);
        }
        public void RequestExtensions_SetBody_Null_Does_Not_Throw()
        {
            var       r  = new StreamingRequest();
            Exception ex = null;

            try
            {
                r.SetBody(null);
            }
            catch (Exception caughtEx)
            {
                ex = caughtEx;
            }
            finally
            {
                Assert.AreEqual(ex, null);
            }
        }
        public async Task RequestExtensions_SetBody_Success()
        {
            var r = new StreamingRequest();
            var a = new Activity()
            {
                Text = "hi", Type = "message"
            };

            r.SetBody(a);

            Assert.IsNotNull(r.Streams);
            Assert.AreEqual(1, r.Streams.Count);
            Assert.AreEqual(typeof(StringContent), r.Streams[0].Content.GetType());

            var s = await r.Streams[0].Content.ReadAsAsync <Activity>().ConfigureAwait(false);

            Assert.AreEqual(a.Text, s.Text);
            Assert.AreEqual(a.Type, s.Type);
        }
Ejemplo n.º 11
0
        public async Task RequestExtensions_SetBody_Success()
        {
            var r = new StreamingRequest();
            var a = new Activity()
            {
                Text = "hi", Type = "message"
            };

            r.SetBody(a);

            Assert.NotNull(r.Streams);
            Assert.Single(r.Streams);
            Assert.Equal(typeof(StringContent), r.Streams[0].Content.GetType());

            var s = JsonConvert.DeserializeObject <Activity>(await r.Streams[0].Content.ReadAsStringAsync().ConfigureAwait(false));

            Assert.Equal(a.Text, s.Text);
            Assert.Equal(a.Type, s.Type);
        }
Ejemplo n.º 12
0
        public void RequestExtensions_SetBodyObject_Null_Does_Not_Throw()
        {
            Exception ex = null;

            var r = new StreamingRequest();

            try
            {
                r.SetBody((object)null);
            }
            catch (Exception caughtEx)
            {
                ex = caughtEx;
            }
            finally
            {
                Assert.Null(ex);
            }
        }
        public async Task <ResourceResponse> PostActivityAsync(string conversationId, Activity activity, CancellationToken cancellationToken = default(CancellationToken), params AttachmentStream[] attachmentStreams)
        {
            if (SocketClient == null)
            {
                throw new InvalidOperationException("Connection is not opened.");
            }

            var request = new StreamingRequest()
            {
                Verb = "POST",
                Path = $"/v3/directline/conversations/{conversationId}/activities"
            };

            request.SetBody(activity);

            if (attachmentStreams != null && attachmentStreams.Length > 0)
            {
                foreach (var stream in attachmentStreams)
                {
                    var contentStream = new StreamContent(stream.ContentStream);
                    contentStream.Headers.TryAddWithoutValidation(HeaderNames.ContentType, stream.ContentType);
                    request.AddStream(contentStream);
                }
            }

            var response = await SocketClient.SendAsync(request).ConfigureAwait(false);

            if (response.StatusCode != 200 && response.StatusCode != 204)
            {
                var body = await response.ReadBodyAsStringAsync().ConfigureAwait(false);

                var ex = new OperationException(
                    $"Operation returned an invalid status code '{response.StatusCode}'",
                    response.StatusCode,
                    body);
                throw ex;
            }

            var resourceResponse = await response.ReadBodyAsJsonAsync <ResourceResponse>().ConfigureAwait(false);

            return(resourceResponse);
        }
Ejemplo n.º 14
0
        protected override async Task OnMessageActivityAsync(ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            //proxy the message over to the streaming bot
            var request = new StreamingRequest()
            {
                Verb = "POST",
                Path = "/api/messages"
            };

            turnContext.Activity.ServiceUrl = "urn:BridgeBot:ws://localhost";
            request.SetBody(turnContext.Activity);

            if (_useNamedPipes)
            {
                await _namedPipeClient.SendAsync(request).ConfigureAwait(false);
            }

            if (_useWebSockets)
            {
                await _webSocketClient.SendAsync(request).ConfigureAwait(false);
            }
        }