Created
October 3, 2017 15:07
-
-
Save ravpacheco/8cd0e06460958a55f91d2c7147f61363 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Lime.Protocol; | |
using System.Diagnostics; | |
using Take.Blip.Client; | |
using Take.Blip.Client.Extensions.Bucket; | |
using Lime.Messaging.Contents; | |
using Take.Blip.Client.Extensions.EventTracker; | |
using Takenet.Iris.Messaging.Contents; | |
using Take.Blip.Client.Extensions.ArtificialIntelligence; | |
using Takenet.Iris.Messaging.Resources.ArtificialIntelligence; | |
namespace test2 | |
{ | |
/// <summary> | |
/// Defines a class for handling messages. | |
/// This type must be registered in the application.json file in the 'messageReceivers' section. | |
/// </summary> | |
public class PlainTextMessageReceiver : IMessageReceiver | |
{ | |
private readonly ISender _sender; | |
private readonly Settings _settings; | |
private readonly IBucketExtension _bucketExtension; | |
private readonly IEventTrackExtension _eventTrackExtension; | |
private readonly IArtificialIntelligenceExtension _artificialIntelligenceExtension; | |
public PlainTextMessageReceiver(ISender sender, | |
Settings settings, | |
IBucketExtension bucketExtension, | |
IEventTrackExtension eventTrackExtension, | |
IArtificialIntelligenceExtension artificialIntelligenceExtension) | |
{ | |
_sender = sender; | |
_settings = settings; | |
_bucketExtension = bucketExtension; | |
_eventTrackExtension = eventTrackExtension; | |
_artificialIntelligenceExtension = artificialIntelligenceExtension; | |
} | |
public async Task ReceiveAsync(Message message, CancellationToken cancellationToken) | |
{ | |
Trace.TraceInformation($"From: {message.From} \tContent: {message.Content}"); | |
var text = (message.Content as PlainText).Text; | |
var response = await _artificialIntelligenceExtension.AnalyzeAsync(new AnalysisRequest { Text = text }); | |
var bestIntention = response.Intentions[0]; | |
if (bestIntention.Name.Equals("Pesquisa")) | |
{ | |
var menuDocument = new Select | |
{ | |
Text = "Olá, tudo bem ? Deseja participar da nossa pesquisa ?", | |
Options = new SelectOption[] | |
{ | |
new SelectOption{ | |
Text = "Sim", | |
Value = "S" | |
}, | |
new SelectOption{ | |
Text = "Não", | |
Value = "N" | |
}, | |
} | |
}; | |
var session = new JsonDocument(); | |
session["state"] = "S1"; | |
await _bucketExtension.SetAsync<JsonDocument>(message.From, session); | |
await _sender.SendMessageAsync(menuDocument, message.From, cancellationToken); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment