Reputation: 2827
For an azure chatbot I want it to ask me a simple question after its answer, so I can for example give feedback in return.
I am using the HeroCard
class.
dialog
private async Task ShowWeatherResult(IDialogContext context, LuisResult result)
{
bool found = false;
foreach (var entity in result.Entities)
{
if (entity.Type.Equals(Entity_Location))
{
WeatherAPI weather = new WeatherAPI(entity.Entity);
found = true;
await context.PostAsync(weather.ForecastReport());
await Task.Delay(500);
// ask for happiness
Attachment attachment = new Attachment()
{
ContentType = HeroCard.ContentType,
Content = CardsBuilder.CreateHappinessCard()
};
var reply = context.MakeMessage();
reply.Attachments.Add(attachment);
await context.PostAsync(reply, CancellationToken.None);
context.Wait(MessageReceivedAsync);
}
}
if (!found)
{
await context.PostAsync($"I don't speak human fluently, try another question asking for a specific city!");
context.Wait(MessageReceived);
}
}
public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
if (message.Text != null)
{
//
var happiness = new HappinessAPI();
// Got an Action Submit
string value = message.Text;
//string submitType = value.Type.ToString();
switch (value)
{
case "ShowGif":
await context.PostAsync(happiness.ShowGif(context), CancellationToken.None);
await Task.Delay(500);
break;
case "HappinessSearch":
await context.PostAsync(happiness.GetJoke(context), CancellationToken.None);
await Task.Delay(500);
break;
default:
break;
}
}
context.Wait(MessageReceived);
}
HerdoCard
internal static HeroCard CreateHappinessCard()
{
HeroCard card = new HeroCard()
{
Title = "Hi!",
Text = "Are you happy?",
Buttons = new List<CardAction>()
{
new CardAction()
{
Title = "Yes",
Text = "Yes",
DisplayText = "Yes",
Type = ActionTypes.PostBack,
Value = "ShowGif"
},
new CardAction()
{
Title = "Meh...",
Text ="No",
DisplayText = "Meh...",
Type = ActionTypes.PostBack,
Value = "HappinessSearch"
}
}
};
return card;
}
happinessapi
public class HappinessAPI
{
internal IMessageActivity ShowGif(IDialogContext context)
{
Attachment attachment = new Attachment()
{
ContentType = HeroCard.ContentType,
Content = new HeroCard()
{
Images = new List<CardImage>()
{
new CardImage("https://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/220px-Smiley.svg.png")
}
}
};
var reply = context.MakeMessage();
reply.Attachments.Add(attachment);
return reply;
}
internal IMessageActivity GetJoke(IDialogContext context)
{
var request = WebRequest.Create("http://api.icndb.com/jokes/random");
request.ContentType = "application/json; charset=utf-8";
string text;
var response = (HttpWebResponse)request.GetResponse();
using (var sr = new StreamReader(response.GetResponseStream()))
{
text = sr.ReadToEnd();
}
var reply = context.MakeMessage();
reply.Text = (string)(JObject.Parse(text))["value"]["joke"];
return reply;
}
}
Thing is, it works while testing using the WebChat in AzurePortal but the responseback to its questions doesn't in microsoft teams.
Sample:
Works in Webchat:
Me: weather in Frankfurt
Bot: "It's cold.... whatever"
Bot: Are you happy?
Me: click "yes/no"
Doesn't work in Microsoft Teams
Everything is ok until I click "yes/no", then it just tris to do something(the "is typing..." appears but after that, nothing happens.
EDIT
I see while using the Chatbot in Microsoft Teams when I click the herocard a message is written in the chat, when it shouldn't, because it was set to ActionTypes.Postback
EDIT 2
The HeroCard now looks like this:
internal static HeroCard CreateHappinessCard()
{
HeroCard card = new HeroCard()
{
Title = "Hi!",
Text = "Are you happy?",
Buttons = new List<CardAction>()
{
new CardAction()
{
Title = "Yes",
Text = "ShowGif",
//DisplayText = null,
Type = ActionTypes.MessageBack,
Value= "{\"action\": \"ShowGif\" }"
},
new CardAction()
{
Title = "Meh...",
Text ="HappinessSearch",
//DisplayText = null,
Type = ActionTypes.MessageBack,
Value = "{\"action\": \"HappinessSearch\" }"
}
}
};
return card;
}
}
But still not working. No message is sent back to the bot. If I use imBack
type it does, but the message appears in the chat, what I don't want and messageBack
is supposed to work.
EDIT 3 following the code provided
dialog
private async Task ShowLuisResult(IDialogContext context, LuisResult result)
{
await context.PostAsync($"You have reached {result.Intents[0].Intent}. You said: {result.Query}");
context.Call(new HeroCardDialog(), MessageReceivedAsync);
}
public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var message = await result;
if (message != null)
{
//
}
//context.Wait(MessageReceived);
context.Done<object>(null);
}
HeroCardDialog
public class HeroCardDialog : IDialog<object>
{
public async Task StartAsync(IDialogContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
//Set the Last Dialog in Conversation Data
context.UserData.SetValue("HeroCardId", "HerdoCard Dialog");
var message = context.MakeMessage();
var attachment = GetHeroCard();
message.Attachments.Add(attachment);
await context.PostAsync((message));
context.Done<object>(null);
}
private static Attachment GetHeroCard()
{
var heroCard = new HeroCard()
{
Title = "Hi!",
Text = "Are you happy?",
Buttons = new List<CardAction>()
{
new CardAction()
{
Title = "Yes",
Text = "ShowGif",
DisplayText = null,
Type = ActionTypes.MessageBack,
Value= "{\"msgback\" : \"ShowGif\"}"
},
new CardAction()
{
Title = "Meh...",
Text ="HappinessSearch",
DisplayText = null,
Type = ActionTypes.MessageBack,
Value= "{\"msgback\" : \"HappinessSearch\"}"
}
}
};
return heroCard.ToAttachment();
}
}
Upvotes: 2
Views: 873
Reputation: 3158
PostBack
is not supported by Microsoft Teams. Please check the list of supported button activities in Microsoft Teams.
We recommend that you use messageBack
as you can create a fully customized action.
Upvotes: 1