예제 #1
0
        /// <summary>
        /// Creates a new <see cref="TokenPair"/> from an ADAL <see cref="IdentityModel.Clients.ActiveDirectory.AuthenticationResult"/>.
        /// </summary>
        /// <param name="authResult">
        /// A successful <see cref="IdentityModel.Clients.ActiveDirectory.AuthenticationResult"/>
        /// which contains both access and refresh token data.
        /// </param>
        public TokenPair(IdentityModel.Clients.ActiveDirectory.AuthenticationResult authResult)
        {
            Debug.Assert(authResult != null, "The authResult parameter is null.");
            Debug.Assert(!String.IsNullOrWhiteSpace(authResult.AccessToken), "The authResult.AccessToken parameter is null or invalid.");
            Debug.Assert(!String.IsNullOrWhiteSpace(authResult.RefreshToken), "The authResult.RefreshToken parameter is null or invalid.");
            Debug.Assert(authResult.ExpiresOn > DateTimeOffset.UtcNow, "The authResult is expired and invalid.");

            this.AccessToken = new Token(authResult, TokenType.Access);
            this.RefeshToken = new Token(authResult, TokenType.Refresh);
        }
예제 #2
0
파일: QnABot.cs 프로젝트: ukk77/bot_repo
        protected override async Task OnMessageActivityAsync(ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            var welcomeUserStateAccessor = UserState.CreateProperty <WelcomeUserState>(nameof(WelcomeUserState));
            var didBotWelcomeUser        = await welcomeUserStateAccessor.GetAsync(turnContext, () => new WelcomeUserState());

            var options = new QnAMakerOptions {
                Top = 1
            };

            var text = "";

            if (turnContext.Activity.Text == null)
            {
                text = "null";
            }
            else
            {
                text = turnContext.Activity.Text.ToLowerInvariant();
            }
            switch (text)
            {
            case "intro":
            case "help":
                await SendIntroCardAsync(turnContext, cancellationToken);

                break;

            case "file an incident":
                await turnContext.SendActivityAsync(MessageFactory.Attachment(CreateAdaptiveCardAttachment()));

                break;

            case "null":
                if (turnContext.Activity.Value != null)
                {
                    var ticket     = JsonConvert.SerializeObject(turnContext.Activity.Value);
                    var ticket_num = 123456789;

                    JObject jObject = JObject.Parse(ticket);

                    string t_date    = (string)jObject.SelectToken("dateinput");
                    string t_name    = (string)jObject.SelectToken("nameinput");
                    string t_issue   = (string)jObject.SelectToken("issueinput");
                    string t_urgency = "";
                    if ((int)jObject.SelectToken("urgencyinput") == 1)
                    {
                        t_urgency = "Normal";
                    }
                    else
                    {
                        t_urgency = "Urgent";
                    }

                    try {
                        string resourceId = "https://graph.microsoft.com/";
                        string authString = "https://login.microsoftonline.com/" + "8ad25bda-a157-4eda-a940-b1069931e221";
                        //secret - BO.7~0daznFl84x-3B0_79X9OMMpwH.UYM
                        //tenant id- 8ad25bda-a157-4eda-a940-b1069931e221
                        //app id - e6679829-27ac-4d4e-ae24-f752ab77755a

                        var authenticationContext = new AuthenticationContext(authString, false);
                        IdentityModel.Clients.ActiveDirectory.ClientCredential     clientCred           = new IdentityModel.Clients.ActiveDirectory.ClientCredential("e6679829-27ac-4d4e-ae24-f752ab77755a", "BO.7~0daznFl84x-3B0_79X9OMMpwH.UYM");
                        IdentityModel.Clients.ActiveDirectory.AuthenticationResult authenticationResult = await authenticationContext.AcquireTokenAsync(resourceId, clientCred);

                        var acc = "eyJ0eXAiOiJKV1QiLCJub25jZSI6Ijg1SEkzRE5Ob3ZERDFRb2dhcDdQaEczTk1qTVJhbUlXZEoxWTNsWjltZEEiLCJhbGciOiJSUzI1NiIsIng1dCI6IlNzWnNCTmhaY0YzUTlTNHRycFFCVEJ5TlJSSSIsImtpZCI6IlNzWnNCTmhaY0YzUTlTNHRycFFCVEJ5TlJSSSJ9.eyJhdWQiOiIwMDAwMDAwMy0wMDAwLTAwMDAtYzAwMC0wMDAwMDAwMDAwMDAiLCJpc3MiOiJodHRwczovL3N0cy53aW5kb3dzLm5ldC82NTgzNjM2Yi1kMTU2LTQ5MmUtODZjZS1iOGZjY2I3OTBkZjEvIiwiaWF0IjoxNTkxOTc4NzAxLCJuYmYiOjE1OTE5Nzg3MDEsImV4cCI6MTU5MTk4MjYwMSwiYWNjdCI6MCwiYWNyIjoiMSIsImFpbyI6IjQyZGdZTkR5NkM2YVdkWDMrOGJFNjJ0U0h3bTJSYmIyMWt6VD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CPMFsVcsFcl2qQnXcnKxLeKl4LZexLFD-wlk5a7rJCNhrp92k3guxrLmFXF0z398ksu_QcP1lc0na___WQOjOB4wmB1ak-1-6nAvedjSQfSV6RwCGFRCpkYLQIR6BsjN3agE9h88ik9covtAi-QYQNgooqaxF4OmjJWGv-6QvijhtrLMZXsoU2ev3giQ0bwsHmqN4gp3Amc1S7gFoTgCR4qIjncOUxZ_KBdQA22M09U3t-SMAiYKAvHV_EMxA8yPtVKTh5bVd_tf-obuad4hTgRmS2clmWYom1TjIocA";
                        GraphServiceClient graphClient = new GraphServiceClient(new DelegateAuthenticationProvider(
                                                                                    (requestMessage) => {
                            requestMessage.Headers.Authorization = new AuthenticationHeaderValue("Bearer", acc);
                            return(Task.FromResult(0));
                        }));

                        AdaptiveCard card = new AdaptiveCard(new AdaptiveSchemaVersion(1, 0))
                        {
                            Body = new List <AdaptiveElement>()
                            {
                                new AdaptiveContainer
                                {
                                    Items = new List <AdaptiveElement>()
                                    {
                                        new AdaptiveColumnSet()
                                        {
                                            Columns = new List <AdaptiveColumn>()
                                            {
                                                new AdaptiveColumn {
                                                    Width = AdaptiveColumnWidth.Auto,
                                                    Items = new List <AdaptiveElement>()
                                                    {
                                                        new AdaptiveTextBlock {
                                                            Text = "Ticket Date " + t_date,
                                                            Size = AdaptiveTextSize.Small,
                                                        },
                                                        new AdaptiveTextBlock {
                                                            Text = "Ticket By " + t_name,
                                                            Size = AdaptiveTextSize.Small
                                                        },
                                                        new AdaptiveTextBlock {
                                                            Text = "Issue : " + t_issue,
                                                            Size = AdaptiveTextSize.Small
                                                        },
                                                        new AdaptiveTextBlock {
                                                            Text = "Assistance urgency : " + t_urgency,
                                                            Size = AdaptiveTextSize.Small
                                                        },
                                                        new AdaptiveTextBlock {
                                                            Text = "Ticket number : " + ticket_num,
                                                            Size = AdaptiveTextSize.Small
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            },
                            Actions = new List <AdaptiveAction>()
                            {
                                new AdaptiveSubmitAction
                                {
                                    Title = "Resolved",
                                    Id    = "resolvedticket",
                                    Data  = new AdaptiveShowCardAction {
                                    }
                                }
                            }
                        };

                        string  cardstr  = card.ToJson();
                        JObject cardobj  = JObject.Parse(cardstr);
                        var     cardType = "application/adaptivecard+json";
                        //cardobj.SelectToken("type");
                        var content = string.Format(File.ReadAllText(@".\Message.html"), cardType, cardobj.ToString());

                        var message = new Message
                        {
                            Subject = "Ticket request",
                            Body    = new ItemBody
                            {
                                ContentType = BodyType.Html,
                                Content     = content
                            },

                            Attachments = new MessageAttachmentsCollectionPage(),

                            ToRecipients = new List <Recipient>()
                            {
                                new Recipient
                                {
                                    EmailAddress = new EmailAddress
                                    {
                                        Address = "*****@*****.**"
                                    }
                                }
                            }
                        };
                        await graphClient.Users["*****@*****.**"]
                        .SendMail(message, null)
                        .Request()
                        .PostAsync();
                    }
                    catch (MsalException)
                    {
                        await turnContext.SendActivityAsync($"broken");
                    }
                }
                else
                {
                    await turnContext.SendActivityAsync($"No Input");
                }
                break;

            case "thank you":
            case "bye":
                await turnContext.SendActivityAsync($"Bye");

                break;

            default:
                var qnaMaker = _services.QnAMakerService;
                var response = await qnaMaker.GetAnswersAsync(turnContext, options);

                string[] split_response = (response[0].Answer).Split(';');
                if (split_response.Length > 1)
                {
                    var card = new HeroCard();
                    card.Title = split_response[0];
                    card.Text  = split_response[1];
                    if (split_response.Length == 3)
                    {
                        card.Buttons = new List <CardAction>()
                        {
                            new CardAction(ActionTypes.OpenUrl, split_response[0], null, split_response[0], split_response[0], split_response[2]),
                        };
                    }
                    await turnContext.SendActivityAsync(MessageFactory.Attachment(card.ToAttachment()));
                }
                else
                {
                    await turnContext.SendActivityAsync(split_response[0]);
                }

                break;
            }
            await UserState.SaveChangesAsync(turnContext);
        }