예제 #1
0
    static void Main(string[] args)
    {
        // Find your Account Sid and Auth Token at twilio.com/user/account
        string AccountSid   = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        string AuthToken    = "your_auth_token";
        string WorkspaceSid = "WSXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        string WorkerSid    = "WKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
        var    client       = new TaskRouterClient(AccountSid, AuthToken);

        Worker worker = client.UpdateWorker(WorkspaceSid, WorkerSid, "", "{\"type\":\"support\"}", null);

        Console.WriteLine(worker.FriendlyName);
        Console.WriteLine(worker.Attributes);

        // update a worker attributes with a dictionary to map to json
        Dictionary <string, string> attributes = new Dictionary <string, string>();

        attributes.Add("type", "support");
        worker = client.UpdateWorker(WorkspaceSid, WorkerSid, attributes, null);
        Console.WriteLine(worker.FriendlyName);
        Console.WriteLine(worker.Attributes);
    }
예제 #2
0
        public async Task <ApiResponse <CreateAgentResponse> > CreateAgentAsync(CreateAgentRequest request)
        {
            try
            {
                var now = DateTime.UtcNow;

                if (request == null || !AgentIsValid(request))
                {
                    return(new ApiResponse <CreateAgentResponse>(null)
                    {
                        HasError = true,
                        Message = "Insufficient agent information was sent.",
                        StatusCode = StatusCodes.BadRequest
                    });
                }

                if (EmailHasAlias(request))
                {
                    return(new ApiResponse <CreateAgentResponse>(null)
                    {
                        HasError = true,
                        Message = "The email address should not contain an agent or customer alias.",
                        StatusCode = StatusCodes.BadRequest
                    });
                }

                var customerEmail = AddEmailAlias(request.Email, "+customer");
                var agentEmail    = AddEmailAlias(request.Email, "+agent");

                if (customerEmail == null || agentEmail == null)
                {
                    return(new ApiResponse <CreateAgentResponse>(null)
                    {
                        HasError = true,
                        Message = "The email address was invalid.",
                        StatusCode = StatusCodes.BadRequest
                    });
                }

                var taskRouter = new TaskRouterClient(appSettings.Get("twilio:AccountSID"), appSettings.Get("twilio:AuthToken"));

                var worker = taskRouter.AddWorker(
                    appSettings.Get("twilio:WorkspaceSID"),
                    $"Agent {request.FirstName} {request.LastName}",
                    appSettings.Get("twilio:IdleActivitySID"),
                    "{}");

                if (worker.RestException != null)
                {
                    return(new ApiResponse <CreateAgentResponse>(null)
                    {
                        HasError = true,
                        Message = worker.RestException.Message,
                        StatusCode = StatusCodes.BadRequest
                    });
                }

                worker.Attributes = JsonConvert.SerializeObject(new
                {
                    skills = new[] { "voice", "sms", "messaging" },
                    sid    = worker.Sid
                });

                worker = taskRouter.UpdateWorker(
                    worker.WorkspaceSid,
                    worker.Sid,
                    worker.ActivitySid,
                    worker.Attributes,
                    worker.FriendlyName);

                if (worker.RestException != null)
                {
                    return(new ApiResponse <CreateAgentResponse>(null)
                    {
                        HasError = true,
                        Message = worker.RestException.Message,
                        StatusCode = StatusCodes.BadRequest
                    });
                }

                var auth0Agent = await auth0.CreatePasswordUserAsync(agentEmail, request.Password);

                var auth0Customer = await auth0.CreatePasswordUserAsync(customerEmail, request.Password);

                if (auth0Customer.InvalidRequest || auth0Agent.InvalidRequest)
                {
                    return(new ApiResponse <CreateAgentResponse>(null)
                    {
                        HasError = true,
                        Message = "Error creating auth0 users.",
                        StatusCode = StatusCodes.BadRequest
                    });
                }

                var customer = new Customer
                {
                    IdentityID   = auth0Customer.UserId,
                    FirstName    = request.FirstName,
                    LastName     = request.LastName,
                    Address      = CreateDefaultAddress(),
                    PhoneNumber  = request.PhoneNumber,
                    ValueLevel   = CustomerValueLevel.Gold,
                    CreatedDate  = now,
                    ModifiedDate = now,
                    IsActive     = true
                };

                var agent = new Agent
                {
                    IdentityID     = auth0Agent.UserId,
                    SID            = worker.Sid,
                    FirstName      = "Agent",
                    LastName       = request.LastName,
                    CreatedDate    = now,
                    ModifiedDate   = now,
                    IsActive       = true,
                    PairedCustomer = customer
                };

                CreateSeedData(customer);
                context.Agents.Add(agent);
                await context.SaveChangesAsync();

                // Send a hockey app invite
                using (var client = new HttpClient())
                {
                    try
                    {
                        var appId     = appSettings.Get("hockeyApp:AppID");
                        var appSecret = appSettings.Get("hockeyApp:Secret");
                        client.BaseAddress = new Uri("https://rink.hockeyapp.net/");
                        client.DefaultRequestHeaders.Add("X-HockeyAppToken", appSecret);
                        var msg = $"Hello {request.FirstName},\n" +
                                  $"An account for Owl Finance has been created for you.\n\n" +
                                  $"The webpage is https://YOUR-WEBPAGE-URL.com\n" +
                                  $"Your username for the mobile app is {customerEmail}. The password is {request.Password}.\n" +
                                  $"Your username for the web site is {agentEmail}. The password is {request.Password}.\n\n" +
                                  $"Please contact YOUR-EMAIL with any questions!\n" +
                                  $"Thanks";
                        var pairs = new List <KeyValuePair <string, string> >
                        {
                            new KeyValuePair <string, string>("first_name", request.FirstName),
                            new KeyValuePair <string, string>("last_name", request.LastName),
                            new KeyValuePair <string, string>("email", request.Email),
                            new KeyValuePair <string, string>("message", msg)
                        };
                        var content  = new FormUrlEncodedContent(pairs);
                        var response = await client.PostAsync($"api/2/apps/{appId}/app_users", content);

                        if (!response.IsSuccessStatusCode)
                        {
                            return(new ApiResponse <CreateAgentResponse>(null)
                            {
                                HasError = true,
                                Message = "Error creating hockey app invite.",
                                StatusCode = StatusCodes.BadRequest
                            });
                        }
                    }
                    catch (Exception)
                    {
                        // do nothing
                    }
                }

                var successResponse = new ApiResponse <CreateAgentResponse>(new CreateAgentResponse())
                {
                    HasError = false,
                    Message  = "Your account has been created. Please check your email.",
                };

                return(successResponse);
            }
            catch (Exception e)
            {
                return(HandleErrorAndReturnStatus <ApiResponse <CreateAgentResponse> >(e));
            }
        }