i am new to C# and I would like to make a bot mixing Luis services and customvision.
I would like the user to be able to send an image or text to the bot.
I am starting from the microsoft bot framework "core bot"* and the imageprocessing bot** (*https://github.com/microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/13.core-bot, **https://github.com/mandardhikari/ImageProcessingBot )
for the moment, i do have this code which is mix between the two samples
#region References
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Schema;
using Newtonsoft.Json;
using Microsoft.Extensions.Configuration;
using System.Net.Http;
using System.IO;
using Microsoft.Azure.CognitiveServices.Vision.ComputerVision.Models;
using Microsoft.Extensions.Logging;
using Microsoft.Recognizers.Text.DataTypes.TimexExpression;
#endregion
namespace ImageProcessingBot
{
public class ImageProcessingBot : IBot
{
private readonly FlightBookingRecognizer _luisRecognizer;
protected readonly ILogger Logger;
private readonly ImageProcessingBotAccessors _accessors;
private readonly IConfiguration _configuration;
private readonly DialogSet _dialogs;
public ImageProcessingBot(ImageProcessingBotAccessors accessors, IConfiguration configuration)
{
_accessors = accessors ?? throw new ArgumentNullException(nameof(accessors));
_configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
_dialogs = new DialogSet(_accessors.ConversationDialogState);
}
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
Activity reply = null;
HeroCard card ;
StringBuilder sb;
switch(turnContext.Activity.Type)
{
case ActivityTypes.ConversationUpdate:
foreach(var member in turnContext.Activity.MembersAdded)
{
if(member.Id != turnContext.Activity.Recipient.Id)
{
reply = await CreateReplyAsync(turnContext, "Welcome. Please select and operation");
await turnContext.SendActivityAsync(reply, cancellationToken:cancellationToken);
}
}
break;
case ActivityTypes.Message:
int attachmentCount = turnContext.Activity.Attachments != null ? turnContext.Activity.Attachments.Count() : 0;
var command = !string.IsNullOrEmpty(turnContext.Activity.Text) ? turnContext.Activity.Text : await _accessors.CommandState.GetAsync(turnContext, () => string.Empty, cancellationToken);
command = command.ToLowerInvariant();
if(attachmentCount == 0)
{
var luisResult = await _luisRecognizer.RecognizeAsync<FlightBooking>(turnContext, cancellationToken);
switch (luisResult.TopIntent().intent)
{
case FlightBooking.Intent.Weather:
// We haven't implemented the GetWeatherDialog so we just display a TODO message.
var getWeatherMessageText = "TODO: get weather flow here";
var getWeatherMessage = MessageFactory.Text(getWeatherMessageText, getWeatherMessageText, InputHints.IgnoringInput);
await turnContext.SendActivityAsync(getWeatherMessage, cancellationToken);
var attachments = new List<Attachment>();
var reply = MessageFactory.Attachment(attachments);
reply.Attachments.Add(Cards.CreateAdaptiveCardAttachment());
default:
// Catch all for unhandled intents
var didntUnderstandMessageText = $"Sorry, I didn't get that. Please try asking in a different way (intent was {luisResult.TopIntent().intent})";
var didntUnderstandMessage = MessageFactory.Text(didntUnderstandMessageText, didntUnderstandMessageText, InputHints.IgnoringInput);
await turnContext.SendActivityAsync(didntUnderstandMessage, cancellationToken);
break;
}
}
else
{
HttpClient client = new HttpClient();
Attachment attachment = turnContext.Activity.Attachments[0];
...// then it stays as in https://github.com/mandardhikari/ImageProcessingBot/blob/master/ImageProcessingBot/ImageProcessingBot/ImageProcessingBot.cs
I am getting these logs.
1>ImageProcessingBot.cs(78,41,78,46): error CS0136: A local or parameter named 'reply' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter
1>ImageProcessingBot.cs(72,33,72,67): error CS0163: Control cannot fall through from one case label ('case FlightBooking.Intent.Weather:') to another
I am new to c#, so any insights on the above would be greatly appreciated!
This is essentially repeating what @stuartd says above in the comments.
The method MessageFactory.Attachemnt
returns an IMessageActivity
so probably best to use a second variable rather than your Activity reply
to avoid casting
case FlightBooking.Intent.Weather:
// We haven't implemented the GetWeatherDialog so we just display a TODO message.
var getWeatherMessageText = "TODO: get weather flow here";
var getWeatherMessage = MessageFactory.Text(getWeatherMessageText, getWeatherMessageText, InputHints.IgnoringInput);
await turnContext.SendActivityAsync(getWeatherMessage, cancellationToken);
var attachments = new List<Attachment>();
meesageReply = MessageFactory.Attachment(attachments); //new variable
messageReply.Attachments.Add(Cards.CreateAdaptiveCardAttachment());
break;