Exemplo n.º 1
0
        public LeadDialog(IConfiguration configuration, UserState userState)
            : base(nameof(LeadDialog))
        {
            _prospectProperty = userState.CreateProperty <Prospect>("ProspectCustomer");
            _configuration    = configuration;

            cRMCredentials = new CRMCredentials()
            {
                ClientID     = _configuration["clientId"],
                ServiceUrl   = _configuration["serviceUrl"],
                AuthpointEnd = _configuration["authEndpoint"],
                RedirectUrl  = _configuration["redirectUrl"],
                Key          = _configuration["key"]
            };

            // This array defines how the Waterfall will execute.
            var waterfallSteps = new WaterfallStep[]
            {
                NameStepAsync,
                PhoneNumberStepAsync,
                EmailAsync,
                ConfirmAsync,
                SummaryAsync
            };

            // Add named dialogs to the DialogSet. These names are saved in the dialog state.
            AddDialog(new WaterfallDialog(nameof(WaterfallDialog), waterfallSteps));
            AddDialog(new TextPrompt(nameof(TextPrompt)));
            AddDialog(new ConfirmPrompt(nameof(ConfirmPrompt)));

            // The initial child Dialog to run.
            InitialDialogId = nameof(WaterfallDialog);
        }
Exemplo n.º 2
0
        private static BotServices InitBotServices(BotConfiguration config)
        {
            CRMCredentials cRMCredentials = new CRMCredentials();

            foreach (var service in config.Services)
            {
                switch (service.Type)
                {
                case ServiceTypes.Generic:
                {
                    var crmService = service as GenericService;
                    cRMCredentials.ServiceUrl   = crmService.Configuration["serviceUrl"];
                    cRMCredentials.ClientID     = crmService.Configuration["clientId"];
                    cRMCredentials.Key          = crmService.Configuration["key"];
                    cRMCredentials.RedirectUrl  = crmService.Configuration["redirectUrl"];
                    cRMCredentials.AuthpointEnd = crmService.Configuration["authEndpoint"];
                    break;
                }
                }
            }

            return(new BotServices(cRMCredentials));
        }
Exemplo n.º 3
0
 public BotServices(CRMCredentials credentials)
 {
     CRMUser = credentials;
 }
        public async static Task <string> GenerateLead(Prospect prospect, CRMCredentials crmUser)
        {
            HttpMessageHandler messageHandler = null;
            Version            webAPIVersion  = new Version(9, 0);

            //One message handler for OAuth authentication, and the other for Windows integrated
            // authentication.  (Assumes that HTTPS protocol only used for CRM Online.)
            if (crmUser.ServiceUrl.StartsWith("https://"))
            {
                messageHandler = new OAuthMessageHandler(crmUser.ServiceUrl, crmUser.ClientID, crmUser.RedirectUrl, crmUser.AuthpointEnd, crmUser.Key,
                                                         new HttpClientHandler());
            }

            try
            {
                //Create an HTTP client to send a request message to the CRM Web service.
                using (HttpClient httpClient = new HttpClient(messageHandler))
                {
                    //Specify the Web API address of the service and the period of time each request
                    // has to execute.
                    httpClient.BaseAddress = new Uri(crmUser.ServiceUrl);
                    httpClient.Timeout     = new TimeSpan(0, 2, 0); //2 minutes

                    httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
                    httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
                    httpClient.DefaultRequestHeaders.Accept.Add(
                        new MediaTypeWithQualityHeaderValue("application/json"));

                    // Get the current userId of the service account (owner incase of Lead)
                    HttpResponseMessage whoAmIResponse = await httpClient.GetAsync("api/data/v9.0/WhoAmI");

                    Guid userId;
                    if (whoAmIResponse.IsSuccessStatusCode)
                    {
                        JObject jWhoAmIResponse =
                            JObject.Parse(whoAmIResponse.Content.ReadAsStringAsync().Result);
                        userId = (Guid)jWhoAmIResponse["UserId"];

                        // Populate the fields of prospect customer (lead)
                        JObject lead = new JObject();
                        lead.Add("subject", "Lead from the Event Bot");
                        lead.Add("lastname", prospect.Name);
                        lead.Add("mobilephone", prospect.PhoneNumber);
                        lead.Add("emailaddress1", prospect.Email);
                        lead.Add("*****@*****.**", $"/systemusers({userId})");

                        // Create request and response
                        HttpRequestMessage createRequest = new HttpRequestMessage(HttpMethod.Post, "api/data/v9.0/leads");
                        createRequest.Content = new StringContent(lead.ToString(), Encoding.UTF8, "application/json");
                        HttpResponseMessage createResponse = await httpClient.SendAsync(createRequest);

                        // Check if the status is good to go (204)
                        if (createResponse.StatusCode == HttpStatusCode.NoContent)
                        {
                            return(Helpers.Common.GenerateReferenceID(6));
                        }
                        else
                        {
                            return($"Failed to register {createResponse.StatusCode}");
                        }
                    }
                    else
                    {
                        return($"Failed - {whoAmIResponse.StatusCode}");
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }