Access UserProfile from NotifyBot - botframework

I'm trying to kind of merge the functionality of EchoBot and ProactiveBot. A bot that parses user commands, which can also be triggered through a URL to send some users a message based on the contents of their UserProfile.
I managed to get both parts to work, but I'm having trouble accessing the UserProfiles from the NotifyController, or solving the problem another way. I tried a few things, read many of the tutorials and also this excellent explanation, but I'm still stuck.
So my question is: How do I rewrite NotifyController so it calls a method in my Bot class (the one implementing ActivityHandler) - just like BotController (from EchoBot) does?
Or, if that's better: How do I get a reference to the UserProfile in the message handler in NotifyController?
Thanks!

Alright, I got it to work.
Basically I duplicated all the code for the conversationReferences, because those get stored in the Bot and are used in NotifyController.
Some code, in case somebody else is trying to do the same:
Startup.cs
Create another Singleton.
services.AddSingleton<ConcurrentDictionary<string, UserProfile>>();
In the bot class
I have a variable storing UserProfiles just like the one for ConversationReferences
private readonly ConcurrentDictionary<string, ConversationReference> _conversationReferences;
private readonly ConcurrentDictionary<string, UserProfile> _userProfiles;
The constructor now starts like this
public RmindrBot(ConversationState conversationState,
UserState userState,
ConcurrentDictionary<string, ConversationReference> conversationReferences,
ConcurrentDictionary<string, UserProfile> userProfiles)
{
_conversationReferences = conversationReferences;
_userProfiles = userProfiles;
...
(I think this is the most surprising bit for me - didn't think the framework would just magically figure out the new signature.)
A method for adding references:
private void AddConversationReference(Activity activity)
{
var conversationReference = activity.GetConversationReference();
_conversationReferences.AddOrUpdate(
conversationReference.User.Id,
conversationReference,
(key, newValue) => conversationReference);
}
...which is called in OnMessageActivityAsync:
var userProfile = await userStateAccessors.GetAsync(turnContext, () => new UserProfile());
userProfile.ID = turnContext.Activity.From.Id;
AddUserProfile(userProfile);
NotifyController
private readonly ConcurrentDictionary<string, UserProfile> _userProfiles;
public NotifyController(
IBotFrameworkHttpAdapter adapter,
IConfiguration configuration,
ConcurrentDictionary<string, ConversationReference> conversationReferences)
ConcurrentDictionary<string, ConversationReference> conversationReferences,
ConcurrentDictionary<string, UserProfile> userProfiles)
{
_adapter = adapter;
_conversationReferences = conversationReferences;
_userProfiles = userProfiles;
...
And then we can finally use it in the callback method and match the user to their ID:
private async Task BotCallback(ITurnContext turnContext, CancellationToken cancellationToken)
{
var userProfile = _userProfiles[turnContext.Activity.From.Id];
await turnContext.SendActivityAsync("proactive hello " + userProfile.ID);
}

Related

Store Workflow Activity Data When Publishing

I Need to store a specific activity data in another collection in database whenever a user publish a workflow in elsa.
I dont find any documentation, Please suggest me some resource or suggestion to achieve this. I have try to implement this with middleware. The Middleware code is
namespace WorkFlowV3
{
// You may need to install the Microsoft.AspNetCore.Http.Abstractions package into your project
public class CustomMiddleware
{
private readonly RequestDelegate _next;
static HttpClient client = new HttpClient();
public CustomMiddleware(RequestDelegate next)
{
_next = next;
}
public async Task Invoke(HttpContext httpContext)
{
//Write Custom Logic Here....
client.BaseAddress = new Uri("#");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
string path = "/api/test-middleware-call";
HttpResponseMessage response = await client.GetAsync(path);
await _next(httpContext);
}
}
// Extension method used to add the middleware to the HTTP request pipeline.
public static class CustomMiddlewareExtensions
{
public static IApplicationBuilder UseCustomMiddleware(this IApplicationBuilder builder)
{
return builder.UseMiddleware<CustomMiddleware>();
}
}
}
But in this process, I cant fetch the specific activity data.
The easiest way to store information in your own DB in response to the "workflow published" event is by implementing a notification handler (from MediatR) that handles the WorkflowDefinitionPublished notification.
For example:
public class MyWorkflowPublishedhandler : INotificationhandler<WorkflowDefinitionPublished>
{
private readonly IMyDatabaseStore _someRepository;
public MyWorkflowPublishedhandler(IMyDatabaseStore someRepository)
{
_someRepository = someRepository;
}
public async Task Handle(WorkflowDefinitionPublished notification, CancellationToken cancellationToken)
{
var workflowDefinition = notification.WorkflowDefinition;
// Your logic to do a thing.
}
}
To register this handler, from your Startup or Program class, add the following code:
services.AddNotificationHandler<MyWorkflowPublishedhandler>();
Your handler will be invoked every time a workflow gets published.

How to change locale for a conversation in runtme

I'm using botframework composer with multi language and want each user to be able to select preferred language/locale. After resolving the local code for his selection with a choice dialog, how can I set it in conversation so that his locale setting in his device will be overruled for rest of conversation?
Changing locale in emulator works fine, want same behaviour after user selection.
Setting turn.locale works for one turn, but is reset on next turn.
supposing you don't have control over the client, which would be the best.
You can resort to an old overload on the ever-growing hierarchy of bot adapters that hasn't been marked as deprecated.
You'd have to use the PostAsync method (api/post-messages endpoint) in the following controller (showing the one created by the current set of bot framework templates just for comparison):
[Route("api")]
[ApiController]
public class BotController : ControllerBase
{
private readonly IBotFrameworkHttpAdapter StreamingAdapter;
private readonly BotFrameworkAdapter PostAdapter;
private readonly ConversationLocales ConversationLocales;
private readonly IBot Bot;
public BotController(
IBotFrameworkHttpAdapter streamingAdapter,
BotFrameworkAdapter postAdapter,
ConversationLocales conversationLocales,
IBot bot)
{
StreamingAdapter = streamingAdapter;
PostAdapter = postAdapter;
Bot = bot;
}
[HttpPost("messages"), HttpGet("messages")]
public async Task PostOrStreamingAsync()
{
// Delegate the processing of the HTTP POST to the adapter.
// The adapter will invoke the bot.
await StreamingAdapter.ProcessAsync(Request, Response, Bot);
}
[HttpPost("post-messages")]
public async Task<InvokeResponse> PostAsync([FromBody] Activity activity)
{
var savedLocale = ConversationLocales.GetLocaleForConversation(activity.Conversation.Id);
activity.Locale = savedLocale ?? activity.Locale;
return await PostAdapter.ProcessActivityAsync(string.Empty, activity, Bot.OnTurnAsync, default);
}
}
That's supposing you implement a ConversationLocales service that allows you to keep the selected locale for each conversation id.
In the code above we're using the BotFrameworkAdapter adapter instead of IBotFrameworkHttpAdapter, however the AdapterWithErrorHandler used in the templates inherits indirectly from BotFrameworkAdapter, so you could do something like this in ConfigureServices to register "both" adapters:
services.AddSingleton<AdapterWithErrorHandler>();
services.AddSingleton<IBotFrameworkHttpAdapter>(sp => sp.GetRequiredService<AdapterWithErrorHandler>());
services.AddSingleton<BotFrameworkAdapter>(sp => sp.GetRequiredService<AdapterWithErrorHandler>());
To have a single adapter instance.
Using this method the adapter won't be able to use the bot channel streaming endpoints, but that shouldn't be much of a trouble, as long as you don't use the speech client.
You can also read some other details that might be relevan to you in my blog post How does a Bot Builder v4 bot work?, it's a bit dated but still valid.
UPDATE - Found a better solution 😊
This one works with the current wave of adapters and uses the messages pipeline, so it's "modern".
It also requires you to use a custom runtime, that you'll customize as follows.
1 - Create the following middleware
public class LocaleSelectionMiddleware : IMiddleware
{
private readonly IStatePropertyAccessor<string> _userLocale;
public LocaleSelectionMiddleware(UserState userState)
{
_userLocale = userState.CreateProperty<string>("locale");
}
public async Task OnTurnAsync(ITurnContext turnContext, NextDelegate next, CancellationToken cancellationToken = default)
{
if (turnContext is null)
{
throw new ArgumentNullException(nameof(turnContext));
}
var userLocale = await _userLocale.GetAsync(turnContext, () => turnContext.Activity.Locale);
turnContext.Activity.Locale = userLocale;
(turnContext as TurnContext).Locale = userLocale;
await next(cancellationToken).ConfigureAwait(false);
}
}
2 - Configure the middleware in the adapter in GetBotAdapter() in Startup.cs
public class Startup
{
public Startup(IWebHostEnvironment env, IConfiguration configuration)
{
this.HostingEnvironment = env;
this.Configuration = configuration;
}
//...
public BotFrameworkHttpAdapter GetBotAdapter(IStorage storage, BotSettings settings, UserState userState, ConversationState conversationState, IServiceProvider s)
{
var adapter = IsSkill(settings)
? new BotFrameworkHttpAdapter(new ConfigurationCredentialProvider(this.Configuration), s.GetService<AuthenticationConfiguration>())
: new BotFrameworkHttpAdapter(new ConfigurationCredentialProvider(this.Configuration));
adapter
.UseStorage(storage)
.UseBotState(userState, conversationState)
.Use(new RegisterClassMiddleware<IConfiguration>(Configuration))
.Use(new LocaleSelectionMiddleware(userState)) // <-- Add the middleware here
.Use(s.GetService<TelemetryInitializerMiddleware>());
//...
return adapter;
}
//...
}
3 - Set the user.locale property in any dialog
Set the user.locale property from any dialog, and the next turn will have the desired locale, and will be persisted in the user state, until they change it again.

How to handle end of conversation to start another dialog in QnA multi turn - Microsoft.Bot.Builder.AI.QnA.Dialogs.QnAMakerDialog

I have implemented multi turn QnA in our bot and using this class Microsoft.Bot.Builder.AI.QnA.Dialogs.QnAMakerDialog.
Now, I want to extend its functionality so that after mutli turn conversation, bot can ask user if the conversation helped or not? if not then bot will ask to log a ticket with help desk.
I am able to catch the end of multi turn dialog by overriding the Dialog.EndDialogAsync method but not able to start another dialog from there. Please help.
public class QnAMultiTurnBase : QnAMakerDialog
{
// Dialog Options parameters
public readonly string DefaultNoAnswer = Configuration.Messages("Troubleshoot", "NoAnswer");//"No QnAMaker answers found.";
public readonly string DefaultCardTitle = Configuration.Messages("Troubleshoot", "DidYouMean");//"Did you mean:";
public readonly string DefaultCardNoMatchText = Configuration.Messages("Troubleshoot", "NoneOfTheAbove");//"None of the above.";
public readonly string DefaultCardNoMatchResponse = Configuration.Messages("Troubleshoot", "Feedback");//"Thanks for the feedback.";
private readonly BotServices _services;
private readonly IConfiguration _configuration;
//private readonly IStatePropertyAccessor<Dictionary<string, string>> troubleshootQuery;
private readonly Dictionary<string, string> qnaPair = new Dictionary<string, string>();
private readonly string qnAMakerServiceName;
/// <summary>
/// Initializes a new instance of the <see cref="QnAMakerBaseDialog"/> class.
/// Dialog helper to generate dialogs.
/// </summary>
/// <param name="services">Bot Services.</param>
public QnAMultiTurnBase(BotServices services, IConfiguration configuration, string qnAMakerServiceName) : base()
{
this._services = services;
this._configuration = configuration;
this.qnAMakerServiceName = qnAMakerServiceName;
}
protected async override Task<IQnAMakerClient> GetQnAMakerClientAsync(DialogContext dc)
{
return this._services?.QnAServices[qnAMakerServiceName];
}
protected override Task<QnAMakerOptions> GetQnAMakerOptionsAsync(DialogContext dc)
{
return Task.FromResult(new QnAMakerOptions
{
ScoreThreshold = DefaultThreshold,
Top = DefaultTopN,
QnAId = 0,
RankerType = "Default",
IsTest = false
});
}
protected async override Task<QnADialogResponseOptions> GetQnAResponseOptionsAsync(DialogContext dc)
{
var noAnswer = (Activity)Activity.CreateMessageActivity();
noAnswer.Text = this._configuration["DefaultAnswer"] ?? DefaultNoAnswer;
var cardNoMatchResponse = MessageFactory.Text(DefaultCardNoMatchResponse);
var responseOptions = new QnADialogResponseOptions
{
ActiveLearningCardTitle = DefaultCardTitle,
CardNoMatchText = DefaultCardNoMatchText,
NoAnswer = noAnswer,
CardNoMatchResponse = cardNoMatchResponse,
};
return responseOptions;
}
public override Task EndDialogAsync(ITurnContext turnContext, DialogInstance instance, DialogReason reason, CancellationToken cancellationToken = default(CancellationToken))
{
try
{
// end of multi turn convversation
// ask if conversation helped the user or not
}
catch (Exception)
{
turnContext.SendActivityAsync(MessageFactory.Text(Configuration.Messages("UnknownError"))).Wait();
throw;
}
return base.EndDialogAsync(turnContext, instance, reason, cancellationToken);
}
}
Add a new dialog and initiate the dialog added using BeginDialogAsync:
AddDialog(new MoreHelp());
return await stepContext.BeginDialogAsync(nameof(MoreHelp), UserInfo, cancellationToken);
You can refer to this documentation where it specifies how to create your own prompts to gather user input. A conversation between a bot and a user often involves asking (prompting) the user for information, parsing the user's response, and then acting on that information.
Dialog actions – ability to control dialogs, BeginDialog, RepeatDialog, GotoDialog, EndDialog, etc.
Please follow the below for multi turn.
https://github.com/microsoft/BotBuilder-Samples/tree/main/samples/csharp_dotnetcore/05.multi-turn-prompt

Access IStatePropertyAccessor botstate in a controller in v4

If you need to access the state in botframework v4 outside of a bot context (that does not implement IBot), for instance a Controller, how could you easily get a hold of this state object? The problem is that you can't really inject it directly because it needs to be initialized with a ChannelId and ConversationId.
The following approach works but looks a bit strange as I am using the TestAdapter to initialize the TurnContext. Isn't there a better, more obvious method to get a hold of the state?
public class SampleController : ControllerBase
{
private readonly MyBotAccessors _botAccessors;
public SampleController(MyBotAccessors botAccessors)
{
_botAccessors = botAccessors;
}
}
public async Task Reset(string conversationKey, string channelId)
{
var turnContext = new TurnContext(new TestAdapter(), new Activity { ChannelId = channelId, Conversation = new ConversationAccount { Id = conversationKey } });
await _botAccessors.MyContextState.SetAsync(turnContext, new MyContextState().Reset());
await _botAccessors.ConversationState.SaveChangesAsync(turnContext);
}

How to open database connection in a BackgroundJob in ABP application

Issue
For testing, I create a new job, it just use IRepository to read data from database. The code as below:
public class TestJob : BackgroundJob<string>, ITransientDependency
{
private readonly IRepository<Product, long> _productRepository;
private readonly IUnitOfWorkManager _unitOfWorkManager;
public TestJob(IRepository<Product, long> productRepository,
IUnitOfWorkManager unitOfWorkManager)
{
_productRepository = productRepository;
_unitOfWorkManager = unitOfWorkManager;
}
public override void Execute(string args)
{
var task = _productRepository.GetAll().ToListAsync();
var items = task.Result;
Debug.WriteLine("test db connection");
}
}
Then I create a new application service to trigger the job. The code snippet as below:
public async Task UowInJobTest()
{
await _backgroundJobManager.EnqueueAsync<TestJob, string>("aaaa");
}
When I test the job, It will throw following exception when execute var task = _productRepository.GetAll().ToListAsync();
Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.Object name: 'AbpExampleDbContext'.
Solution
S1: Add UnitOfWork attribute on execute method. It can address the issue. But it is not better for my actual scenario. In my actual scenario, the job is a long time task, and has much DB operatons, if enable UnitOfWork for Execute method, it will lock db resource for a long time. So this is not a solution for my scenario.
[UnitOfWork]
public override void Execute(string args)
{
var task = _productRepository.GetAll().ToListAsync();
var items = task.Result;
Debug.WriteLine("test db connection");
}
S2: Execute DB operation in UnitOfWork explicitly. Also, this can address the issue, but I don’t think this is a best practice. In my example,just read data from database, no transaction is required. Even-though the issue is addressed, but I don’t think it’s a correct way.
public override void Execute(string args)
{
using (var unitOfWork = _unitOfWorkManager.Begin())
{
var task = _productRepository.GetAll().ToListAsync();
var items = task.Result;
unitOfWork.Complete();
}
Debug.WriteLine("test db connection");
}
Question
My question is what’s the correct and best way to execute a DB operation in BackgroundJob?
There is addtional another question, I create a new application service, and disable UnitOfWrok, but it works fine. Please see the code as below. Why It works fine in application service, but doesn’t work in BackgroundJob?
[UnitOfWork(IsDisabled =true)]
public async Task<GetAllProductsOutput> GetAllProducts()
{
var result = await _productRepository.GetAllListAsync();
var itemDtos = ObjectMapper.Map<List<ProductDto>>(result);
return new GetAllProductsOutput()
{
Items = itemDtos
};
}
The documentation on Background Jobs And Workers uses [UnitOfWork] attribute.
S1: Add UnitOfWork attribute on execute method. It can address the issue. But it is not better for my actual scenario. In my actual scenario, the job is a long time task, and has much DB operatons, if enable UnitOfWork for Execute method, it will lock db resource for a long time. So this is not a solution for my scenario.
Background jobs are run synchronously on a background thread, so this concern is unfounded.
S2: Execute DB operation in UnitOfWork explicitly. Also, this can address the issue, but I don’t think this is a best practice. In my example,just read data from database, no transaction is required. Even-though the issue is addressed, but I don’t think it’s a correct way.
You can use a Non-Transactional Unit Of Work:
[UnitOfWork(isTransactional: false)]
public override void Execute(string args)
{
var task = _productRepository.GetAll().ToListAsync();
var items = task.Result;
}
You can use IUnitOfWorkManager:
public override void Execute(string args)
{
using (var unitOfWork = _unitOfWorkManager.Begin(TransactionScopeOption.Suppress))
{
var task = _productRepository.GetAll().ToListAsync();
var items = task.Result;
unitOfWork.Complete();
}
}
You can also use AsyncHelper:
[UnitOfWork(isTransactional: false)]
public override void Execute(string args)
{
var items = AsyncHelper.RunSync(() => _productRepository.GetAll().ToListAsync());
}
Conventional Unit Of Work Methods
I create a new application service, and disable UnitOfWork, but it works fine.
Why it works fine in application service, but doesn’t work in BackgroundJob?
[UnitOfWork(IsDisabled = true)]
public async Task<GetAllProductsOutput> GetAllProducts()
{
var result = await _productRepository.GetAllListAsync();
var itemDtos = ObjectMapper.Map<List<ProductDto>>(result);
return new GetAllProductsOutput
{
Items = itemDtos
};
}
You are using different methods: GetAllListAsync() vs GetAll().ToListAsync()
Repository methods are Conventional Unit Of Work Methods, but ToListAsync() isn't one.
From the documentation on About IQueryable<T>:
When you call GetAll() outside of a repository method, there must be an open database connection. This is because of the deferred execution of IQueryable<T>. It does not perform a database query unless you call the ToList() method or use the IQueryable<T> in a foreach loop (or somehow access the queried items). So when you call the ToList() method, the database connection must be alive.

Resources