public async Task ReceiveActivity(IBotContext context, MiddlewareSet.NextDelegate next) { if (HasImageAttachment(context)) { byte[] imageBuffer = await GetImageFromMessageAsync(context); bool isUploaded = await UploadToBlob(context, imageBuffer); Vision result = await MakeAnalysisRequest(imageBuffer, "d44d077e88e24f1ab5dbda8c0794455a"); var entity = new LuisEntity() { Value = result.description.captions.FirstOrDefault().text, Type = "Description" }; var topIntent = new Intent { Name = "intent.image", Score = 1.0 }; topIntent.Entities.Add(entity); context.TopIntent = topIntent; } await next().ConfigureAwait(false); }
/// <summary> /// Processess an incoming activity. /// </summary> /// <param name="context">The context object for this turn.</param> /// <param name="next">The delegate to call to continue the bot middleware pipeline.</param> public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next) { BotAssert.ContextNotNull(context); if (context.Activity.Type == ActivityTypes.Message) { var utterance = context.Activity.AsMessageActivity().Text; if (!string.IsNullOrWhiteSpace(utterance)) { var result = await _luisRecognizer.Recognize(utterance, CancellationToken.None).ConfigureAwait(false); context.Services.Add(LuisRecognizerResultKey, result); var traceInfo = new LuisTraceInfo { RecognizerResult = result, LuisModel = RemoveSensitiveData(_luisModel), LuisOptions = _luisOptions, LuisResult = (LuisResult)result.Properties["luisResult"] }; var traceActivity = Activity.CreateTraceActivity("LuisRecognizerMiddleware", LuisTraceType, traceInfo, LuisTraceLabel); await context.SendActivity(traceActivity).ConfigureAwait(false); } } await next().ConfigureAwait(false); }
/// <summary> /// Incoming activity /// </summary> /// <param name="context"></param> /// <param name="token"></param> /// <returns></returns> public async Task ReceiveActivity(IBotContext context, MiddlewareSet.NextDelegate next) { IMessageActivity message = context.Request.AsMessageActivity(); if (message != null) { if (!String.IsNullOrWhiteSpace(message.Text)) { // determine the language we are using for this conversation var sourceLanguage = "en"; // context.Conversation.Data["Language"]?.ToString() ?? this.nativeLanguages.FirstOrDefault() ?? "en"; var translationContext = new TranslationContext { SourceText = message.Text, SourceLanguage = sourceLanguage, TargetLanguage = (this.nativeLanguages.Contains(sourceLanguage)) ? sourceLanguage : this.nativeLanguages.FirstOrDefault() ?? "en" }; context.Set(translationContext); // translate to bots language if (translationContext.SourceLanguage != translationContext.TargetLanguage) { await TranslateMessageAsync(context, message, translationContext.SourceLanguage, translationContext.TargetLanguage).ConfigureAwait(false); } } } await next().ConfigureAwait(false); }
public async Task ContextCreated(IBotContext context, MiddlewareSet.NextDelegate next) { Write($"BEFORE ContextCreated"); await next(); Write($"AFTER ContextCreated"); }
public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next) { Activity activity = context.Activity; if (activity.Type is ActivityTypes.Message) { bool.TryParse( Configuration[KeyRejectConnectionRequestIfNoAggregationChannel], out bool rejectConnectionRequestIfNoAggregationChannel); // Store the conversation references (identities of the sender and the recipient [bot]) // in the activity MessageRouter.StoreConversationReferences(activity); AbstractMessageRouterResult messageRouterResult = null; // Check the activity for commands if (await CommandHandler.HandleCommandAsync(context) == false) { // No command detected/handled // Let the message router route the activity, if the sender is connected with // another user/bot messageRouterResult = await MessageRouter.RouteMessageIfSenderIsConnectedAsync(activity); if (messageRouterResult is MessageRoutingResult && (messageRouterResult as MessageRoutingResult).Type == MessageRoutingResultType.NoActionTaken) { // No action was taken by the message router. This means that the user // is not connected (in a 1:1 conversation) with a human // (e.g. customer service agent) yet. // Check for cry for help (agent assistance) if (!string.IsNullOrWhiteSpace(activity.Text) && activity.Text.ToLower().Contains("human")) { // Create a connection request on behalf of the sender // Note that the returned result must be handled messageRouterResult = MessageRouter.CreateConnectionRequest( MessageRouter.CreateSenderConversationReference(activity), rejectConnectionRequestIfNoAggregationChannel); } else { // No action taken - this middleware did not consume the activity so let it propagate await next().ConfigureAwait(false); } } } // Uncomment to see the result in a reply (may be useful for debugging) //if (messageRouterResult != null) //{ // await MessageRouter.ReplyToActivityAsync(activity, messageRouterResult.ToString()); //} // Handle the result, if necessary await MessageRouterResultHandler.HandleResultAsync(messageRouterResult); } }
public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next) { if (context.Activity.Type == ActivityTypes.Message) { var messageActivity = context.Activity.AsMessageActivity(); if (!string.IsNullOrEmpty(messageActivity.Text)) { var results = await this.personalityChatService.QueryServiceAsync(messageActivity.Text.Trim()).ConfigureAwait(false); if (!this.personalityChatMiddlewareOptions.RespondOnlyIfChat || results.IsChatQuery) { string personalityChatResponse = this.GetResponse(results); await this.PostPersonalityChatResponseToUser(context, next, personalityChatResponse); } } } if (this.personalityChatMiddlewareOptions.EndActivityRoutingOnResponse) { // Query is answered, don't keep routing return; } await next().ConfigureAwait(false); }
public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next) { if (context.Activity.Type == ActivityTypes.Message && context.Activity.Attachments?.Count > 0) { var http = new HttpClient(); var sa = new CloudStorageAccount(new StorageCredentials(StorageAccountName, StorageAccountKey), true); var ccli = sa.CreateCloudBlobClient(); var ctr = ccli.GetContainerReference(StorageAccountContainer); await ctr.CreateIfNotExistsAsync(); foreach (var x in context.Activity.Attachments) { var str = await http.GetStreamAsync(x.ContentUrl); var f = ctr.GetBlockBlobReference(AppendGuid ? $"{Guid.NewGuid()}_{x.Name}" : x.Name); await f.UploadFromStreamAsync(str); if (ReplaceUri) { x.ContentUrl = f.StorageUri.PrimaryUri.AbsoluteUri; } } } await next(); }
public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next) { if (context.Activity.Type is ActivityTypes.Message) { await context.SendActivity("parrot middleware"); } }
public async Task ReceiveActivity(IBotContext context, MiddlewareSet.NextDelegate next) { context.Reply("BEFORE"); await next(); context.Reply("AFTER"); }
private async Task BotReceiveHandler(IBotContext context, MiddlewareSet.NextDelegate next) { if (context.Request.Type == ActivityTypes.Message) { context.Reply($"echo: {context.Request.AsMessageActivity().Text}"); } }
// We log each message in a consistent way within Application Insights along with LUIS intent information in order // to provide a consistent set of insights across all Bots using a baseline PowerBI dashboard. // This middleware comoponent always passes on to the next in the pipeline public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next) { TelemetryHelper telemetryHelper = new TelemetryHelper(appInsightsKey, textAnalyticsKey, textAnalyticsEndpoint, logOriginalMessages, logUserName, logSentimentAndKeyPhrases, sentimentWordThreshold); try { // We create an Event for each message coming in (regardless of LUIS invocation) telemetryHelper.LogIncomingMessage(context); // Protect against LUIS not having been called var luisResult = context.Services.Get <RecognizerResult> (LuisRecognizerMiddleware.LuisRecognizerResultKey); if (luisResult != null) { // We also log a seperate intent to store the LUIS results telemetryHelper.LogIntent(context); } } catch (Exception e) { telemetryHelper.TrackException(e); } // On to the next component await next(); }
private async Task BotReceiveHandler(IBotContext context, MiddlewareSet.NextDelegate next) { // --- Bot logic bool handled = false; // Get the current ActiveTopic from my conversation state var activeTopic = context.State.Conversation[ConversationProperties.ACTIVETOPIC] as ITopic; // if there isn't one if (activeTopic == null) { // use default topic activeTopic = new DefaultTopic(); context.State.Conversation[ConversationProperties.ACTIVETOPIC] = activeTopic; handled = await activeTopic.StartTopic(context); } else { // continue to use the active topic handled = await activeTopic.ContinueTopic(context); } // AlarmBot only needs to transition from defaultTopic -> subTopic and back, so // if activeTopic's result is false and the activeToic is NOT the default topic, we switch back to default topic if (handled == false && !(context.State.Conversation[ConversationProperties.ACTIVETOPIC] is DefaultTopic)) { // resume default topic activeTopic = new DefaultTopic(); context.State.Conversation[ConversationProperties.ACTIVETOPIC] = activeTopic; handled = await activeTopic.ResumeTopic(context); } await next(); }
public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next) { Fired = false; if (context.Activity.Text != null) { var w = context.Activity.Text.Split(' ', ',', '-', '.', '!', '(', ')').Select(x => x.Trim()) .Where(x => x.Length > 0); foreach (var x in ActionClass.GetType().GetMethods()) { var A = x.GetCustomAttributes(typeof(KeywordAttribute), false); if (A != null) { foreach (var t in A) { if (w.Contains(((KeywordAttribute)t).Text)) { // UserState<KGBState>.Get(context).AV.Add(((KeywordAttribute)t).Text, "1"); await(Task) x.Invoke(ActionClass, new object[] { context }); Fired = true; } } } } } await next(); }
private async Task HandleMessage(ITurnContext context, string messageText, MiddlewareSet.NextDelegate next) { if (HandlerByBestMatchLists == null) { HandlerByBestMatchLists = new Dictionary <BestMatchAttribute, BestMatchHandler>(GetHandlersByBestMatchLists()); } BestMatchHandler handler = null; double bestMatchedScore = 0; foreach (var handlerByBestMatchList in HandlerByBestMatchLists) { var match = FindBestMatch(handlerByBestMatchList.Key.BestMatchList, messageText, handlerByBestMatchList.Key.Threshold, handlerByBestMatchList.Key.IgnoreCase, handlerByBestMatchList.Key.IgnoreNonAlphanumericCharacters); if (match?.Score > bestMatchedScore) { bestMatchedScore = match.Score; handler = handlerByBestMatchList.Value; } } await(handler ?? NoMatchHandler).Invoke(context, messageText, next); }
/// <inheritdoc /> /// <summary> /// Intercepts each turn to determine whether the source of the message is from the bespoke DirectLine based NotifyConnector. /// Pushes the outgoing message on to a queue if so. /// </summary> /// <param name="context">The turn context</param> /// <param name="next">the next OnTurn operation in the pipeline</param> /// <returns>The <see cref="T:System.Threading.Tasks.Task" /></returns> public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next) { if (context.Activity.Type == ActivityTypes.Message) { // Create a send activity handler to grab all response activities // from the activity list. context.OnSendActivities( async(activityContext, activityList, activityNext) => { dynamic channelData = context.Activity.ChannelData; if (channelData?.NotifyMessage == null) { return(await activityNext()); } foreach (Activity activity in activityList) { if (activity.Type != ActivityTypes.Message || !activity.HasContent()) { continue; } await this.RelayMessage(context, activity); } return(await activityNext()); }); } // Pass execution on to the next layer in the pipeline. await next(); }
public async Task OnProcessRequest(ITurnContext context, MiddlewareSet.NextDelegate next) { if (context.Activity.Type == ActivityTypes.Message) { var messageActivity = context.Activity.AsMessageActivity(); if (!string.IsNullOrEmpty(messageActivity.Text)) { var results = await _qnaMaker.GetAnswers(messageActivity.Text.Trim()).ConfigureAwait(false); if (results.Any()) { if (!string.IsNullOrEmpty(_options.DefaultAnswerPrefixMessage)) { await context.SendActivity(_options.DefaultAnswerPrefixMessage); } await context.SendActivity(results.First().Answer); if (_options.EndActivityRoutingOnAnswer) { //Question is answered, don't keep routing return; } } } } await next().ConfigureAwait(false); }
public async Task OnProcessRequest(IBotContext context, MiddlewareSet.NextDelegate next) { await context.SendActivity("I'm QnAMakerMiddleware"); if (context.Request.Type == ActivityTypes.Message) { var messageActivity = context.Request.AsMessageActivity(); if (!string.IsNullOrEmpty(messageActivity.Text)) { var results = await qnAMaker.GetAnswers(messageActivity.Text.Trim()); if (results.Any()) { if (!string.IsNullOrEmpty(qaOptions.DefaultAnswerPrefixMessage)) { await context.SendActivity(qaOptions.DefaultAnswerPrefixMessage); } await context.SendActivity(results.First().Answer); if (qaOptions.EndActivityRoutingOnAnswer) { return; } } } } await next(); }
/// <summary> /// Incoming activity /// </summary> /// <param name="context"></param> /// <param name="next"></param> /// <returns></returns> public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next) { if (context.Activity.Type == ActivityTypes.Message) { IMessageActivity message = context.Activity.AsMessageActivity(); if (message != null) { if (!String.IsNullOrWhiteSpace(message.Text)) { bool localeChanged = await _setUserLocale(context); if (!localeChanged) { string fromLocale = _getUserLocale(context); ConvertLocaleMessage(context, fromLocale); } else { // skip routing in case of user changed the locale return; } } } } await next().ConfigureAwait(false); }
public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next) { if (context.Activity.Type is ActivityTypes.Message) { if (string.IsNullOrEmpty(context.Activity.Text)) { context.Services.Add <string>("0.0"); } // Create a client var client = new TextAnalyticsAPI(new ApiKeyServiceClientCredentials(ApiKey)); client.AzureRegion = AzureRegions.Westeurope; // Extract the language var result = await client.DetectLanguageAsync(new BatchInput(new List <Input>() { new Input("1", context.Activity.Text) })); var language = result.Documents?[0].DetectedLanguages?[0].Name; // Get the sentiment var sentimentResult = await client.SentimentAsync( new MultiLanguageBatchInput( new List <MultiLanguageInput>() { new MultiLanguageInput("en", "0", context.Activity.Text), })); context.Services.Add <string>(sentimentResult.Documents?[0].Score?.ToString("#.#")); } await next(); }
public async Task ReceiveActivity(IBotContext context, MiddlewareSet.NextDelegate next) { Write($"BEFORE ReceiveActivity {PrettyPrint(context.Request)}"); await next(); Write($"AFTER ReceiveActivity {PrettyPrint(context.Request)}"); }
public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next) { if (context.Activity.Type == ActivityTypes.Message) { await HandleMessage(context, context.Activity.Text, next); } }
public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next) { Fired = false; var q = System.Web.HttpUtility.UrlEncode(context.Activity.Text); var http = new HttpClient(); http.DefaultRequestHeaders.Add("Authorization", "Bearer " + ApiKey); string res; res = await http.GetStringAsync("https://api.dialogflow.com/v1/query?sessionId=1&lang=ru&v=20150910&query=" + q); dynamic jres = Newtonsoft.Json.JsonConvert.DeserializeObject(res); if (ActionClass != null) { foreach (var x in ActionClass.GetType().GetMethods()) { var A = x.GetCustomAttributes(typeof(EntityAttribute), false); if (A != null) { foreach (var t in A) { var val = jres.result.parameters[((EntityAttribute)t).EntityName]; if (val != null) { await(Task) x.Invoke(ActionClass, new object[] { context, val }); } } } } } await next(); }
public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next) { await context.SendActivity(context.Activity.CreateReply("BEFORE")); await next(); await context.SendActivity(context.Activity.CreateReply("AFTER")); }
public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next) { await context.SendActivity($"[SimpleMiddleware1] {context.Activity.Type}/OnTurn/Before"); await next(); await context.SendActivity($"[SimpleMiddleware1] {context.Activity.Type}/OnTurn/After"); }
public async Task ReceiveActivity(IBotContext context, MiddlewareSet.NextDelegate next) { var response = ((Activity)context.Request).CreateReply(); response.Text = context.Request.AsMessageActivity().Text; context.Responses.Add(response); await next(); }
private Task BotReceiveHandler(IBotContext context, MiddlewareSet.NextDelegate next) { if (context.Request.Type == ActivityTypes.Message) { context.Reply("hello"); } return(Task.CompletedTask); }
public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next) { var text = context.Activity.Text; var fromLanguage = this.options.From; var toLanguage = this.options.To; context.Activity.Text = await translate(text, fromLanguage, toLanguage); await next().ConfigureAwait(false); }
private Task BotReceiveHandler(IBotContext context, MiddlewareSet.NextDelegate next) { if (context.Request.Type == ActivityTypes.Message && context.Responses.Count == 0) { // add app logic when QnA Maker doesn't find an answer context.Reply("No good match found in the KB."); } return(Task.CompletedTask); }
public async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next) { if (context.Activity.Type is ActivityTypes.Message) { context.Services.Add <string>(await context.Activity.Text.Sentiment(ApiKey)); } await next(); }
public override async Task OnTurn(ITurnContext context, MiddlewareSet.NextDelegate next) { // alter the original utterance before translation. if (context.Activity.Text == "mañana") { context.Activity.Text = "para mañana"; } await base.OnTurn(context, next); }