Ejemplo n.º 1
0
        public async Task RefreshTransactions()
        {
            RefreshingTransactions = true;

            var context = await _agentContextProvider.GetContextAsync();

            var message = _discoveryService.CreateQuery(context, "*");

            DiscoveryDiscloseMessage protocols = null;

            try
            {
                var response = await _messageService.SendAsync(context.Wallet, message, _record, null, true);

                protocols = response.GetMessage <DiscoveryDiscloseMessage>();
            }
            catch (Exception)
            {
                //Swallow exception
                //TODO more granular error protection
            }

            IList <TransactionItem> transactions = new List <TransactionItem>();

            Transactions.Clear();

            if (protocols == null)
            {
                HasTransactions        = false;
                RefreshingTransactions = false;
                return;
            }

            foreach (var protocol in protocols.Protocols)
            {
                switch (protocol.ProtocolId)
                {
                case MessageTypes.TrustPingMessageType:
                    transactions.Add(new TransactionItem()
                    {
                        Title                = "Trust Ping",
                        Subtitle             = "Version 1.0",
                        PrimaryActionTitle   = "Ping",
                        PrimaryActionCommand = new Command(async() =>
                        {
                            await PingConnectionAsync();
                        }, () => true),
                        Type = TransactionItemType.Action.ToString("G")
                    });
                    break;
                }
            }

            Transactions.InsertRange(transactions);
            HasTransactions = transactions.Any();

            RefreshingTransactions = false;
        }
        public async Task <IActionResult> Index()
        {
            var context = await _agentContextProvider.GetContextAsync();

            return(View(new ConnectionsViewModel
            {
                Connections = await _connectionService.ListAsync(context)
            }));
        }
Ejemplo n.º 3
0
        /// <summary>Called by the ASPNET Core runtime</summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        /// <exception cref="Exception">Empty content length</exception>
        public async Task InvokeAsync(HttpContext context)
        {
            if (!context.Request.Method.Equals("POST", StringComparison.OrdinalIgnoreCase))
            {
                await _next(context);

                return;
            }

            if (context.Request.ContentLength == null)
            {
                throw new Exception("Empty content length");
            }

            using (var stream = new StreamReader(context.Request.Body))
            {
                var body = await stream.ReadToEndAsync();

                var result = await ProcessAsync(
                    context : await _contextProvider.GetContextAsync(), //TODO assumes all recieved messages are packed
                    messageContext : new MessageContext(body.GetUTF8Bytes(), true));

                context.Response.StatusCode = 200;

                if (result != null)
                {
                    context.Response.ContentType = DefaultMessageService.AgentWireMessageMimeType;
                    await context.Response.Body.WriteAsync(result, 0, result.Length);
                }
                else
                {
                    await context.Response.WriteAsync(string.Empty);
                }
            }
        }
Ejemplo n.º 4
0
        private Action <ServiceMessageProcessingEvent> CreateCallback(BotAdapter adapter, ConversationReference conversationReference,
                                                                      string agentId, string connectionId)
        {
            return(async message =>
            {
                var agentContext = await _agentContextProvider.GetContextAsync(agentId);

                var connection = await _connectionService.GetAsync(agentContext, connectionId);

                await adapter.ContinueConversationAsync(AppId, conversationReference, async (turnContext, cancellationToken) =>
                {
                    await turnContext.SendActivityAsync($"You are now connected to {connection.Alias?.Name ?? "[unspecified]"}");
                }, CancellationToken.None);
            });
        }
Ejemplo n.º 5
0
        private async Task <DialogTurnResult> AcceptInvitationAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            if (Uri.TryCreate(stepContext.Options?.ToString(), UriKind.Absolute, out var invitationUri))
            {
                await stepContext.Context.SendTypingAsync();

                string invitationDecoded = null;
                var    query             = QueryHelpers.ParseQuery(invitationUri.Query);
                if (query.TryGetValue("c_i", out var invitationEncoded))
                {
                    invitationDecoded = Uri.UnescapeDataString(invitationEncoded);
                }

                var json = Encoding.UTF8.GetString(Convert.FromBase64String(invitationDecoded));
                var jobj = JObject.Parse(json);

                switch (jobj["@type"].Value <string>())
                {
                case MessageTypes.ConnectionInvitation:
                {
                    var state = await _accessors.AgentState.GetAsync(stepContext.Context, () => new AgentState(), cancellationToken);

                    if (state.WalletId != null)
                    {
                        var invitation   = JsonConvert.DeserializeObject <ConnectionInvitationMessage>(json);
                        var agentContext = await _contextProvider.GetContextAsync(state.WalletId);

                        var(message, record) = await _connectionService.CreateRequestAsync(agentContext, invitation);

                        await _messageService.SendToConnectionAsync(agentContext.Wallet, message, record,
                                                                    invitation.RecipientKeys.First());

                        await stepContext.Context.SendActivityAsync("I accepted the invitation and initiated connection");

                        return(await stepContext.NextAsync(record.Id));
                    }
                    break;
                }
                }
            }
            // End the dialog and notify the flow hasn't been successful for whatever reason
            return(await stepContext.EndDialogAsync(false));
        }
Ejemplo n.º 6
0
        private async Task <DialogTurnResult> CreateInvitationAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
        {
            var state = await _accessors.AgentState.GetAsync(stepContext.Context, () => new AgentState(), cancellationToken);

            if (state.WalletId == null)
            {
                await stepContext.Context.SendActivityAsync("Sorry, I couldn't find information about your agent");

                return(await stepContext.EndDialogAsync());
            }
            await stepContext.Context.SendTypingAsync();

            var context = await _contextProvider.GetContextAsync(state.WalletId);

            var provisioning = await _provisioningService.GetProvisioningAsync(context.Wallet);

            var(invitation, record) = await _connectionService.CreateInvitationAsync(context,
                                                                                     new InviteConfiguration { AutoAcceptConnection = true });

            var invitationDetails = $"{provisioning.Endpoint.Uri}?c_i={invitation.ToByteArray().ToBase64String()}";

            var reply = stepContext.Context.Activity.CreateReply("Here are the invitation details");

            reply.Attachments = new List <Attachment>
            {
                new HeroCard
                {
                    Images = new List <CardImage>
                    {
                        new CardImage {
                            Url = $"https://chart.googleapis.com/chart?cht=qr&chs=300x300&chld=M|0&chl={Uri.EscapeDataString(invitationDetails)}"
                        }
                    }
                }.ToAttachment()
            };

            await stepContext.Context.SendActivityAsync(reply);

            await stepContext.Context.SendActivityAsync(invitationDetails);

            return(await stepContext.NextAsync(record.Id));
        }
Ejemplo n.º 7
0
        public async Task InvokeAsync(HttpContext context)
        {
            if (!context.Request.Method.Equals("POST", StringComparison.OrdinalIgnoreCase))
            {
                await _next(context);

                return;
            }

            if (context.Request.ContentLength == null)
            {
                throw new Exception("Empty content length");
            }

            using (var stream = new StreamReader(context.Request.Body))
            {
                var body = await stream.ReadToEndAsync();

                var agentId = context.Request.Path.Value.Replace("/", string.Empty);

                var result = await ProcessAsync(
                    body : body.GetUTF8Bytes(),
                    context : await _contextProvider.GetContextAsync(agentId));

                context.Response.StatusCode = 200;

                if (result != null)
                {
                    await context.Response.Body.WriteAsync(result, 0, result.Length);
                }
                else
                {
                    await context.Response.WriteAsync(string.Empty);
                }
            }
        }