Esempio n. 1
0
        protected override async Task OnMessageActivityAsync(ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            //var adaptiveCardAttachment = CardsDemoWithTypes();
            //await turnContext.SendActivityAsync(MessageFactory.Attachment(adaptiveCardAttachment));

            if (turnContext.Activity.Type == ActivityTypes.Message)
            {
                // Check if user submitted AdaptiveCard input
                if (turnContext.Activity.Value != null)
                {
                    //var activityValue = turnContext.Activity.AsMessageActivity().Value as Newtonsoft.Json.Linq.JObject;
                    //if (activityValue != null)
                    //{
                    //    var categorySelection = activityValue.ToObject<CategorySelection>();
                    //    var category = categorySelection.Category;
                    //    await turnContext.SendActivityAsync(category);
                    //}

                    // Convert String to JObject
                    String  value   = turnContext.Activity.Value.ToString();
                    JObject results = JObject.Parse(value);

                    // Get type from input field
                    String submitType = results.GetValue("Type").ToString().Trim();
                    switch (submitType)
                    {
                    case "email":
                        IMessageActivity message = Activity.CreateMessageActivity();
                        message.Type       = ActivityTypes.Message;
                        message.Text       = "email \n <br> sent";
                        message.Locale     = "en-Us";
                        message.TextFormat = TextFormatTypes.Plain;
                        await turnContext.SendActivityAsync(message, cancellationToken);

                        /* */
                        return;

                    default:
                        await turnContext.SendActivityAsync("No Action Logic Written for this button", cancellationToken : cancellationToken);

                        break;
                    }

                    String name = results.GetValue("Type").ToString().Trim();
                    //String actionText = results.GetValue("ActionText").ToString();
                    //await turnContext.SendActivityAsync("Respond to user " + actionText, cancellationToken: cancellationToken);

                    // Get Keywords from input field
                    String userInputKeywords = "";
                    //                    if (name == "GetPPT") {
                    if (name == "ViewProfile")
                    {
                        //String DisplayVal = results.GetValue("DisplayText").ToString();
                        //await turnContext.SendActivityAsync(MessageFactory.Text(DisplayVal), cancellationToken);

                        userInputKeywords = "View Profile";

                        AdaptiveCard ViewcardAttachment = null;
                        ViewcardAttachment = AdaptiveCardBotHelper.ViewProfile();

                        var attachment = new Attachment
                        {
                            ContentType = AdaptiveCard.ContentType,
                            Content     = ViewcardAttachment
                        };

                        if (attachment != null)
                        {
                            await turnContext.SendActivityAsync(MessageFactory.Attachment(attachment), cancellationToken);

                            //await turnContext.SendActivityAsync(MessageFactory.Text("Please enter any text to see another card."), cancellationToken);
                        }
                    }
                    else if (name == "UpdateProfile")
                    {
                        userInputKeywords = "Update Profile";

                        AdaptiveCard UpdatecardAttachment = null;
                        UpdatecardAttachment = AdaptiveCardBotHelper.UpdateProfile();

                        var attachment = new Attachment
                        {
                            ContentType = AdaptiveCard.ContentType,
                            Content     = UpdatecardAttachment
                        };

                        if (attachment != null)
                        {
                            await turnContext.SendActivityAsync(MessageFactory.Attachment(attachment), cancellationToken);

                            //await turnContext.SendActivityAsync(MessageFactory.Text("Please enter any text to see another card."), cancellationToken);
                        }

                        //userInputKeywords = results.GetValue("GetUserInputKeywords").ToString();
                    }
                    else if (name == "SendIssue")
                    {
                        AdaptiveCard IssuecardAttachment = null;
                        IssuecardAttachment = AdaptiveCardBotHelper.ReportIssue();

                        var attachment = new Attachment
                        {
                            ContentType = AdaptiveCard.ContentType,
                            Content     = IssuecardAttachment
                        };

                        if (attachment != null)
                        {
                            await turnContext.SendActivityAsync(MessageFactory.Attachment(attachment), cancellationToken);

                            //await turnContext.SendActivityAsync(MessageFactory.Text("Please enter any text to see another card."), cancellationToken);
                        }

                        userInputKeywords = "Report Issue";
                    }
                    else if (name == "Update")
                    {
                        userInputKeywords = "Update Info";
                        userInputKeywords = results.GetValue("GetUserInputKeywords").ToString();
                    }
                    //

                    // Make Http request to api with paramaters
                    //String myUrl = $"http://myurl.com/api/{userInputKeywords}";

                    //...

                    // Respond to user
                    await turnContext.SendActivityAsync("Respond to user" + userInputKeywords, cancellationToken : cancellationToken);
                }
                else
                {
                    //Conversation Text:- hi, "*****@*****.**", "i want to raise an issue", "hardware", "software"
                    turnContext.Activity.RemoveRecipientMention();
                    var text = turnContext.Activity.Text.Trim().ToLower();
                    Body.Text    = text;
                    apiHelperObj = new BotAPIHelper();
                    CreateResponseBody responseBody = apiHelperObj.CreateApiPostCall(Body);
                    if (responseBody != null)
                    {
                        if (responseBody.OutputStack != null && responseBody.OutputStack.Count() > 0)
                        {
                            foreach (var OutputStack in responseBody.OutputStack)
                            {
                                if (!string.IsNullOrEmpty(OutputStack.Text))
                                {
                                    await turnContext.SendActivityAsync(MessageFactory.Text(OutputStack.Text), cancellationToken);

                                    //IMessageActivity message = Activity.CreateMessageActivity();
                                    //message.Type = ActivityTypes.Message;
                                    //message.Text = "your \n <br> text";
                                    //message.Locale = "en-Us";
                                    //message.TextFormat = TextFormatTypes.Plain;
                                    //await turnContext.SendActivityAsync(message);
                                }
                                else
                                {
                                    var mainCard = new AdaptiveCard(new AdaptiveSchemaVersion(1, 0));
                                    if (OutputStack.Data.type == "buttons")
                                    {
                                        //===========================Add DropDownList
                                        //AdaptiveChoiceSetInput choiceSet = AdaptiveCardBotHelper.AddDropDownList();
                                        AdaptiveChoiceSetInput choiceSet = AdaptiveCardHelper.AddDropDownToAdaptiveCard(OutputStack);
                                        mainCard.Body.Add(choiceSet);

                                        //=========================== Add Button
                                        var adaptiveButtonList = AdaptiveCardHelper.AddButtonsToAdaptiveCard(OutputStack);
                                        if (adaptiveButtonList != null && adaptiveButtonList.Count() > 0)
                                        {
                                            mainCard.Body.Add(AdaptiveCardHelper.AddTextBlock(OutputStack.Data._cognigy._default._buttons.text));
                                            mainCard.Actions = adaptiveButtonList;
                                        }
                                        //var card = AdaptiveCardBotHelper.GetCard(OutputStack);
                                    }
                                    if (mainCard != null)
                                    {
                                        var cardAttachment = new Attachment()
                                        {
                                            ContentType = AdaptiveCard.ContentType,
                                            Content     = mainCard,
                                            Name        = "CardName"
                                        };
                                        await turnContext.SendActivityAsync(MessageFactory.Attachment(cardAttachment), cancellationToken);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 2
0
        private async Task SendtProactiveMsgAsync(ITurnContext turnContext, List <TeamsChannelAccount> members, Attachment card, CancellationToken cancellationToken)
        {
            if (!members.Any())
            {
                return;
            }

            var teamsChannelId = turnContext.Activity.TeamsGetChannelId();

            teamsChannelId ??= "msteams";
            var serviceUrl  = turnContext.Activity.ServiceUrl;
            var credentials = new MicrosoftAppCredentials(_appId, _appPassword);

            var conversationParameters = new ConversationParameters
            {
                IsGroup = false,
                Bot     = turnContext.Activity.Recipient,
                Members = new List <ChannelAccount> {
                    members.First()
                },
                TenantId = turnContext.Activity.Conversation.TenantId,
                // TopicName = "Mafia Group"
            };

            var activity = MessageFactory.Attachment(card);

            await((BotFrameworkAdapter)turnContext.Adapter).CreateConversationAsync(
                teamsChannelId,
                serviceUrl,
                credentials,
                conversationParameters,
                async(t1, c1) =>
            {
                ConversationReference conversationReference = t1.Activity.GetConversationReference();
                _conversationReferences.AddOrUpdate(conversationReference.Conversation.Id, conversationReference, (key, newValue) => conversationReference);

                await((BotFrameworkAdapter)turnContext.Adapter).ContinueConversationAsync(
                    _appId,
                    conversationReference,
                    async(t2, c2) =>
                {
                    await t2.SendActivityAsync(activity, c2);
                },
                    cancellationToken);
            },
                cancellationToken);
        }
        public static AdaptiveCard CreateAdaptiveCard_WithDynamicContent()
        {
            List <string> city = new List <string>()
            {
                "Delhi", "Bangalore", "Mumbai"
            };
            List <string> date = new List <string>()
            {
                "1-Jan", "26-Jan", "15-Aug"
            };
            List <string> des = new List <string>()
            {
                "New Year", "Republic Day", "Independence Day"
            };

            List <string> date1 = new List <string>()
            {
                "1-Jan", "26-Jan", "15-Aug", "25-Dec"
            };
            List <string> des1 = new List <string>()
            {
                "New Year", "Republic Day", "Independence Day", "Christmas Day"
            };

            List <string> date2 = new List <string>()
            {
                "1-Jan", "25-Dec"
            };
            List <string> des2 = new List <string>()
            {
                "New Year", "Christmas Day"
            };

            List <AdaptiveCard> cards = new List <AdaptiveCard>();

            cards.Add(HolidayListAdaptiveCard(date, des));
            cards.Add(HolidayListAdaptiveCard(date1, des1));
            cards.Add(HolidayListAdaptiveCard(date2, des2));

            var mainCard = new AdaptiveCard(new AdaptiveSchemaVersion(1, 0));
            var column3  = new AdaptiveColumn();

            column3.Items.Add(new AdaptiveTextBlock()
            {
                Text = "Holiday City", Weight = AdaptiveTextWeight.Bolder
            });
            var columnSet1 = new AdaptiveColumnSet();

            columnSet1.Columns.Add(column3);
            var container1 = new AdaptiveContainer();

            container1.Style = AdaptiveContainerStyle.Emphasis;
            container1.Items.Add(columnSet1);
            mainCard.Body.Add(container1);

            List <AdaptiveShowCardAction> adaptiveShowCardActions = new List <AdaptiveShowCardAction>();

            for (int i = 0; i < city.Count; i++)
            {
                //mainCard.Actions.Add(new AdaptiveShowCardAction() { Title = city[i], Card = cards[i] });
                //mainCard.Actions.Add(new AdaptiveSubmitAction() {Title="Submit",Data= "Submit" });
                mainCard.Actions.Add(new AdaptiveSubmitAction()
                {
                    Title = "Submit", Data = "Submit"
                });
                //{ \"Type\": \"GetPPTNo\" }
                //mainCard.Actions.Add(new AdaptiveOpenUrlAction() { Title = "OpenUrl", Url = new Uri("https://adaptivecards.io/explorer/Action.OpenUrl.html") });
            }

            var attachment = new Attachment
            {
                ContentType = AdaptiveCard.ContentType,
                Content     = mainCard
            };

            return(mainCard);
            //var reply = MessageFactory.Attachment(attachment);
            //await stepContext.Context.SendActivityAsync(reply);
            //return new DialogTurnResult(DialogTurnStatus.Waiting);
        }
        public static AdaptiveCard CreateAdaptiveCard()
        {
            //Activity replyToConversation = message.CreateReply("Should go to conversation");
            //replyToConversation.Attachments = new List<Attachment>();

            AdaptiveCard card = new AdaptiveCard
            {
                // Specify speech for the card.
                Speak = "<s>Your  meeting about \"Adaptive Card design session\"<break strength='weak'/> is starting at 12:30pm</s><s>Do you want to snooze <break strength='weak'/> or do you want to send a late notification to the attendees?</s>"
            };

            // Add text to the card.
            card.Body.Add(new AdaptiveTextBlock()
            {
                Text   = "Adaptive Card design session",
                Size   = AdaptiveTextSize.Large,
                Weight = AdaptiveTextWeight.Bolder
            });

            // Add text to the card.
            card.Body.Add(new AdaptiveTextBlock()
            {
                Text = "Conf Room 112/3377 (10)"
            });

            // Add text to the card.
            card.Body.Add(new AdaptiveTextBlock()
            {
                Text = "12:30 PM - 1:30 PM"
            });

            // Add list of choices to the card.
            card.Body.Add(new AdaptiveChoiceSetInput()
            {
                Id      = "snooze",
                Style   = AdaptiveChoiceInputStyle.Compact,
                Choices = new List <AdaptiveChoice>()
                {
                    new AdaptiveChoice()
                    {
                        Title = "5 minutes", Value = "5", IsSelected = true
                    },
                    new AdaptiveChoice()
                    {
                        Title = "15 minutes", Value = "15"
                    },
                    new AdaptiveChoice()
                    {
                        Title = "30 minutes", Value = "30"
                    }
                }
            });

            // Add buttons to the card.
            card.Actions.Add(new AdaptiveOpenUrlAction()
            {
                Url   = new Uri("http://foo.com"),
                Title = "Snooze"
            });

            card.Actions.Add(new AdaptiveOpenUrlAction()
            {
                Url   = new Uri("http://foo.com"),
                Title = "I'll be late"
            });

            card.Actions.Add(new AdaptiveOpenUrlAction()
            {
                Url   = new Uri("http://foo.com"),
                Title = "Dismiss"
            });

            // Create the attachment.
            Attachment attachment = new Attachment()
            {
                ContentType = AdaptiveCard.ContentType,
                Content     = card
            };

            return(card);
            //replyToConversation.Attachments.Add(attachment);

            //var reply = await connector.Conversations.SendToConversationAsync(replyToConversation);
        }
        protected override async Task OnMessageActivityAsync(ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            if (turnContext.Activity.Type == ActivityTypes.Message)
            {
                // Check if user submitted AdaptiveCard input
                if (turnContext.Activity.Value != null)
                {
                    // Convert String to JObject
                    String  value   = turnContext.Activity.Value.ToString();
                    JObject results = JObject.Parse(value);

                    // Get type from input field
                    String name = results.GetValue("Type").ToString();
                    //String actionText = results.GetValue("ActionText").ToString();
                    //await turnContext.SendActivityAsync("Respond to user " + actionText, cancellationToken: cancellationToken);

                    // Get Keywords from input field
                    String userInputKeywords = "";
                    //                    if (name == "GetPPT") {
                    if (name == "ViewProfile")
                    {
                        //String DisplayVal = results.GetValue("DisplayText").ToString();
                        //await turnContext.SendActivityAsync(MessageFactory.Text(DisplayVal), cancellationToken);

                        userInputKeywords = "View Profile";

                        AdaptiveCard ViewcardAttachment = null;
                        ViewcardAttachment = ViewProfile();

                        var attachment = new Attachment
                        {
                            ContentType = AdaptiveCard.ContentType,
                            Content     = ViewcardAttachment
                        };

                        if (attachment != null)
                        {
                            await turnContext.SendActivityAsync(MessageFactory.Attachment(attachment), cancellationToken);

                            //await turnContext.SendActivityAsync(MessageFactory.Text("Please enter any text to see another card."), cancellationToken);
                        }
                    }
                    else if (name == "UpdateProfile")
                    {
                        userInputKeywords = "Update Profile";

                        AdaptiveCard UpdatecardAttachment = null;
                        UpdatecardAttachment = UpdateProfile();

                        var attachment = new Attachment
                        {
                            ContentType = AdaptiveCard.ContentType,
                            Content     = UpdatecardAttachment
                        };

                        if (attachment != null)
                        {
                            await turnContext.SendActivityAsync(MessageFactory.Attachment(attachment), cancellationToken);

                            //await turnContext.SendActivityAsync(MessageFactory.Text("Please enter any text to see another card."), cancellationToken);
                        }

                        //userInputKeywords = results.GetValue("GetUserInputKeywords").ToString();
                    }
                    else if (name == "SendIssue")
                    {
                        AdaptiveCard IssuecardAttachment = null;
                        IssuecardAttachment = ReportIssue();

                        var attachment = new Attachment
                        {
                            ContentType = AdaptiveCard.ContentType,
                            Content     = IssuecardAttachment
                        };

                        if (attachment != null)
                        {
                            await turnContext.SendActivityAsync(MessageFactory.Attachment(attachment), cancellationToken);

                            //await turnContext.SendActivityAsync(MessageFactory.Text("Please enter any text to see another card."), cancellationToken);
                        }

                        userInputKeywords = "Report Issue";
                    }
                    else if (name == "Update")
                    {
                        userInputKeywords = "Update Info";
                        userInputKeywords = results.GetValue("GetUserInputKeywords").ToString();
                    }
                    //

                    // Make Http request to api with paramaters
                    //String myUrl = $"http://myurl.com/api/{userInputKeywords}";

                    //...

                    // Respond to user
                    await turnContext.SendActivityAsync("Respond to user" + userInputKeywords, cancellationToken : cancellationToken);
                }
                else
                {
                    //// Send user AdaptiveCard
                    ////var cardAttachment = GetUserInputForCustomPPT();
                    ////var reply = turnContext.Activity.CreateReply();
                    ////reply.Attachments = new List<Attachment>() { cardAttachment };
                    ////await turnContext.SendActivityAsync(reply, cancellationToken);

                    //var cardAttachment = GetUserInputForCustomPPT();
                    ////await turnContext.SendActivityAsync(MessageFactory.Attachment(cardAttachment), cancellationToken);
                    //await turnContext.SendActivityAsync(MessageFactory.Attachment(cardAttachment), cancellationToken);
                    //await turnContext.SendActivityAsync(MessageFactory.Text("Please enter any text to see another card."), cancellationToken);

                    //2.
                    turnContext.Activity.RemoveRecipientMention();
                    var        text           = turnContext.Activity.Text.Trim().ToLower();
                    var        attachments    = new List <Attachment>();
                    var        reply          = MessageFactory.Attachment(attachments);
                    Attachment cardAttachment = null;
                    //if (text.Contains("testdc"))
                    if (text.Contains("hi"))
                    {
                        RequestBody        Body         = CreateBody(text);
                        CreateResponseBody responseBody = CreateAPIPostCall(Body);
                        if (responseBody != null)
                        {
                            await turnContext.SendActivityAsync(MessageFactory.Text(responseBody.Text), cancellationToken);
                        }
                    }

                    if (text.Contains("*****@*****.**") || text.Contains("i want to raise an issue"))
                    {
                        RequestBody        Body         = CreateBody(text);
                        CreateResponseBody responseBody = CreateAPIPostCall(Body);
                        if (responseBody != null)
                        {
                            string test = string.Empty;
                            if (responseBody.OutputStack != null && responseBody.OutputStack.Count() > 0)
                            {
                                foreach (var OutputStack in responseBody.OutputStack)
                                {
                                    if (!string.IsNullOrEmpty(OutputStack.Text))
                                    {
                                        test = OutputStack.Text;
                                        await turnContext.SendActivityAsync(MessageFactory.Text(test), cancellationToken);
                                    }
                                    else
                                    {
                                        //if(OutputStack.Data._cognigy._default._buttons.type=="buttons")
                                        if (OutputStack.Data.type == "buttons")
                                        {
                                            string btnText = OutputStack.Data._cognigy._default._buttons.text;
                                            if (OutputStack.Data._cognigy._default._buttons.buttons != null && OutputStack.Data._cognigy._default._buttons.buttons.Count() > 0)
                                            {
                                                var mainCard = new AdaptiveCard(new AdaptiveSchemaVersion(1, 0));
                                                //add textblock to the button
                                                // Add text to the card.
                                                mainCard.Body.Add(AdaptiveCardHelper.AddTextBlock(btnText));

                                                foreach (var button in OutputStack.Data._cognigy._default._buttons.buttons)
                                                {
                                                    string btnActionText = button.title;
                                                    //double btnId = button.id;
                                                    string btnId      = Guid.NewGuid().ToString();
                                                    string btnpayload = button.payload;

                                                    mainCard.Actions.Add(AddButtonToAdaptiveCard(btnId, btnActionText, btnpayload));
                                                }
                                                cardAttachment = new Attachment()
                                                {
                                                    ContentType = AdaptiveCard.ContentType,
                                                    Content     = mainCard
                                                };
                                                if (cardAttachment != null)
                                                {
                                                    await turnContext.SendActivityAsync(MessageFactory.Attachment(cardAttachment), cancellationToken);

                                                    //await turnContext.SendActivityAsync(MessageFactory.Text("Please enter any text to see another card."), cancellationToken);
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                            //await turnContext.SendActivityAsync(MessageFactory.Text(test), cancellationToken);

                            //AdaptiveCard card = CreateAdaptiveCard_WithDynamicContent1();
                            AdaptiveCard card = DisplayActionCards();
                            cardAttachment = new Attachment()
                            {
                                ContentType = AdaptiveCard.ContentType,
                                Content     = card
                            };
                            reply.Attachments.Add(cardAttachment);
                        }
                    }
                    if (cardAttachment != null)
                    {
                        await turnContext.SendActivityAsync(MessageFactory.Attachment(cardAttachment), cancellationToken);

                        //await turnContext.SendActivityAsync(MessageFactory.Text("Please enter any text to see another card."), cancellationToken);
                    }
                }
            }
        }
        public static Attachment GetUserInputForCustomPPT()
        {
            AdaptiveCard card = new AdaptiveCard();

            card.Body.Add(new AdaptiveTextBlock()
            {
                Text = "Do you want to apply filter and customise the PPT?",
                Wrap = true,
                Size = AdaptiveTextSize.Small
            });

            card.Body.Add(new AdaptiveContainer()
            {
                Id           = "getCustomPPTNo",
                SelectAction = new AdaptiveSubmitAction()
                {
                    Id       = "getCustomPPTNo",
                    Title    = "No",
                    DataJson = "{ \"Type\": \"GetCustomPPT\" }",
                }
            });

            card.Body.Add(new AdaptiveContainer()
            {
                Id    = "getCustomPPTYes",
                Items = new List <AdaptiveElement>()
                {
                    new AdaptiveTextBlock()
                    {
                        Text = "Please select an option",
                        Wrap = true,
                        Size = AdaptiveTextSize.Small
                    }
                }
            });


            card.Actions.Add(new AdaptiveShowCardAction()
            {
                Id    = "GetPPTYes",
                Title = "Yes",
                Card  = new AdaptiveCard()
                {
                    Body = new List <AdaptiveElement>()
                    {
                        new AdaptiveTextBlock()
                        {
                            Text = "Please enter your input",
                            Wrap = true
                        },
                        new AdaptiveTextInput()
                        {
                            Id          = "GetUserInputKeywords",
                            Placeholder = "Please enter the keyword list separated by ',' Ex:RPA,FS ",
                            MaxLength   = 490,
                            IsMultiline = true
                        }
                    },
                    Actions = new List <AdaptiveAction>()
                    {
                        new AdaptiveSubmitAction()
                        {
                            Id       = "contactSubmit",
                            Title    = "Submit",
                            DataJson = "{ \"Type\": \"GetPPT\" }"
                        },
                        new AdaptiveOpenUrlAction()
                        {
                            Id  = "CallApi",
                            Url = new Uri("https://xyz" + "RPA")
                                  //card.Actions.Card.AdaptiveTextInput.Placeholder
                        }
                    }
                }
            });

            card.Actions.Add(new AdaptiveShowCardAction()
            {
                Id    = "GetPPTNo",
                Title = "No",
                Card  = new AdaptiveCard()
                {
                    Body = new List <AdaptiveElement>()
                    {
                    },
                    Actions = new List <AdaptiveAction>()
                    {
                        new AdaptiveSubmitAction()
                        {
                            Id       = "contactSubmit",
                            Title    = "Submit",
                            DataJson = "{ \"Type\": \"GetPPTNo\" }"
                        }
                    }
                }
            });


            // Create the attachment with adapative card.
            Attachment attachment = new Attachment()
            {
                ContentType = AdaptiveCard.ContentType,
                Content     = card
            };

            return(attachment);
        }