public async Task <AnalysisResponse> ProcessAsync(string input, Message messageOriginator, CancellationToken cancellationToken) { var request = new AnalysisRequest { Text = input }; var analysisResponse = await _artificialIntelligenceExtension.AnalyzeAsync(request, cancellationToken); var bestIntention = analysisResponse .Intentions .OrderByDescending(i => i.Score) .FirstOrDefault(); var bestIntentionAnswer = bestIntention.Answer.Value.ToString(); if (HasLowConfidence(bestIntention)) { await SendCommandToMPAAsync(DontKnowCommand, messageOriginator, cancellationToken); return(analysisResponse); } //Send to MPA await _sender.SendMessageAsync(bestIntentionAnswer, messageOriginator.From, cancellationToken); await Task.Delay(_settings.MPASettings.WaitBetweenSends); await SendCommandToMPAAsync(InitialFlowCommand, messageOriginator, cancellationToken); return(analysisResponse); }
private Lazy <Task <AnalysisResponse> > CreateLazyAnalyzedContent(Identity userIdentity) => new Lazy <Task <AnalysisResponse> >(async() => { // Only analyze the input if the type is plain text or analyzable metadata is true. if (!_analyzable.Value && Content.GetMediaType() != PlainText.MediaType) { return(null); } try { return(await _artificialIntelligenceExtension.AnalyzeAsync( new AnalysisRequest { Text = _lazySerializedContent.Value, Extras = new Dictionary <string, string> { ["MessageId"] = Message.Id, ["UserIdentity"] = userIdentity.ToString() } }, _cancellationToken)); } catch (LimeException) { return(null); } });
public LazyInput( Message message, Identity userIdentity, BuilderConfiguration builderConfiguration, IDocumentSerializer documentSerializer, IEnvelopeSerializer envelopeSerializer, IArtificialIntelligenceExtension artificialIntelligenceExtension, CancellationToken cancellationToken) { Message = message ?? throw new ArgumentNullException(nameof(message)); _builderConfiguration = builderConfiguration ?? throw new ArgumentNullException(nameof(builderConfiguration)); _lazySerializedContent = new Lazy <string>(() => documentSerializer.Serialize(Content)); _analyzable = new Lazy <bool>(() => { string result = null; Message?.Metadata?.TryGetValue("builder.analyzable", out result); return(result?.ToLower() == "true"); }); _lazyAnalyzedContent = new Lazy <Task <AnalysisResponse> >(async() => { // Only analyze the input if the type is plain text or analyzable metadata is true. if (!_analyzable.Value && Content.GetMediaType() != PlainText.MediaType) { return(null); } try { return(await artificialIntelligenceExtension.AnalyzeAsync( new AnalysisRequest { Text = _lazySerializedContent.Value, Extras = new Dictionary <string, string> { ["MessageId"] = Message.Id, ["UserIdentity"] = userIdentity.ToString() } }, cancellationToken)); } catch (LimeException) { return(null); } }); _lazySerializedMessage = new Lazy <string>(() => { if (Message != null) { return(envelopeSerializer.Serialize(Message)); } return(null); }); }
public LazyInput( Document content, IDictionary <string, string> flowConfiguration, IDocumentSerializer documentSerializer, IEnvelopeSerializer envelopeSerializer, IArtificialIntelligenceExtension artificialIntelligenceExtension, CancellationToken cancellationToken) { _flowConfiguration = flowConfiguration; Content = content; _lazySerializedContent = new Lazy <string>(() => documentSerializer.Serialize(content)); _lazyAnalyzedContent = new Lazy <Task <AnalysisResponse> >(async() => { // Only analyze the input if the type is plain text. if (Content.GetMediaType() != PlainText.MediaType) { return(null); } try { return(await artificialIntelligenceExtension.AnalyzeAsync( new AnalysisRequest { Text = _lazySerializedContent.Value }, cancellationToken)); } catch (LimeException) { return(null); } }); _lazySerializedMessage = new Lazy <string>(() => { var message = EnvelopeReceiverContext <Message> .Envelope; if (message != null) { return(envelopeSerializer.Serialize(message)); } return(null); }); }
public LazyInput( Message message, BuilderConfiguration builderConfiguration, IDocumentSerializer documentSerializer, IEnvelopeSerializer envelopeSerializer, IArtificialIntelligenceExtension artificialIntelligenceExtension, CancellationToken cancellationToken) { Message = message ?? throw new ArgumentNullException(nameof(message)); _builderConfiguration = builderConfiguration ?? throw new ArgumentNullException(nameof(builderConfiguration)); _lazySerializedContent = new Lazy <string>(() => documentSerializer.Serialize(Content)); _lazyAnalyzedContent = new Lazy <Task <AnalysisResponse> >(async() => { // Only analyze the input if the type is plain text. if (Content.GetMediaType() != PlainText.MediaType) { return(null); } try { return(await artificialIntelligenceExtension.AnalyzeAsync( new AnalysisRequest { Text = _lazySerializedContent.Value }, cancellationToken)); } catch (LimeException) { return(null); } }); _lazySerializedMessage = new Lazy <string>(() => { if (Message != null) { return(envelopeSerializer.Serialize(Message)); } return(null); }); }
public async Task ReceiveAsync(Message message, CancellationToken cancellationToken) { var currentState = await _stateManager.GetStateAsync(message.From.ToIdentity(), cancellationToken); var receivedText = (message.Content as PlainText).Text; var result = await _artificialIntelligenceExtension.AnalyzeAsync(new AnalysisRequest { Text = receivedText }); var bestIntention = result.Intentions[0]; var actionsQuickReply = new Select { Text = "Oii 😄, em que posso te ajudar ? 👇", Scope = SelectScope.Immediate, Options = new SelectOption[] { new SelectOption { Text = "📍 Postos próximos", Value = new Trigger { StateId = "3.1.0" } }, new SelectOption { Text = "⭐ Meus favoritos", Value = new Trigger { StateId = "3.2.0" } } } }; var notHandledText = new PlainText { Text = "Não entendi 😶 ainda estou aprendendo" }; if (bestIntention.Score < 0.3) { //Not handled await _sender.SendMessageAsync(notHandledText, message.From, cancellationToken); await Task.Delay(2000); actionsQuickReply.Text = "Mas eu já sei falar sobre isso:"; await _sender.SendMessageAsync(actionsQuickReply, message.From, cancellationToken); return; } switch (bestIntention.Name) { case "Salutation": await _sender.SendMessageAsync(actionsQuickReply, message.From, cancellationToken); break; case "Help": actionsQuickReply.Text = "Eu sou o bot Gasosa Barata, que te ajuda a encontrar o combustível mais em conta perto de você! O que deseja ? 👇"; await _sender.SendMessageAsync(actionsQuickReply, message.From, cancellationToken); break; case "SearchFor": actionsQuickReply.Text = "Quais postos deseja consultar ? 👇"; await _sender.SendMessageAsync(actionsQuickReply, message.From, cancellationToken); break; default: //Not handled await _sender.SendMessageAsync(notHandledText, message.From, cancellationToken); break; } }