I created a bot using QnA Maker, which depending on the answer I should return the same answer or call another service, which in my case is to assemble a form flow from a JSON.
But when calling this service I start a new Conversation, so it does not return to the emulator.
I'm creating a conversation in another conversation. There must be something missing.
RootDialog.cs:
[Serializable]
public class RootDialog : QnAMakerDialog
{
public RootDialog() : base(
new QnAMakerService(
new QnAMakerAttribute(
ConfigurationManager.AppSettings["QnaSubscriptionKey"],
ConfigurationManager.AppSettings["QnaKnowledgebaseId"],
"Não encontrei sua resposta",
0.5
)
)
)
{
}
protected override async Task RespondFromQnAMakerResultAsync(IDialogContext context, IMessageActivity message, QnAMakerResults result)
{
var primeiraResposta = result.Answers.First().Answer;
if (primeiraResposta.IndexOf("form") == -1)
{
await context.PostAsync(primeiraResposta);
return;
}
await Conversation.SendAsync(message, () => Chain.From(() => FormDialog.FromForm(() => Formulario.JsonForm.BuildJsonForm(), FormOptions.PromptFieldsWithValues)));
return;
}
}
MessagesController.cs
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
if (activity.Type == ActivityTypes.ConversationUpdate)
{
if (activity.MembersAdded.Any(o => o.Id == activity.Recipient.Id))
{
var reply = activity.CreateReply();
reply.Text = "Hello...";
await connector.Conversations.ReplyToActivityAsync(reply);
}
}
else if (activity.Type == ActivityTypes.Message)
{
// HEREE!!!
await Conversation.SendAsync(activity, () => new Dialogs.RootDialog());
}
else
{
HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
The response of emulator:
I could not send, Repeat
Related
when I use the WebSockets I Can't get the userId on the server
Client code:
HubConnection = new HubConnectionBuilder().WithUrl(Config.BaseUrl + ApplicationConstants.SignalR.HubUrl, options =>
{
options.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransportType.WebSockets;
options.Headers.Add("Authorization", $"Bearer {NPCompletApp.Token}");
options.AccessTokenProvider = async () => await Task.FromResult(NPCompletApp.Token);
}).WithAutomaticReconnect().Build();
Server code:
public override async Task<Task> OnConnectedAsync()
{
ConnectedUserModel connectedUserModel = new ConnectedUserModel()
{
ConnectionId = Context.ConnectionId, UserId = _userManager.GetUserId(Context.User)
};
UserHandler.connectedUsers.Add(connectedUserModel);
var temp = new List<string>();
foreach(ConnectedUserModel connectedUser in UserHandler.connectedUsers)
{
if (!String.IsNullOrEmpty(connectedUserModel.UserId) && temp.Find(x=> x == connectedUserModel.UserId) == null)
{
temp.Add(connectedUserModel.UserId);
await OnConnectAsync(connectedUserModel.UserId);
}
}
return base.OnConnectedAsync();
}
The good thing is that I can catch if the user Disconnected, but still can't know who is the user.
server code (On disconnecting):
public override async Task<Task> OnDisconnectedAsync(Exception? exception)
{
var connection = UserHandler.connectedUsers.Find(x => x.ConnectionId == Context.ConnectionId);
await OnDisconnectAsync(connection.UserId);
UserHandler.connectedUsers.Remove(connection);
return base.OnDisconnectedAsync(exception);
}
On the other hand When I use LongPolling I can get the userId but I can't catch him when disconnecting
client code:
HubConnection = new HubConnectionBuilder().WithUrl(Config.BaseUrl + ApplicationConstants.SignalR.HubUrl, options =>
{
options.Transports = Microsoft.AspNetCore.Http.Connections.HttpTransportType.LongPolling;
options.Headers.Add("Authorization", $"Bearer {NPCompletApp.Token}");
options.AccessTokenProvider = async () => await Task.FromResult(NPCompletApp.Token);
}).WithAutomaticReconnect().Build();
What should I do ? I want to know who is the user in my context & to catch him when he diconnected.
On your server your have to configure the middleware.
This is taken from a working project...
Server :
services.TryAddEnumerable(
ServiceDescriptor.Singleton<IPostConfigureOptions<JwtBearerOptions>,
ConfigureJwtBearerOptions>());
ConfigureJwtBearerOptions.cs
public class ConfigureJwtBearerOptions : IPostConfigureOptions<JwtBearerOptions>
{
private readonly ChatConfigurations config;
public ConfigureJwtBearerOptions(ChatConfigurations config)
{
this.config = config;
}
public void PostConfigure(string name, JwtBearerOptions options)
{
var originalOnMessageReceived = options.Events.OnMessageReceived;
options.Events.OnMessageReceived = async context =>
{
await originalOnMessageReceived(context);
if (string.IsNullOrEmpty(context.Token)) {
var accessToken = context.Request.Query["access_token"];
var requestPath = context.HttpContext.Request.Path;
var endPoint = $"/{config.EndPoint}";
if (!string.IsNullOrEmpty(accessToken) &&
requestPath.StartsWithSegments(endPoint)) {
context.Token = accessToken;
}
}
};
}
}
In your client you also have to configure for tokens.
public async ValueTask InitialiseAsync()
{
IsInitialised = false;
hubConnection = CreateHubConnection();
hubConnection.On<string, string>("ReceiveMessage", ReceiveMessageAsync);
....
await hubConnection.StartAsync();
await hubConnection.SendAsync("JoinGroup", roomName);
IsInitialised = true;
}
private HubConnection CreateHubConnection()
{
var endPoint = $"/{config.EndPoint}";
var hubConnection = new HubConnectionBuilder()
.WithUrl(navigationManager.ToAbsoluteUri(endPoint), options =>
{
options.AccessTokenProvider = async () =>
{
var accessTokenResult = await accessTokenProvider.RequestAccessToken();
accessTokenResult.TryGetToken(out var accessToken);
var token = accessToken.Value;
return token;
};
})
.WithAutomaticReconnect()
.Build();
return hubConnection;
}
My Hubs OnConnectedAsync
public async override Task OnConnectedAsync()
{
logger.LogDebug("Hub Connection");
await chatService.RegisterConnectionAsync(Context.ConnectionId, Context.UserIdentifier);
await base.OnConnectedAsync();
}
Note: I am persisting connections to a database.
My Hubs OnDisconnectedAsync
public async override Task OnDisconnectedAsync(Exception exception)
{
logger.LogDebug("Hub Disconnect");
await chatService.RegisterDisconnectAsync(Context.ConnectionId);
await base.OnDisconnectedAsync(exception);
}
Some debug logs:
dbug: OrakTech.ChatServer.Brokers.Loggings.LoggingBroker[0]
Hub Connection
dbug: OrakTech.ChatServer.Brokers.Loggings.LoggingBroker[0]
Hub Connected (r7SJaAMEGs7eovH810H5Xg, c8f81673-d8b3-4e46-80f6-a83b671e6ff1)
dbug: OrakTech.ChatServer.Brokers.Loggings.LoggingBroker[0]
Join Group (TestRoom : r7SJaAMEGs7eovH810H5Xg)
dbug: OrakTech.ChatServer.Brokers.Loggings.LoggingBroker[0]
Hub Disconnect
dbug: OrakTech.ChatServer.Brokers.Loggings.LoggingBroker[0]
Hub Disconnect (r7SJaAMEGs7eovH810H5Xg)
I'm developing an UWP app that calls a web service. For that I use a HttpClient object from Windows.Web.Http namespace and I pass a IHttpFilter object to its constructor. This filter is responsible for the authentication process. I based my solution following this link and the authentication logic is based on this
I don't know what I'm doing wrong but I got this exception: A method was called at an unexpected time. (Exception from HRESULT: 0x8000000E)
The scenario that I'm testing is when the token is invalid despite it is assumed it is valid. In this case the (now > TokenManager.Token.ExpiresOn) condition is false, then an authorization header is added (with an invalid token), then a request is sent, then the http response code and www-authenticate header is inspected and if it is neccessary, a new access token must be requested by means of refresh token in order to do a retry. It is when I reach this line when the exception is thrown (the second time): response = await InnerFilter.SendRequestAsync(request).AsTask(cancellationToken, progress);
I have no idea what I'm doing wrong. I have seen another questions where people got this error and usually it's because they try to get the task's result without waiting for the task's completion but I'm using the await keyword in all asynchronous methods.
public class AuthFilter : HttpFilter
{
public override IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress> SendRequestAsync(HttpRequestMessage request)
{
return AsyncInfo.Run<HttpResponseMessage, HttpProgress>(async (cancellationToken, progress) =>
{
var retry = true;
if (TokenManager.TokenExists)
{
var now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
if (now > TokenManager.Token.ExpiresOn)
{
retry = false;
await RefreshTokenAsync();
}
request.Headers.Authorization = new HttpCredentialsHeaderValue(TokenManager.Token.TokenType, TokenManager.Token.AccessToken);
}
HttpResponseMessage response = await InnerFilter.SendRequestAsync(request).AsTask(cancellationToken, progress);
cancellationToken.ThrowIfCancellationRequested();
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
var authHeader = response.Headers.WwwAuthenticate.SingleOrDefault(x => x.Scheme == "Bearer");
if (authHeader != null)
{
var challenge = ParseChallenge(authHeader.Parameters);
if (challenge.Error == "token_expired" && retry)
{
var success = await RefreshTokenAsync();
if (success)
{
request.Headers.Authorization = new HttpCredentialsHeaderValue(TokenManager.Token.TokenType, TokenManager.Token.AccessToken);
response = await InnerFilter.SendRequestAsync(request).AsTask(cancellationToken, progress);
}
}
}
}
return response;
});
}
private async Task<bool> RefreshTokenAsync()
{
using (var httpClient = new HttpClient())
{
httpClient.DefaultRequestHeaders.Accept.Add(new HttpMediaTypeWithQualityHeaderValue("application/json"));
var content = new HttpStringContent(JsonConvert.SerializeObject(new { RefreshToken = TokenManager.Token.RefreshToken }), UnicodeEncoding.Utf8, "application/json");
var response = await httpClient.PostAsync(SettingsService.Instance.WebApiUri.Append("api/login/refresh-token"), content);
if (response.IsSuccessStatusCode)
TokenManager.Token = JsonConvert.DeserializeObject<Token>(await response.Content.ReadAsStringAsync());
return response.IsSuccessStatusCode;
}
}
private (string Realm, string Error, string ErrorDescription) ParseChallenge(IEnumerable<HttpNameValueHeaderValue> input)
{
var realm = input.SingleOrDefault(x => x.Name == "realm")?.Value ?? string.Empty;
var error = input.SingleOrDefault(x => x.Name == "error")?.Value ?? string.Empty;
var errorDescription = input.SingleOrDefault(x => x.Name == "error_description")?.Value ?? string.Empty;
return (realm, error, errorDescription);
}
public override void Dispose()
{
InnerFilter.Dispose();
GC.SuppressFinalize(this);
}
}
public abstract class HttpFilter : IHttpFilter
{
public IHttpFilter InnerFilter { get; set; }
public HttpFilter()
{
InnerFilter = new HttpBaseProtocolFilter();
}
public abstract IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress> SendRequestAsync(HttpRequestMessage request);
public abstract void Dispose();
}
EDIT:
According to these links (link, link), it seems to be that I cannot send the same request twice. But I need to re-send the same request but with different authentication header. How can I achieve that?
Ok, after struggling with this for a long time, I ended up doing this:
public override IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress> SendRequestAsync(HttpRequestMessage request)
{
return AsyncInfo.Run<HttpResponseMessage, HttpProgress>(async (cancellationToken, progress) =>
{
var retry = true;
if (TokenManager.TokenExists)
{
var now = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
if (now > TokenManager.Token.ExpiresOn)
{
retry = false;
await RefreshTokenAsync();
}
request.Headers.Authorization = new HttpCredentialsHeaderValue(TokenManager.Token.TokenType, TokenManager.Token.AccessToken);
}
HttpResponseMessage response = await InnerFilter.SendRequestAsync(request).AsTask(cancellationToken, progress);
cancellationToken.ThrowIfCancellationRequested();
if (response.StatusCode == HttpStatusCode.Unauthorized)
{
var authHeader = response.Headers.WwwAuthenticate.SingleOrDefault(x => x.Scheme == "Bearer");
if (authHeader != null)
{
var challenge = ParseChallenge(authHeader.Parameters);
if (challenge.Error == "token_expired" && retry)
{
var secondRequest = request.Clone();
var success = await RefreshTokenAsync();
if (success)
{
secondRequest.Headers.Authorization = new HttpCredentialsHeaderValue(TokenManager.Token.TokenType, TokenManager.Token.AccessToken);
response = await InnerFilter.SendRequestAsync(secondRequest).AsTask(cancellationToken, progress);
}
}
}
}
return response;
});
}
public static HttpRequestMessage Clone(this HttpRequestMessage request)
{
var clone = new HttpRequestMessage(request.Method, request.RequestUri)
{
Content = request.Content
};
foreach (KeyValuePair<string, object> prop in request.Properties.ToList())
{
clone.Properties.Add(prop);
}
foreach (KeyValuePair<string, string> header in request.Headers.ToList())
{
clone.Headers.Add(header.Key, header.Value);
}
return clone;
}
Because I needed to re-send the request, I made a second request cloning the first one.
When a conversation is inside FormFlow and user types 'Help', at present the Scorables Global Message Handlers 'Help' takes control. I would like the FormFlows 'Help' Command when inside form and Scorables 'Help' only when its in other conversations. Is there a way to achieve this?
protected override async Task PostAsync(IActivity item, string state,
CancellationToken token)
{
var message = item as IMessageActivity;
if (message != null)
{
var helpDialog = new HelpDialog();
var interruption = helpDialog.Void<object, IMessageActivity>();
this.task.Call(interruption, null);
await this.task.PollAsync(token);
}
}
public class HelpDialog : IDialog<object>
{
public async Task StartAsync(IDialogContext context)
{
var userHelpMessage = "You have reached Help Section.\n"
+ StaticMessages.HelpMessage
+ "\n Your previous conversation is active
and you can return to prior dialog by typing 'Back' or 'Return'.";
await context.PostAsync(userHelpMessage);
context.Wait(this.MessageReceived);
}
private async Task MessageReceived(IDialogContext context,
IAwaitable<IMessageActivity> result)
{
var message = await result;
var incomingMessage = message.Text.ToLowerInvariant();
if ((incomingMessage != null) && (incomingMessage.Trim().Length > 0))
{
context.Done<object>(null);
}
else if (incomingMessage.Equals("return", StringComparison.InvariantCultureIgnoreCase))
{
context.Done<object>(null);
}
else if (incomingMessage.Equals("back", StringComparison.InvariantCultureIgnoreCase))
{
context.Done<object>(null);
}
else
{
context.Fail(new Exception("Message was not a string or was an empty string."));
}
}
}
[Serializable]
public class BuildARCollRaiseDisputeForm
{
//Custom Form Builder for custom yes options
BuildCustomForm customForm = new BuildCustomForm();
ARCollRaiseDisputeQuery disputeQuery = new ARCollRaiseDisputeQuery();
public IForm<ARCollRaiseDisputeQuery> BuildARCollRaiseDisputeForms()
{
IForm<ARCollRaiseDisputeQuery> form;
OnCompletionAsyncDelegate<ARCollRaiseDisputeQuery> processARCollRaiseDisputeSave = async (context, state) =>
{
var message = $"Saving your dispute reason against this Invoice. ";
await context.PostAsync(message);
};
var builder = customForm.CreateCustomForm<ARCollRaiseDisputeQuery>()
.Field(nameof(disputeQuery.BODISPUTEREASONOPTIONS),
validate: async(state, value) => await ValidatePymtDisputeReason(state, value)
)
.Field(nameof(disputeQuery.DisputeItemReasons),
active: state => ActiveDisputeItemReason(state)
)
.Field(nameof(disputeQuery.DisputeShipmentReasons),
active: ActiveDisputeShipmentReason
)
.Field(nameof(disputeQuery.DisputeInvoicingReasons),
active: ActiveDisputeInvoicingReason
)
.Field(new FieldReflector<ARCollRaiseDisputeQuery>(nameof(disputeQuery.OtherPymtDisputeReason))
.SetActive(state => ActiveOtherPymtDisputeReason(state))
.SetPrompt(new PromptAttribute("Do you have any custom message to my team for non-payment for this Invoice? If yes, then please type and send as single message."))
.SetValidate(async (state, value) => await ValidateOtherPymtDisputeReason(state, value))
)
.AddRemainingFields()
.Confirm("I have the following reasons for raising dispute against this Invoice and I am ready to submit your message. {BODISPUTEREASONOPTIONS} Shall I go ahead? ")
.OnCompletion(processARCollRaiseDisputeSave)
.Message("Thank you");
form = builder.Build();
return form;
}
}
Below are the source Code for communicating to LUIS- AI from my Bot application. When I try to communicate I am always getting Access Denied response. I don't know what I am missing here.
[LuisModel("8c9285fb-198a-4f49-8fe4-b08ac5541ac2", "5c47c63887e346c2aee24d1755e07d29")]
[Serializable]
public class LUISDialog:LuisDialog<RoomReservation>
{
private readonly BuildFormDelegate<RoomReservation> Reservation;
public LUISDialog(BuildFormDelegate<RoomReservation> reservceRoom)
{
this.Reservation = reservceRoom;
}
[LuisIntent("")]
[LuisIntent("None")]
public async Task None(IDialogContext dialogContext, LuisResult luisResult)
{
await dialogContext.PostAsync("I am sorry I don't know what you mean ");
dialogContext.Wait(MessageReceived);
}
[LuisIntent("Greeting")]
public async Task Greeting(IDialogContext dialogContext, LuisResult luisResult)
{
dialogContext.Call(new GreetingDialog(), CallBack);
}
private async Task CallBack(IDialogContext context, IAwaitable<object> result)
{
context.Wait(MessageReceived);
}
[LuisIntent("Reservation")]
public async Task RoomReservation(IDialogContext dialogContext, LuisResult luisResult)
{
FormDialog<RoomReservation> enrollmentForm =new FormDialog<RoomReservation>(new RoomReservation(),this.Reservation, FormOptions.PromptInStart);
dialogContext.Call(enrollmentForm, CallBack);
}
[LuisIntent("QueryAmenities")]
public async Task QueryAmenities(IDialogContext dialogContext, LuisResult luisResult)
{
foreach (var entity in luisResult.Entities.Where(entity=>entity.Type=="Amenity"))
{
var value = entity.Entity.ToLower();
if (value == "pool" || value == "gym" || value == "wifi" || value == "towels")
{
await dialogContext.PostAsync("Yes we have that");
dialogContext.Wait(MessageReceived);
return;
}
await dialogContext.PostAsync("I'am sorry we don't have that");
dialogContext.Wait(MessageReceived);
return;
}
await dialogContext.PostAsync("I'am sorry we don't have that");
dialogContext.Wait(MessageReceived);
}
}
Screen shot of error I am getting
Controller Code
[BotAuthentication]
public class MessagesController : ApiController
{
/// <summary>
/// POST: api/Messages
/// Receive a message from a user and reply to it
/// </summary>
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
//ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
//// calculate something for us to return
//int length = (activity.Text ?? string.Empty).Length;
//// return our reply to the user
//Activity reply = activity.CreateReply($"You sent {activity.Text} which was {length} characters");
//await connector.Conversations.ReplyToActivityAsync(reply);
// await Conversation.SendAsync(activity, () => HotelBotDialog.dialog);
await Conversation.SendAsync(activity, MakeLuisDialog);
}
else
{
await HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
LUIS Intent
Please help me to resolve this
I just tested a connection to your LUIS and it seems to be working: https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/8c9285fb-198a-4f49-8fe4-b08ac5541ac2?subscription-key=5c47c63887e346c2aee24d1755e07d29&verbose=true&q=hello
Probrably you are using the endpoint key instead the Programmatic Key API. Check this:
enter image description here
I've updated my NuGet packages to use the version 1.2.0.1 of the Microsoft Bot Framework.
Some breaking changes were reported here, and I managed to fix the build errors. But the application is not working anymore..
I have two problems:
The code throws an InvalidIntentHandlerException when I send a message an utterance to the controller.
In my 'intent' method (decorated with the LuisIntent attribute) it was possible to read the value of the entities. Like so:
[Serializable]
[LuisModel("xxxxx", "xxxx")]
public class BookFlightDialog : LuisDialog<BookFlightForm>
{
private readonly BuildFormDelegate<BookFlightForm> BuildForm;
internal BookFlightDialog(BuildFormDelegate<BookFlightForm> buildForm)
{
BuildForm = buildForm;
}
[LuisIntent("")]
[LuisIntent("None")]
public async Task None(IDialogContext context, LuisResult result)
{
await context.PostAsync("I'm sorry. I didn't understand you.");
context.Wait(MessageReceived);
}
[LuisIntent("BookAFlight")]
public async Task BookAFlight(IDialogContext context, LuisResult result)
{
var form = new BookFlightForm();
// var entities = new List<EntityRecommendation>(result.Entities);
var locations = result.Entities.Where(e => e.Type.Equals("builtin.geography") || e.Type.Equals("builtin.geography.city")).OrderBy(e => e.StartIndex);
if (locations.Any())
{
form.LocationFrom = locations.First().Name;
if (locations.Count() == 2)
{
form.LocationTo = locations.Skip(1).First().Name;
}
}
var date = result.Entities.FirstOrDefault(e => e.Type == "builtin.datetime.date");
if (date != null) form.DepartureDate = DateTime.Parse(date.Name);
var formDialog = new FormDialog<BookFlightForm>(form, BuildForm, FormOptions.PromptInStart);
context.Call(formDialog, OnComplete);
}
private async Task OnComplete(IDialogContext context, IAwaitable<BookFlightForm> result)
{
BookFlightForm booking;
try
{
booking = await result;
}
catch (OperationCanceledException)
{
await context.PostAsync("Ok, see you later.");
return;
}
if (booking != null)
{
var service = new SkyScannerService();
var possibilities = await service.Search(booking);
await context.PostAsync(possibilities);
}
else
{
await context.PostAsync("Form returned empty response!");
}
context.Wait(MessageReceived);
}
}
How do I fix the exception and how do I read the value of the entities?
Thanks once again!
This is because you are not using inbuilt LuisResult class by having using LuisResult = Bots.Results.LuisResult;. Replace it with using Microsoft.Bot.Builder.Luis.Models;.