Search code examples
c#botframework

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.

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.


Solution

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