发送单个自适应卡作为欢迎,然后采取action.submit数据和结束对话

问题描述 投票:0回答:1

所以我在下面有这个代码我已经调整它只使用一个卡需要,我想帮助删除随机功能,因为当我点击卡上的action.submit它再次带来同一张卡我希望这张卡是只显示一次,并在按下action.submit时结束对话,谢谢。

我已经跟着大量的文档和教程,但有些已经过时了,有些并没有完全解释这个方法。我真的很累自己,只想学习一些指导,谢谢你的帮助。

public class AdaptiveCardsBot : IBot
{
    private const string WelcomeText = @"Type anything to see the prototype.";

    // This array contains the file location of our adaptive cards
    private readonly string[] _cards =
    {
        Path.Combine(".", "Resources", "card.json"),
    };

    public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
    {
        if (turnContext == null)
        {
            throw new ArgumentNullException(nameof(turnContext));
        }

        if (turnContext.Activity.Type == ActivityTypes.Message)
        {
            Random r = new Random();
            var cardAttachment = CreateAdaptiveCardAttachment(this._cards[r.Next(this._cards.Length)]);
            var reply = turnContext.Activity.CreateReply();
            reply.Attachments = new List<Attachment>() { cardAttachment };
            await turnContext.SendActivityAsync(reply, cancellationToken);
        }
        else if (turnContext.Activity.Type == ActivityTypes.ConversationUpdate)
        {
            if (turnContext.Activity.MembersAdded != null)
            {
                await SendWelcomeMessageAsync(turnContext, cancellationToken);
            }
        }
        else
        {
            await turnContext.SendActivityAsync($"{turnContext.Activity.Type} activity detected", cancellationToken: cancellationToken);
        }
    }

    private static async Task SendWelcomeMessageAsync(ITurnContext turnContext, CancellationToken cancellationToken)
    {
        foreach (var member in turnContext.Activity.MembersAdded)
        {
            if (member.Id != turnContext.Activity.Recipient.Id)
            {
                await turnContext.SendActivityAsync(
                    $"Welcome to This Adaptive card Prototype. {WelcomeText}",
                    cancellationToken: cancellationToken);
            }
        }
    }

    private static Attachment CreateAdaptiveCardAttachment(string filePath)
    {
        var adaptiveCardJson = File.ReadAllText(filePath);
        var adaptiveCardAttachment = new Attachment()
        {
            ContentType = "application/vnd.microsoft.card.adaptive",
            Content = JsonConvert.DeserializeObject(adaptiveCardJson),
        };
        return adaptiveCardAttachment;
    }
}

预期结果是单一自适应卡显示收集的数据,动作。提交按下,提交的数据和感谢信息。

c# botframework
1个回答
0
投票

您可以删除删除此指令的随机函数。

r.Next(this._cards.Length) 

在线:

var cardAttachment = CreateAdaptiveCardAttachment(this._cards[r.Next(this._cards.Length)]);

这是卡的数组:

private readonly string[] _cards =
        {
            Path.Combine(".", "Resources", "FlightItineraryCard.json"),
            Path.Combine(".", "Resources", "ImageGalleryCard.json"),
            Path.Combine(".", "Resources", "LargeWeatherCard.json"),
            Path.Combine(".", "Resources", "RestaurantCard.json"),
            Path.Combine(".", "Resources", "SolitaireCard.json"),
        };
© www.soinside.com 2019 - 2024. All rights reserved.