![](/img/trans.png)
[英]How to validate input fields in adaptive card bot framework v4 (waterfall model) c#
[英]Update already posted Adaptive Card in Bot Framework v4 in C#
我正在使用 C# 中的 Bot Framework v4 開發 Bot。 我現在想要的是,在使用 Actions 向用戶發送自適應卡片后,我可以在觸發 Action 后更新這張卡片。 例如,該按鈕將在單擊后消失。
在這里,我在 NodeJS 中有一個解決方案,但我無法將其轉移到 C#。 有人可以幫忙嗎?
在我使用 SharePoint 庫中的 MS Graph 搜索后,將生成這張卡片。
主要目標是,我可以使用 Refiners 用 JSON 的新卡更新卡。 “確定”按鈕是一個提交操作,可以在 OnMessageActivityAsync 方法中捕獲。 輸入值在活動值中,因此可以創建過濾方法。 我的問題是,我無法更新已經發送給用戶的卡。
在將第一張帶有結果的卡片發送給用戶之前,我將 Activity 寫入 State,因此我可以訪問 OnMessageActivityAsync 方法,但我不確定這是否是正確的方法。
protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
Logger.LogInformation("Running dialog with Message Activity.");
var txt = turnContext.Activity.Text;
dynamic val = turnContext.Activity.Value;
// Check if the activity came from a submit action
if (string.IsNullOrEmpty(txt) && val != null)
{
await turnContext.SendActivityAsync(MessageFactory.Text($"Refiner Language: {val.id_language}"));
await turnContext.SendActivityAsync(MessageFactory.Text($"Refiner MachType: {val.id_type}"));
var r = new StreamReader("Cards/helloCard2.json");
string json = r.ReadToEnd();
AdaptiveCard card = AdaptiveCard.FromJson(json).Card;
var docSearchState = await StateAccessor.GetAsync(turnContext);
Activity activity = docSearchState.Activity;
activity.Attachments = new List<Attachment>() {
new Attachment()
{
Name = "Card",
ContentType = AdaptiveCard.ContentType,
Content = card,
}
};
await turnContext.UpdateActivityAsync(activity);
}
此代碼給了我以下錯誤消息:
fail: Microsoft.Bot.Builder.Integration.AspNet.Core.BotFrameworkHttpAdapter[0]
Exception caught : Error reading JArray from JsonReader. Current JsonReader item is not an array: StartObject. Path 'DocumentSearchState.Activity.attachments.$values[0].content.body'.
有沒有人解決這個問題的好主意?
查看此答案以了解如何更新自適應卡片。
您可以編寫代碼以動態地從自適應卡片中刪除提交操作,或者您可以擁有兩種不同版本的卡片:一種帶有提交操作,一種沒有。
如果您希望使用可以安裝在 NuGet 包中的預構建代碼來簡化整個過程,請隨時在 GitHub 上表達您對這些想法的支持:
您可以在 MS Teams 等渠道中更新自適應卡,而不是直接使用。 要更新卡片,您需要一個訪問器來存儲發送當前消息時生成的 ActivityId。
var response = await stepContext.Context.SendActivityAsync(reply);
//Customize object to store ID
UserProfile user = new UserProfile(response.Id);
//Accessor to store card values to update it and disable buttons after click
await CardStateAccessor.SetAsync(stepContext.Context, user, cancellationToken);
在此之后的下一個瀑布步驟中,您需要編寫邏輯來更新此發布的消息。
UserProfile UserDataState = await CardStateAccessor.GetAsync(stepContext.Context, () => new UserProfile());
//Retrive accessor property
//Here I am updating adaptive card
var reply = stepContext.Context.Activity.CreateReply();
string text = File.ReadAllText("./AdaptiveCards/UpdatedYesNo.json");
var cardObj = JsonConvert.DeserializeObject(text);
reply.Attachments = new List<Attachment>()
{
new Attachment(){ContentType="application/vnd.microsoft.card.adaptive",Content=cardObj}
};
//Here you need to assign ID from accessor so that earlier posted msg gets updated
reply.Id = UserDataState.ActivityId;
//Use here UpdateActivityAsync to post updated message
await stepContext.Context.UpdateActivityAsync(reply, cancellationToken);
只是添加信息:對我來說,目標是在點擊后禁用自適應卡的按鈕。 因此,當我第二次發布自適應卡片時,我只是從按鈕中刪除了所有操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.