我们有一个bot框架中的Bot设计-4使用.Net c# sdk。此bot托管在IIS上,可在不同的通道上使用,如Directline、MS Teams等。我们希望向MS团队中的所有用户发送主动消息,通知他们,不管他们是否与bot通信。积极的信息将是1:1的信息。
在做了大量的研发工作后,我们发现只有在有会话参考的情况下,我们才能向用户发送积极主动的信息。(如果还有其他方法,请告诉我。)
使用下面的链接和示例向用户发送主动消息:
我们使用cosmos DB容器和自动保存中间件进行bot会话状态和用户状态管理。
ConfigureServices方法中的Startup.cs文件代码:
var blobDbService = botConfig.Services.FirstOrDefault(s => s.Type == ServiceTypes.BlobStorage) ?? throw new Exception("Please configure your Blob service in your .bot file.");
var BlobDb = blobDbService as BlobStorageService;
var dataStore = new AzureBlobStorage(BlobDb.ConnectionString, BlobDb.Container);
var userState = new UserState(dataStore);
var conversationState = new ConversationState(dataStore);
services.AddSingleton(dataStore);
services.AddSingleton(userState);
services.AddSingleton(conversationState);
services.AddSingleton<ConcurrentDictionary<string, ConversationReference>>();
services.AddSingleton(new BotStateSet(userState, conversationState));
services.AddBot<EnterpriseTiBOT>(options =>
{
// Autosave State Middleware (saves bot state after each turn)
options.Middleware.Add(new AutoSaveStateMiddleware(userState, conversationState));
}
为每个用户存储会话引用的代码:
private void AddConversationReference(Activity activity)
{
var conversationReference = activity.GetConversationReference();
_conversationReferences.AddOrUpdate(conversationReference.User.Id, conversationReference, (key, newValue) => conversationReference);
}
protected override async Task OnStartAsync(DialogContext dc, CancellationToken cancellationToken = default(CancellationToken))
{
AddConversationReference(dc.Context, cancellationToken);
}
notifyContoller中的代码与GitHub示例中的代码相同。我们面临两个问题:
发布于 2020-08-05 13:36:08
发布于 2021-08-20 09:16:59
我想在答复中补充意见,但我没有足够的声誉这样做。如果您想知道在哪里可以得到和保存会话参考,您可以通过名为: OnConversationUpdateActivityAsync的方法获得它。
我花了一些时间才了解到这一点,所以我认为分享是有用的。您可以从活动中获得很多信息,例如用户ID、通道ID,下面是一些示例代码:
public class ProactiveBot : ActivityHandler
{
...
...
protected override Task OnConversationUpdateActivityAsync(ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
{
var conversationReferences = turnContext.Activity.GetConversationReference();
//this is your user's ID
string userId = conversationReference.User.Id;
//this is the bot's ID and will be the same for all activities under same bot.
string botId = conversationReference.Bot.Id;
...
...
...
}
}
https://stackoverflow.com/questions/63261004
复制相似问题