public async Task FetchInboxAsync(IAgentContext agentContext) { var connection = await GetMediatorConnectionAsync(agentContext); if (connection == null) { throw new InvalidOperationException("This agent is not configured with a mediator"); } var createInboxMessage = new GetInboxItemsMessage(); var response = await messageService.SendReceiveAsync <GetInboxItemsResponseMessage>(agentContext.Wallet, createInboxMessage, connection); var processedItems = new List <string>(); foreach (var item in response.Items) { try { await agentContext.Agent.ProcessAsync(agentContext, new PackedMessageContext(item.Data)); processedItems.Add(item.Id); } catch (Exception e) { Console.WriteLine($"Error processing message {e}"); } } await messageService.SendAsync(agentContext.Wallet, new DeleteInboxItemsMessage { InboxItemIds = processedItems }, connection); }
public virtual async Task <(int, IEnumerable <InboxItemMessage>)> FetchInboxAsync(IAgentContext agentContext) { var connection = await GetMediatorConnectionAsync(agentContext); if (connection == null) { throw new InvalidOperationException("This agent is not configured with a mediator"); } var createInboxMessage = new GetInboxItemsMessage(); var response = await messageService.SendReceiveAsync <GetInboxItemsResponseMessage>(agentContext, createInboxMessage, connection); var processedItems = new List <string>(); var unprocessedItem = new List <InboxItemMessage>(); foreach (var item in response.Items) { try { await agentContext.Agent.ProcessAsync(agentContext, new PackedMessageContext(item.Data)); processedItems.Add(item.Id); } catch (AriesFrameworkException e) when(e.ErrorCode == ErrorCode.InvalidMessage) { processedItems.Add(item.Id); } catch (Exception) { unprocessedItem.Add(item); } } if (processedItems.Any()) { await messageService.SendAsync(agentContext, new DeleteInboxItemsMessage { InboxItemIds = processedItems }, connection); } return(processedItems.Count, unprocessedItem); }