简体   繁体   English

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

[英]Send single adaptive card as welcome and then take action.submit data and end conversation

so I have this code below I have adapted it to use only one card need, I would like help to remove the random function as when I click action.submit on the card it brings up the same card again I would like this card to be displayed just once and end the conversation with thank you when action.submit is pressed. 所以我在下面有这个代码我已经调整它只使用一个卡需要,我想帮助删除随机功能,因为当我点击卡上的action.submit它再次带来同一张卡我希望这张卡是只显示一次,并在按下action.submit时结束对话,谢谢。

I have followed along with lots of documentation and tutorials but some are out of date and some don't explain the method fully. 我已经跟着大量的文档和教程,但有些已经过时了,有些并没有完全解释这个方法。 I have really tired myself and would just like a bit of guidance to learn, thank you for any help. 我真的很累自己,只想学习一些指导,谢谢你的帮助。

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;
    }
}

expected results are single adaptive card shown data collected, action. 预期结果是单一自适应卡显示收集的数据,动作。 submit pressed, data submitted and a thank you message. 提交按下,提交的数据和感谢信息。

You can eliminate the random function removing this instruction. 您可以删除删除此指令的随机函数。

r.Next(this._cards.Length) 

In the line: 在线:

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

This is the card's array: 这是卡的数组:

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"),
        };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM