InputHint
Hi, I am building a bot where whenever the user is giving any input through speech, the bot will automatically open the microphone to go to the listening mode. Basically, I have to give expecting inputhint i know that. But i don't know whether it applies to all the dialog files as well when i am going from messagescontroller to some other files. Please help me MessagesController.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using System.Web.Http.Description;
using System.Speech;
using Microsoft.Bot.Builder.History;
using Autofac;
using System.IO;
namespace POSBot
{
[BotAuthentication]
public class MessagesController : ApiController
{
/// <summary>
/// POST: api/Messages
/// receive a message from a user and send replies
/// </summary>
/// <param name="activity"></param>
[ResponseType(typeof(void))]
public virtual async Task<HttpResponseMessage> Post([FromBody] Activity activity)
{
// check if activity is of type message
if (activity.GetActivityType() == ActivityTypes.Message)
{
var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
Activity isTypingReply = activity.CreateReply();
isTypingReply.Type = ActivityTypes.Typing;
await connector.Conversations.ReplyToActivityAsync(isTypingReply);
await Conversation.SendAsync(activity, () => new RootDialog());
}
else
{
await HandleSystemMessageAsync(activity);
}
return new HttpResponseMessage(System.Net.HttpStatusCode.Accepted);
}
private async Task<Activity> HandleSystemMessageAsync(Activity message)
{
if (message.Type == ActivityTypes.DeleteUserData)
{
// Implement user deletion here
// If we handle user deletion, return a real message
}
else if (message.Type == ActivityTypes.ConversationUpdate)
{
// Handle conversation state changes, like members being added and removed
// Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
// Not available in all channels
if (message.MembersAdded.Any(o => o.Id == message.Recipient.Id))
{
ConnectorClient client = new ConnectorClient(new Uri(message.ServiceUrl));
var reply = message.CreateReply();
reply.Text = "Hi, are you having a POS or EID issue.";
reply.Speak = "Hi, are you having a P O S or E I D issue.";
reply.InputHint = InputHints.ExpectingInput;
await client.Conversations.ReplyToActivityAsync(reply);
}
}
else if (message.Type == ActivityTypes.ContactRelationUpdate)
{
// Handle add/remove from contact lists
// Activity.From + Activity.Action represent what happened
}
else if (message.Type == ActivityTypes.Typing)
{
// Handle knowing tha the user is typing
}
else if (message.Type == ActivityTypes.Ping)
{
}
return null;
}
}
}
RootDialog.cs
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
namespace POSBot
{
public class RootDialog : IDialog<object>
{
public Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
return Task.CompletedTask;
}
public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;
await context.Forward(new BasicLuisDialog(), ResumeAfterBasicLuisDialog, activity, CancellationToken.None);
}
public async Task ResumeAfterBasicLuisDialog(IDialogContext context, IAwaitable<object> result)
{
context.Wait(MessageReceivedAsync);
}
}
}
BasicLuisDialog.cs
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Luis;
using Microsoft.Bot.Builder.Luis.Models;
using Microsoft.Bot.Connector;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Threading.Tasks;
namespace POSBot
{
[Serializable]
public class BasicLuisDialog : LuisDialog<object>
{
public int noneattempt = 0;
public int issueattempt = 0;
public int posattempt = 0;
public int eidattempt = 0;
public BasicLuisDialog() : base(new LuisService(new LuisModelAttribute(
ConfigurationManager.AppSettings["LuisAppId"],
ConfigurationManager.AppSettings["LuisAPIKey"],
domain: ConfigurationManager.AppSettings["LuisAPIHostName"])))
{
}
public List<string> none = new List<string>
{
"Sorry, I cannot understand your query",
"Sorry, I am unbale to handle that",
"Sorry, I am not trained for that",
"Sorry, I did not get you",
"Sorry, I did not follow"
};
public List<string> issue = new List<string> { "POS Open Fail", "Merge EID" };
public string myissue;
public string theissue;
[LuisIntent("None")]
public async Task NoneIntent(IDialogContext context, LuisResult result)
{
noneattempt++;
string mynone = new GlobalHandler().GetRandomString(none);
if (noneattempt >= 2)
{
await context.SayAsync(text: mynone+"...", speak: mynone);
await new TransferToAPerson().StartAsync(context);
}
else
{
await context.SayAsync(text: mynone+"...", speak: mynone);
context.Wait(this.MessageReceived);
}
}
[LuisIntent("Issue")]
public async Task IssueIntent(IDialogContext context, LuisResult result)
{
string prompt = "Okay, tell me what is your issue?";
string retryprompt = "Please try again";
var promptOptions = new PromptOptions<string>(prompt: prompt, options: issue, retry: retryprompt, speak: prompt, retrySpeak: retryprompt, promptStyler: new PromptStyler());
PromptDialog.Choice(context, Issue, promptOptions);
}
public async Task Issue(IDialogContext context, IAwaitable<string> result)
{
this.myissue = await result;
if (myissue.ToLower().Contains("pos") || myissue.ToLower().Contains("open") || myissue.ToLower().Contains("fail"))
{
await new POSOpenFail().StartAsync(context);
}
else if (myissue.ToLower().Contains("eid") || myissue.ToLower().Contains("merge") || myissue.ToLower().Contains("id"))
{
await new EIDMerge().StartAsync(context);
}
}
[LuisIntent("ReportOpenFailError")]
public async Task ReportOpenFailErrorIntent(IDialogContext context, LuisResult result)
{
List<string> choices = new List<string> { "Yes", "No" };
string prompt = "Are you calling for POS Open Fail?";
string retryprompt = "Please try again";
string promptspeak = "Are you calling for p o s open fail?";
var promptOptions = new PromptOptions<string>(prompt: prompt, options: choices, retry: retryprompt, speak: promptspeak, retrySpeak: retryprompt, promptStyler: new PromptStyler());
PromptDialog.Choice(context, ConfirmPOS, promptOptions);
}
public async Task ConfirmPOS(IDialogContext context, IAwaitable<string> result)
{
string confirm = await result;
if (confirm.ToLower() == "yes")
{
await new POSOpenFail().StartAsync(context);
}
else
{
posattempt++;
if (posattempt == 2)
{
await new TransferToAPerson().StartAsync(context);
}
else
{
await context.SayAsync(text: "Let's try again", speak: "Let's try again");
await POS(context);
}
}
}
public async Task POS(IDialogContext context)
{
List<string> choices = new List<string> { "Yes", "No" };
string prompt = "Are you calling for POS Open Fail?";
string retryprompt = "Please try again";
string promptspeak = "Are you calling for p o s open fail?";
var promptOptions = new PromptOptions<string>(prompt: prompt, options: choices, retry: retryprompt, speak: promptspeak, retrySpeak: retryprompt, promptStyler: new PromptStyler());
PromptDialog.Choice(context, ConfirmPOS, promptOptions);
}
[LuisIntent("EIDMerge")]
public async Task EIDMergeIntent(IDialogContext context, LuisResult result)
{
List<string> choices = new List<string> { "Yes", "No" };
string prompt = "Are you calling for Merge EID";
string retryprompt = "Please try again";
string promptspeak = "Are you calling for merge e i d?";
var promptOptions = new PromptOptions<string>(prompt: prompt, options: choices, retry: retryprompt, speak: promptspeak, retrySpeak: retryprompt, promptStyler: new PromptStyler());
PromptDialog.Choice(context, ConfirmEID, promptOptions);
}
public async Task ConfirmEID(IDialogContext context, IAwaitable<string> result)
{
string confirm = await result;
if (confirm.ToLower() == "yes")
{
await new EIDMerge().StartAsync(context);
}
else
{
eidattempt++;
if (eidattempt == 2)
{
await new TransferToAPerson().StartAsync(context);
}
else
{
await context.SayAsync(text: "Let's try again", speak: "Let's try again");
await EID(context);
}
}
}
public async Task EID(IDialogContext context)
{
List<string> choices = new List<string> { "Yes", "No" };
string prompt = "Are you calling for Merge EID";
string retryprompt = "Please try again";
string promptspeak = "Are you calling for merge e i d?";
var promptOptions = new PromptOptions<string>(prompt: prompt, options: choices, retry: retryprompt, speak: promptspeak, retrySpeak: retryprompt, promptStyler: new PromptStyler());
PromptDialog.Choice(context, ConfirmEID, promptOptions);
}
}
}
EIDMerge.cs
using Microsoft.Bot.Builder.Dialogs; using System; using System.Collections.Generic; using System.Threading.Tasks;
namespace POSBot { [Serializable] public class EIDMerge : BasicLuisDialog, IDialog
if (confirm.ToLower() == "yes")
{
await PersonAssist(context);
}
else
{
await context.SayAsync(text: "So you do not need assistance.", speak: "So you do not need assistance.");
var incidentNumber = "E" + new Random().Next(1000, 9999);
await new CloseContact().Start(context, incidentNumber);
}
}
public async Task PersonAssist(IDialogContext context)
{
string prompt = "Is the Person Merge needed for a store level employee or staff level employee?";
string retryprompt = "Please try again";
var promptOptions = new PromptOptions<string>(prompt: prompt, options: level, retry: retryprompt, speak: prompt, retrySpeak: retryprompt, promptStyler: new PromptStyler());
PromptDialog.Choice(context, Level, promptOptions);
}
public async Task Level(IDialogContext context, IAwaitable<string> result)
{
string level = await result;
if (level.ToLower().Contains("staff"))
{
await context.SayAsync("Contact the Corporate help desk through service cafe or at 555-555-5555 to assist with the eid merge.", speak: "Contact the Corporate help desk through service cafe or at 555-555-5555 to assist with the e i d merge.");
var incidentNumber = "E" + new Random().Next(1000, 9999);
await new CloseContact().Start(context, incidentNumber);
}
else if (level.ToLower().Contains("store"))
{
await new CreateLMSTicket().StartAsync(context);
}
}
}
}
CreateLMSTicket.cs
using Microsoft.Bot.Builder.Dialogs; using Microsoft.Bot.Connector; using System; using System.Collections.Generic; using System.Diagnostics; using System.Text; using System.Threading; using System.Threading.Tasks;
namespace POSBot { [Serializable] public class CreateLMSTicket : BasicLuisDialog, IDialog
string prompt = "Do you want to Merge another employee?";
string retryprompt = "Please try again";
var promptOptions = new PromptOptions<string>(prompt: prompt, options: choices, retry: retryprompt, speak: prompt, retrySpeak: retryprompt, promptStyler: new PromptStyler());
PromptDialog.Choice(context, Confirmed, promptOptions);
}
else
{
string prompt = "Please select one of the following options.";
string retryprompt = "Please try again";
var promptOptions = new PromptOptions<string>(prompt: prompt, options: option, retry: retryprompt, speak: prompt, retrySpeak: retryprompt, promptStyler: new PromptStyler());
PromptDialog.Choice(context, SubmissionNotComplete, promptOptions);
}
}
public async Task SubmissionNotComplete(IDialogContext context, IAwaitable<string> result)
{
string option = await result;
if (option.ToLower().Contains("change"))
{
string prompt = "Okay, Tell me which field do you want to change?";
string retryprompt = "Please Try again";
var promptOptions = new PromptOptions<string>(prompt: prompt, options: details, retry: retryprompt, speak: prompt, retrySpeak: retryprompt, promptStyler: new PromptStyler());
PromptDialog.Choice(context, Change, promptOptions);
}
else if (option.ToLower().Contains("cancel"))
{
await context.SayAsync(text: "You have cancelled the operation", speak: "You have cancelled the operation.");
await context.SayAsync(text: "You can again start a new conversation by asking me things like 'POS Error', 'EID Merge', etc.", speak: "You can again start a new conversation by asking me things like 'P O S Error', 'E I D Merge', etc");
context.Wait(this.MessageReceived);
}
else if (option.ToLower().Contains("redo"))
{
await context.SayAsync(text: "You requested to start the form fill up again", speak: "You requested to start the form fill up again.");
await First(context);
}
}
public async Task Change(IDialogContext context, IAwaitable<string> result)
{
string details = await result;
if (details.ToLower() == "first name")
{
await context.SayAsync(pf + this.FirstName, speak: pf + this.FirstName);
await FirstRepeat(context);
}
else if (details.ToLower() == "last name")
{
await context.SayAsync(pl+this.LastName, speak: pl + this.LastName);
await LastRepeat(context);
}
else if (details.ToLower() == "phone number")
{
await context.SayAsync(pph+this.PhoneNumber, speak: pph + this.PhoneNumber);
await PhoneRepeat(context);
}
else if (details.ToLower() == "store number")
{
await context.SayAsync(pstr+this.StoreNumber, speak: pstr + this.StoreNumber);
await context.SayAsync(text: str, speak: str);
context.Wait(StoreNumberRepeat);
}
else if (details.ToLower().Contains("previous"))
{
await context.SayAsync(ppetxt+this.Previouseid, speak: ppespk + this.Previouseid);
await context.SayAsync(text: petxt, speak: pespk);
context.Wait(PEIDRepeat);
}
else if (details.ToLower().Contains("new"))
{
await context.SayAsync(pnetxt+this.Neweid, speak: pnespk + this.Neweid);
await context.SayAsync(text: netxt, speak: nespk);
context.Wait(NEIDRepeat);
}
else if (details.ToLower() == "position")
{
await context.SayAsync(ppos+this.Position, speak: ppos+this.Position);
await context.SayAsync(text: pos, speak: pos);
context.Wait(PositionRepeat);
}
else
{
await ShowDetails(context);
}
}
public async Task FirstRepeat(IDialogContext context)
{
await context.SayAsync(text: f, speak: f);
context.Wait(FirstNameRepeat);
}
public async Task FirstNameRepeat(IDialogContext context, IAwaitable<IMessageActivity> activity)
{
string fname = this.FirstName;
var fn = await activity;
this.FirstName = fn.Text;
await context.SayAsync(text: $"First name is changed form {fname} to {this.FirstName}.", speak: $"First name is changed form {fname} to {this.FirstName}.");
string prompt = "Do you want to change any other fields?";
string retryprompt = "Please try again";
var promptOptions = new PromptOptions<string>(prompt: prompt, options: choices, retry: retryprompt, speak: prompt, retrySpeak: retryprompt, promptStyler: new PromptStyler());
PromptDialog.Choice(context, ConfirmChange, promptOptions);
}
public async Task LastRepeat(IDialogContext context)
{
await context.SayAsync(text: l, speak: l);
context.Wait(LastNameRepeat);
}
public async Task LastNameRepeat(IDialogContext context, IAwaitable<IMessageActivity> activity)
{
string lname = this.LastName;
var ln = await activity;
this.LastName = ln.Text;
await context.SayAsync(text: $"Last name is changed from {lname} to {this.LastName}.", speak: $"Last name is changed from {lname} to {this.LastName}.");
string prompt = "Do you want to change any other field?";
string retryprompt = "Please try again";
var promptOptions = new PromptOptions<string>(prompt: prompt, options: choices, retry: retryprompt, speak: prompt, retrySpeak: retryprompt, promptStyler: new PromptStyler());
PromptDialog.Choice(context, ConfirmChange, promptOptions);
}
public async Task PhoneRepeat(IDialogContext context)
{
await context.SayAsync(text: ph, speak: ph);
context.Wait(PhoneNumberRepeat);
}
public async Task PhoneNumberRepeat(IDialogContext context, IAwaitable<IMessageActivity> activity)
{
string phone = this.PhoneNumber;
var ph = await activity;
int flag = 0;
foreach (var c in ph.Text.ToCharArray())
{
if (!char.IsNumber(c) && c != '-')
{
flag = 1;
break;
}
}
if (flag == 1)
{
await context.SayAsync(text: "Phone number cannot contain any characters. Try again...", speak: "Phone number cannot contain any character. Try again");
await Phone(context);
}
else
{
if (ph.Text.Replace("-", "").Length != 10)
{
await context.SayAsync(text: "Phone number should be of length 10. Try again...", speak: "Phone number should be of length 10. Try again...");
await Phone(context);
}
else
{
this.PhoneNumber = ph.Text.Replace("-","");
await context.SayAsync(text: $"Phone number is changed from {phone} to {this.PhoneNumber}.", speak: $"Phone number is changed form {phone} to {this.PhoneNumber}.");
string prompt = "Do you want to change any other field?";
string retryprompt = "Please try again";
var promptOptions = new PromptOptions<string>(prompt: prompt, options: choices, retry: retryprompt, speak: prompt, retrySpeak: retryprompt, promptStyler: new PromptStyler());
PromptDialog.Choice(context, ConfirmChange, promptOptions);
}
}
}
public async Task StoreNumberRepeat(IDialogContext context, IAwaitable<IMessageActivity> activity)
{
string store = this.StoreNumber;
var st = await activity;
this.StoreNumber = st.Text.Replace(" ", string.Empty);
StringBuilder sb = new StringBuilder();
foreach (char c in this.StoreNumber.ToCharArray())
{
if (char.IsNumber(c))
sb.Append(" ").Append(c).Append(" ");
else
sb.Append(c);
}
this.Store = sb.ToString().Trim();
await context.SayAsync(text: $"Store number is changed form {store} to {this.StoreNumber}.", speak: $"Store number is changed form {store} to {this.Store}.");
string prompt = "Do you want to change any other field?";
string retryprompt = "Please try again";
var promptOptions = new PromptOptions<string>(prompt: prompt, options: choices, retry: retryprompt, speak: prompt, retrySpeak: retryprompt, promptStyler: new PromptStyler());
PromptDialog.Choice(context, ConfirmChange, promptOptions);
}
public async Task PEIDRepeat(IDialogContext context, IAwaitable<IMessageActivity> activity)
{
string peid = this.Previouseid;
var pe = await activity;
this.Previouseid = pe.Text.Replace(" ", string.Empty);
StringBuilder sb = new StringBuilder();
foreach (char c in this.Previouseid.ToCharArray())
{
if (char.IsNumber(c))
sb.Append(" ").Append(c).Append(" ");
else
sb.Append(c);
}
this.PreviousEID = sb.ToString().Trim();
await context.SayAsync(text: $"Previous EID is changed from {peid} to {this.Previouseid}.", speak: $"Previous e i d is changed from {peid} to {this.PreviousEID}");
string prompt = "Do you want to change any other field?";
string retryprompt = "Please try again";
var promptOptions = new PromptOptions<string>(prompt: prompt, options: choices, retry: retryprompt, speak: prompt, retrySpeak: retryprompt, promptStyler: new PromptStyler());
PromptDialog.Choice(context, ConfirmChange, promptOptions);
}
public async Task NEIDRepeat(IDialogContext context, IAwaitable<IMessageActivity> activity)
{
string neid = this.Neweid;
var ne = await activity;
this.Neweid = ne.Text.Replace(" ", string.Empty);
StringBuilder sb = new StringBuilder();
foreach (char c in this.Neweid.ToCharArray())
{
if (char.IsNumber(c))
sb.Append(" ").Append(c).Append(" ");
else
sb.Append(c);
}
this.NewEID = sb.ToString().Trim();
await context.SayAsync(text: $"New EID is changed from {neid} to {this.Neweid}.", speak: $"New e i d is changed from {neid} to {this.NewEID}");
string prompt = "Do you want to change any other field?";
string retryprompt = "Please try again";
var promptOptions = new PromptOptions<string>(prompt: prompt, options: choices, retry: retryprompt, speak: prompt, retrySpeak: retryprompt, promptStyler: new PromptStyler());
PromptDialog.Choice(context, ConfirmChange, promptOptions);
}
public async Task PositionRepeat(IDialogContext context, IAwaitable<IMessageActivity> activity)
{
string position = this.Position;
var pos = await activity;
this.Position = pos.Text;
await context.SayAsync(text: $"Position is changed from {position} to {this.Position}.", speak: $"Position is changed form {position} to {this.Position}.");
string prompt = "Do you want to change any other field?";
string retryprompt = "Please try again";
var promptOptions = new PromptOptions<string>(prompt: prompt, options: choices, retry: retryprompt, speak: prompt, retrySpeak: retryprompt, promptStyler: new PromptStyler());
PromptDialog.Choice(context, ConfirmChange, promptOptions);
}
public async Task ConfirmChange(IDialogContext context, IAwaitable<string> result)
{
string confirm = await result;
if (confirm.ToLower() == "yes")
{
string prompt = "Okay, what would you like to change?";
string retryprompt = "Please try again";
var promptOptions = new PromptOptions<string>(prompt: prompt, options: details, retry: retryprompt, speak: prompt, retrySpeak: retryprompt, promptStyler: new PromptStyler());
PromptDialog.Choice(context, Change, promptOptions);
}
else
{
await ShowDetails(context);
}
}
public async Task Confirmed(IDialogContext context, IAwaitable<string> result)
{
string confirm = await result;
if (confirm.ToLower() == "yes")
{
await new CreateLMSTicket().StartAsync(context);
}
else
{
var ticketNumber = "L"+new Random().Next(1000,9999);
await context.SayAsync(text: "A LMSTicket is provided to you and your message has been registered.", speak: "L M S Ticket is provided to you and your message has been registered.");
await new CreateServiceRequest().Start(context, ticketNumber);
}
}
}
}
GlobalHandler.cs
using System; using System.Collections.Generic;
namespace POSBot
{
[Serializable]
public class GlobalHandler
{
public string GetRandomString(List
I want the input hint to work in the createlmsticket.cs file.. Please help me. Whenever the bot says Enter your first name, the microphone should automatically open.
Have you tested the code? Meanwhile I'll be reproducing the issue and get back to you shortly.
Okay friends, this issue has been resolved already. Thank you anyway. I just had to use the 'options' in the context.SayAsync() to incorporate inputhint. I mean: await context.SayAsync(text: ... , speak: ... , options: new MessageOptions() { InputHint = InputHints.expectingInput});
Thanks for the update.