[英]Add property in azure table storage using Microsoft Bot Services
[英]How to insert records to Azure Table Storage via Microsoft Bot Framework?
目前我正在使用帶有 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 和 Microsoft 為我的機器人提供的 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
我還將附上我的應用洞察力的屏幕截圖 - 也許這會有所幫助。
我希望你能幫助我,如果我需要添加更多信息。 隨時要求。
我看了你提供的視頻,發現在你的本地機器上, datetime
時間是這樣的格式: 18.01.2021 17:35:59
。
所以我認為根本原因是當它部署到 azure 時, datetime
時間是這種格式: 1/19/2021 10:09:31 AM
。 並且根據此文檔Characters Disallowed in Key Fields , PartitionKey
和RowKey
屬性不允許使用字符/
。
所以在方法private static void InsertRecord(xxx)
中,請使用Replace
方法將字符/
替換為字符.
或其他允許的字符。 如下所示:
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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.