目前,我正在使用带有C#的Bot Framework 4.0,并且正在尝试实现一种解决方案,其中将来自用户的消息存储在Azure表存储中。我已经想出了达到这个目标的想法,但是当我通过Bot Framework Emulator测试它时,我的方法仅在本地有效。
当我将代码发布到我的机器人到Azure时,消息将不会存储在表存储中。我有点困惑,因为我真的不知道为什么会这样。我已经检查了App Insights,但是显示的信息对我没有太大帮助。
这些是我安装的Nugget-Packages:
还有我安装的SDK:
我的整个实现包含四个元素,下面我将简要说明:
private static CloudTable GetTableReference()
{
var storageAccount = CloudStorageAccount.Parse("<connection-string>");
var tableClient = storageAccount.CreateCloudTableClient();
return tableClient.GetTableReference("<table-name>");
}
第一种方法是打包Azure表存储的凭据。该术语是一个占位符,代表Azure表存储的连接字符串。(是)表存储名称的占位符(显然)。
private static void InsertRecord(ITurnContext<IMessageActivity> turnContext, CloudTable table)
{
string partitionKey = DateTime.Now.ToString();
string rowKey = DateTime.Now.ToString();
var Eintrag = new UserEntity(partitionKey, rowKey)
{
UserStatement = $"{turnContext.Activity.Text}",
};
var insertOperation = TableOperation.Insert(Eintrag);
table.ExecuteAsync(insertOperation);
}
第二种方法为表存储准备内容。例如,用户消息也存储在“ UserStatement”变量以及时间戳中。
protected async Task NoMatchFound(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
var table = GetTableReference();
InsertRecord(turnContext, table);
// Wird ausgeführt, wenn keine KnowledgeBase gefunden wird
System.Diagnostics.Debug.WriteLine("### FINDE KEINE PASSENDE ANTWORT ###");
await turnContext.SendActivityAsync(MessageFactory.Text("Leider kann ich Ihnen hierbei noch nicht weiterhelfen. Ich bin aber schon dabei, Neues zu lernen!"), cancellationToken);
}
在最后一种方法中,将所有内容放在一起并最终执行。上面提到的这三种方法都在同一个名为“ Dispatchbot.cs”的文件中(我使用的是我的机器人将NLP与Dispatch示例一起使用)。
using Microsoft.Azure.Cosmos.Table;
using System;
namespace TableStorage
{
public class UserEntity : TableEntity
{
public UserEntity(string partitionKey, string rowKey)
{
PartitionKey = partitionKey;
RowKey = rowKey;
}
public UserEntity()
{
}
public string UserStatement { get; set; }
}
}
我拥有的最后一件事是另一个名为“ UserEntity.cs”的文件,其中打包了表条目的内容。整个实现肯定更容易实现。
我唯一需要做的就是调用NoMatchFound()方法,并且来自用户的输入应存储在Azure表存储中。但这不是我尝试在线测试的情况。
这是一个视频,其中我证明了表存储记录在本地没有任何问题:https : //youtu.be/fFtzjmOfQDs
我还将附上我的应用洞察力的屏幕截图-也许有帮助。
I hope you can help me out! If i need to add more informations, feel free to ask for that.
I watched the video you provided, and found that in your local machine, the datetime
is in this format: 18.01.2021 17:35:59
.
So I think the root cause is that when it's deployed to azure, the datetime
is in this format: 1/19/2021 10:09:31 AM
. And as per this doc Characters Disallowed in Key Fields, the character /
is not allowed for the PartitionKey
and RowKey
properties.
So in the method private static void InsertRecord(xxx)
, please use the Replace
method to replace the character /
with character .
or other allowed chars. Like below:
private static void InsertRecord(ITurnContext<IMessageActivity> turnContext, CloudTable table)
{
//replace the character / with character .
string partitionKey = DateTime.Now.ToString().Replace('/','.');
string rowKey = DateTime.Now.ToString().Replace('/','.');
var Eintrag = new UserEntity(partitionKey, rowKey)
{
UserStatement = $"{turnContext.Activity.Text}",
};
var insertOperation = TableOperation.Insert(Eintrag);
table.ExecuteAsync(insertOperation);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句