发送一张自适应卡作为欢迎,然后采取行动。提交数据并结束对话

时间:2019-03-28 11:21:15

标签: c# botframework

所以我在下面有此代码,我已将其修改为仅使用一张卡,我希望帮助删除随机功能,因为当我单击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;
    }
}

预期结果是单个自适应卡显示收集的数据,操作。提交新闻稿,提交数据和感谢信息。

1 个答案:

答案 0 :(得分: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"),
        };