Exemple #1
0
        public override async Task <JObject> Process(JObject connectEvent, ILambdaContext context)
        {
            dynamic result = new JObject();

            try
            {
                dynamic newSessionParameters = new JObject();
                var     customerNumber       =
                    (string)connectEvent.SelectToken("Details.ContactData.CustomerEndpoint.Address");
                var customerNumberType = (string)connectEvent.SelectToken("Details.ContactData.CustomerEndpoint.Type");

                if (customerNumberType.Equals("TELEPHONE_NUMBER"))
                {
                    var callerId = SwitchCallerId(customerNumber);
                    newSessionParameters.callerId        = callerId;
                    newSessionParameters.callId          = (string)connectEvent.SelectToken("Details.ContactData.ContactId");
                    newSessionParameters.immediateReturn = true;
                    context.Logger.LogLine("CreateSession request for " + callerId);
                }
                else
                {
                    var failureReason = "no caller id found";
                    context.Logger.LogLine($"CreateSession - {failureReason}");
                    result.LambdaResult  = false;
                    result.FailureReason = failureReason;
                    return(result);
                }

                if (smsWorkflow != null)
                {
                    context.Logger.LogLine("CreateSessionWithSms - Attempt to send SMS with client url");
                    var smsResponse = await smsWorkflow.Process(connectEvent, context);

                    var smsSent = (bool)smsResponse["LambdaResult"];
                    result.SmsSent = smsSent;
                }

                using (var response = await _ApiClient.PostAsync("sessions",
                                                                 new StringContent(newSessionParameters.ToString(), Encoding.UTF8, "application/json")))
                {
                    result.LambdaResult =
                        response.StatusCode == HttpStatusCode.Accepted ||
                        response.StatusCode == HttpStatusCode.Created;

                    if (response.IsSuccessStatusCode)
                    {
                        if (response.StatusCode == HttpStatusCode.Accepted)
                        {
                            var body     = JObject.Parse(await response.Content.ReadAsStringAsync());
                            var queryUrl = (string)body["QueryUrl"];
                            result.ConnectStartTime = DateTime.Now;
                            result.QueryUrl         = MakeRelativeUri(queryUrl);
                            context.Logger.LogLine("CreateSession - Session create started, return immediately");
                            context.Logger.LogLine($"CreateSession - absolute query url = '{queryUrl}', QueryUrl parameter = '{(string)result["QueryUrl"]}'");
                        }
                        else if (response.StatusCode == HttpStatusCode.Created)
                        {
                            var session =
                                JsonConvert.DeserializeObject <SessionResource>(
                                    await response.Content.ReadAsStringAsync());
                            AddSessionToResult(result, session);
                            result.SessionStatus = session.Status;
                            context.Logger.LogLine($"CreateSession - session {session.SessionId} created");
                        }
                        else
                        {
                            await RequestFailed(response, result, context);
                        }
                    }
                    else
                    {
                        await RequestFailed(response, result, context);
                    }
                }
            }
            catch (InvalidOperationException ex)
            {
                RequestException(ex, "Error occurred when making API request - ", result, context);
            }
            catch (ArgumentException ex)
            {
                RequestException(ex, "Argument error - ", result, context);
            }
            catch (UriFormatException ex)
            {
                RequestException(ex, "Bad session uri - ", result, context);
            }
            catch (JsonException ex)
            {
                RequestException(ex, "Error reading the Connect event - ", result, context);
            }

            context.Logger.LogLine("CreateSession - result:\n" + result);
            return(result);
        }
        public async Task <JObject> FunctionHandler(JObject connectEvent, ILambdaContext context)
        {
            context.Logger.LogLine("Connect event:\n" + connectEvent);

            var requestName = (string)connectEvent.SelectToken("Details.Parameters.RequestName") ?? "(null)";

            dynamic invalidApiError = new JObject();

            if (!TwilioValid || !ChoiceViewValid)
            {
                invalidApiError.LambdaResult  = false;
                invalidApiError.FailureResult =
                    ChoiceViewValid ? "Not connected to Twilio." : "Not connected to ChoiceView.";
            }

            switch (requestName)
            {
            case "GetPhoneNumberType":
                return(await(getPhoneNumberType != null ? getPhoneNumberType.Process(connectEvent, context) : invalidApiError));

            case "SendSms":
                return(await(sendSms != null ? sendSms.Process(connectEvent, context) : invalidApiError));

            case "CreateSession":
                return(await(createSession != null ? createSession.Process(connectEvent, context) : invalidApiError));

            case "CreateSessionWithSms":
                return(await(createSessionWithSms != null ? createSessionWithSms.Process(connectEvent, context) : invalidApiError));

            case "GetSession":
                return(await(getSession != null ? getSession.Process(connectEvent, context) : invalidApiError));

            case "TransferSession":
                return(await(transferSession != null ? transferSession.Process(connectEvent, context) : invalidApiError));

            case "QuerySession":
                return(await(querySession != null ? querySession.Process(connectEvent, context) : invalidApiError));

            case "EndSession":
                return(await(endSession != null ? endSession.Process(connectEvent, context) : invalidApiError));

            case "SendUrl":
                return(await(sendUrl != null ? sendUrl.Process(connectEvent, context) : invalidApiError));

            case "GetControlMessage":
                return(await(getControlMessage != null ? getControlMessage.Process(connectEvent, context) : invalidApiError));

            case "ClearControlMessage":
                return(await(clearControlMessage != null ? clearControlMessage.Process(connectEvent, context) : invalidApiError));

            case "AddProperty":
                return(await(addProperty != null ? addProperty.Process(connectEvent, context) : invalidApiError));

            case "GetProperties":
                return(await(getProperties != null ? getProperties.Process(connectEvent, context) : invalidApiError));

            default:
                context.Logger.LogLine("Unknown request " + requestName);
                return(new JObject(new JProperty("LambdaResult", false),
                                   new JProperty("FailureReason", "Unknown request")));
            }
        }