예제 #1
0
        public static async Task <IActionResult> Reset(
            [OrchestrationTrigger] IDurableOrchestrationContext context,
            [Table(nameof(SlideShow))] CloudTable slideShowTable,
            [Table(nameof(User))] CloudTable userTable,
            ILogger log)
        {
            var twilioResponse  = context.GetInput <TwilioResponse>();
            var slideShowClient = new DataAccess <SlideShow>(slideShowTable);
            var userClient      = new DataAccess <User>(userTable);

            var user = await userClient.GetAsync(nameof(User), twilioResponse.From);

            if (user != null) // Verify user
            {
                var slideShow = await slideShowClient.GetAsync(nameof(SlideShow), $"{user.PhoneNumber}{user.PIN}");

                if (slideShow != null)
                {
                    var images = slideShow.Medias.Split(FEED_DELIMITER).Where(media => !string.IsNullOrWhiteSpace(media));

                    slideShow.Medias = "";
                    await slideShowClient.ReplaceAsync(slideShow);

                    log.Log(LogLevel.Information, $"Media resetted for user {twilioResponse.From}");
                }

                TwilioUtil.Notify(user.RowKey, TwilioMessages.ResettedAccount, log);
            }
            return(new OkObjectResult(true));
        }
예제 #2
0
        public static async Task <IActionResult> UploadMedia(
            [OrchestrationTrigger] IDurableOrchestrationContext context,
            [Table(nameof(SlideShow))] CloudTable slideShowTable,
            [Table(nameof(User))] CloudTable userTable,
            ILogger log)
        {
            var twilioResponse  = context.GetInput <TwilioResponse>();
            var slideShowClient = new DataAccess <SlideShow>(slideShowTable);
            var userClient      = new DataAccess <User>(userTable);

            var user = await userClient.GetAsync(nameof(User), twilioResponse.From);

            if (user != null) // Verify user
            {
                var slideShow = await slideShowClient.GetAsync(nameof(SlideShow), $"{user.PhoneNumber}{user.PIN}");

                if (slideShow != null && !string.IsNullOrEmpty(twilioResponse.Medias))
                {
                    slideShow.Medias += $"{twilioResponse.Medias}{FEED_DELIMITER}";
                    await slideShowClient.ReplaceAsync(slideShow);

                    log.Log(LogLevel.Information, $"Media updated for user {twilioResponse.From}");
                    TwilioUtil.Notify(user.RowKey, TwilioMessages.PhotoIsAddedToSlideShow, log);
                }
            }
            return(new OkObjectResult(true));
        }
예제 #3
0
        public static async Task <IActionResult> Help(
            [OrchestrationTrigger] IDurableOrchestrationContext context,
            [DurableClient] IDurableClient client,
            ILogger log)
        {
            var twilioResponse  = context.GetInput <TwilioResponse>();
            var msgProcessed    = true;
            var responseMessage = "";

            try
            {
                var operationMessages = @"
                    text 'start' to start a donation drive \n
                    text 'close' to end a donation drive \n
                    text 'donate' to donate items \n
                    text 'receive' to receive donated items \n
                ";
                TwilioUtil.Notify(twilioResponse.From, operationMessages, log);
            }
            catch (Exception ex)
            {
                msgProcessed    = false;
                responseMessage = $"{ex.Message} {ex.StackTrace} {ex?.InnerException?.StackTrace ?? ""}";
            }
            return(msgProcessed ? new OkObjectResult(msgProcessed) : (IActionResult) new BadRequestObjectResult(responseMessage));
        }
예제 #4
0
파일: FnUser.cs 프로젝트: tarikub/Orion
        public static async Task <IActionResult> UserHelp(
            [OrchestrationTrigger] IDurableOrchestrationContext context,
            ILogger log)
        {
            var twilioResponse = context.GetInput <TwilioResponse>();

            TwilioUtil.Notify(twilioResponse.From, TwilioMessages.MoreText, log);
            bool helpSent = await Task.FromResult(true);

            return(helpSent == true
               ? (ActionResult) new OkObjectResult(true)
               : new BadRequestObjectResult(TwilioMessages.NotAbleToRegister));
        }
예제 #5
0
파일: FnUser.cs 프로젝트: tarikub/Orion
        public static async Task <IActionResult> UserRegister(
            [OrchestrationTrigger] IDurableOrchestrationContext context,
            [Table(nameof(User))] CloudTable userTable,
            [Table(nameof(SlideShow))] CloudTable slideShowTable,
            ILogger log)
        {
            var userClient      = new DataAccess <User>(userTable);
            var slideShowClient = new DataAccess <SlideShow>(slideShowTable);

            OrionEntities.User user = null;

            var twilioResponse = context.GetInput <TwilioResponse>();
            var newPin         = Verification.NewPIN();

            user = await userClient.GetAsync(nameof(User), twilioResponse.From);

            if (user == null) // Register user
            {
                user = new OrionEntities.User()
                {
                    RowKey      = twilioResponse.From,
                    Timestamp   = DateTime.UtcNow,
                    PIN         = newPin,
                    PhoneNumber = twilioResponse.From
                };

                await userClient.InsertAsync(user);

                log.Log(LogLevel.Information, $"User {user.PhoneNumber} is added to storage");
            }
            else
            {
                user.PIN = newPin;
                await slideShowClient.InsertAsync(new SlideShow
                {
                    PIN    = user.PIN,
                    Status = UserStatus.Active,
                    RowKey = $"{user.PhoneNumber}{user.PIN}",
                    Phone  = user.PhoneNumber
                });

                await userClient.ReplaceAsync(user);
            }


            TwilioUtil.Notify(user.RowKey, String.Format(TwilioMessages.VerifyPhone, newPin), log);

            return(user != null
                ? (ActionResult) new OkObjectResult(user)
                : new BadRequestObjectResult(TwilioMessages.NotAbleToRegister));
        }
예제 #6
0
파일: FnUser.cs 프로젝트: tarikub/Orion
        public static async Task <IActionResult> UserVerify(
            [OrchestrationTrigger] IDurableOrchestrationContext context,
            [Table(nameof(User))] CloudTable userTable,
            [Table(nameof(SlideShow))] CloudTable slideShowTable,
            ILogger log)
        {
            var userClient      = new DataAccess <User>(userTable);
            var slideShowClient = new DataAccess <SlideShow>(slideShowTable);
            var twilioResponse  = context.GetInput <TwilioResponse>();

            User user     = null;
            var  verified = false;

            var pin = twilioResponse.Body;

            user = await userClient.GetAsync(nameof(User), twilioResponse.From);

            if (user != null) // Verify user
            {
                if (string.Equals(user.PIN, pin, StringComparison.OrdinalIgnoreCase))
                {
                    verified = true;


                    // Check if number has been used before
                    var slideShow = await slideShowClient.GetAsync(nameof(SlideShow), $"{user.PhoneNumber}{user.PIN}");

                    if (slideShow == null)
                    {
                        await slideShowClient.InsertAsync(new SlideShow
                        {
                            PIN    = user.PIN,
                            Status = UserStatus.Active,
                            RowKey = $"{user.PhoneNumber}{user.PIN}",
                            Phone  = user.PhoneNumber
                        });
                    }

                    TwilioUtil.Notify(user.RowKey, String.Format(TwilioMessages.AccountVerified, user.PIN), log);
                }
                else
                {
                    TwilioUtil.Notify(user.RowKey, TwilioMessages.InvalidPin, log);
                }
            }

            return(verified == true
               ? (ActionResult) new OkObjectResult(user)
               : new BadRequestObjectResult(TwilioMessages.NotAbleToRegister));
        }
예제 #7
0
        public static async Task <IActionResult> Start(
            [OrchestrationTrigger] IDurableOrchestrationContext context,
            [Table(nameof(Center))] CloudTable centerTable,
            ILogger log)
        {
            var twilioResponse  = context.GetInput <TwilioResponse>();
            var centerClient    = new DataAccess <Center>(centerTable);
            var msgProcessed    = true;
            var responseMessage = "";

            try
            {
                var centerFilter = new TableQuery <Center>()
                                   .Where(TableQuery.GenerateFilterCondition("HostPhoneNumber", QueryComparisons.Equal,
                                                                             twilioResponse.From));

                var centers = await centerClient.GetAllAsync(centerFilter);

                var center = centers.FirstOrDefault();

                if (center == null)
                {
                    await centerClient.InsertAsync(new Center
                    {
                        Active              = false,
                        Address             = string.Empty,
                        Lat                 = string.Empty,
                        HostPhoneNumber     = twilioResponse.From,
                        DonationPhoneNumber = string.Empty
                    });

                    TwilioUtil.Notify(twilioResponse.From, Messages.Host, log);
                }
                else
                {
                    TwilioUtil.Notify(twilioResponse.From, string.Format(Messages.EventFound), log);
                }
            }
            catch (Exception ex)
            {
                msgProcessed    = false;
                responseMessage = $"{ex.Message} {ex.StackTrace} {ex?.InnerException?.StackTrace ?? ""}";
            }
            return(msgProcessed ? new OkObjectResult(msgProcessed) : (IActionResult) new BadRequestObjectResult(responseMessage));
        }
예제 #8
0
        public static async Task <IActionResult> Donate(
            [OrchestrationTrigger] IDurableOrchestrationContext context,
            [Table(nameof(Center))] CloudTable centerTable,
            ILogger log)
        {
            var twilioResponse  = context.GetInput <TwilioResponse>();
            var centerClient    = new DataAccess <Center>(centerTable);
            var msgProcessed    = true;
            var responseMessage = "";

            try
            {
                var centerFilter = new TableQuery <Center>()
                                   .Where(TableQuery.GenerateFilterCondition("DonationPhoneNumber", QueryComparisons.Equal,
                                                                             twilioResponse.To));

                var centers = await centerClient.GetAllAsync(centerFilter);

                var center = centers.FirstOrDefault();
                if (center != null)
                {
                    if (center.Active)
                    {
                        // Notify donor
                        TwilioUtil.Notify(twilioResponse.From, string.Format(Messages.Donate, center.Address), log);
                        // Notify host
                        TwilioUtil.Notify(center.HostPhoneNumber, string.Format(Messages.DonationReceipt, twilioResponse.From, twilioResponse.Body), log);
                    }
                    else
                    {
                        TwilioUtil.Notify(twilioResponse.From, Messages.Close, log);
                    }
                }
            }
            catch (Exception ex)
            {
                msgProcessed    = false;
                responseMessage = $"{ex.Message} {ex.StackTrace} {ex?.InnerException?.StackTrace ?? ""}";
            }
            return(msgProcessed ? new OkObjectResult(msgProcessed) : (IActionResult) new BadRequestObjectResult(responseMessage));
        }
예제 #9
0
        public static async Task <IActionResult> Delete(
            [OrchestrationTrigger] IDurableOrchestrationContext context,
            [Table(nameof(SlideShow))] CloudTable slideShowTable,
            [Table(nameof(User))] CloudTable userTable,
            ILogger log)
        {
            var twilioResponse  = context.GetInput <TwilioResponse>();
            var slideShowClient = new DataAccess <SlideShow>(slideShowTable);
            var userClient      = new DataAccess <User>(userTable);

            var user = await userClient.GetAsync(nameof(User), twilioResponse.From);

            if (user != null) // Verify user
            {
                var slideShow = await slideShowClient.GetAsync(nameof(SlideShow), $"{user.PhoneNumber}{user.PIN}");

                if (slideShow != null && !string.IsNullOrEmpty(slideShow.Medias))
                {
                    var images           = slideShow.Medias.Split(FEED_DELIMITER).Where(media => !string.IsNullOrWhiteSpace(media));
                    var checkidxToDelete = twilioResponse.Body.Split(FEED_DELIMITER).Where(userText => int.TryParse(userText, out var indexToDelete)).FirstOrDefault();

                    if (int.TryParse(checkidxToDelete, out int numToDelete) && numToDelete > 0 && numToDelete <= images.Count())
                    {
                        var idxToDelete   = --numToDelete;
                        var updatedList   = new List <string>(images);
                        var removedMsgURL = updatedList[idxToDelete];
                        updatedList.RemoveAt(idxToDelete);
                        slideShow.Medias = String.Join(FEED_DELIMITER, updatedList);
                        await slideShowClient.ReplaceAsync(slideShow);

                        log.Log(LogLevel.Information, $"Media deleted for user {twilioResponse.From}");
                        TwilioUtil.Notify(user.RowKey, TwilioMessages.PhotoDeleted, log);
                    }
                }
            }
            return(new OkObjectResult(true));
        }
예제 #10
0
        public static async Task <IActionResult> DonateLocation(
            [OrchestrationTrigger] IDurableOrchestrationContext context,
            [SignalR(HubName = "center")] IAsyncCollector <SignalRMessage> signalRMessages,
            [Table(nameof(Center))] CloudTable centerTable,
            ILogger log)
        {
            var twilioResponse  = context.GetInput <TwilioResponse>();
            var centerClient    = new DataAccess <Center>(centerTable);
            var msgProcessed    = true;
            var responseMessage = "";

            try
            {
                var centerFilter = new TableQuery <Center>()
                                   .Where(TableQuery.GenerateFilterCondition("HostPhoneNumber", QueryComparisons.Equal,
                                                                             twilioResponse.From));
                var centers = await centerClient.GetAllAsync(centerFilter);

                var center      = centers.FirstOrDefault();
                var donatedItem = ConfigUtil.GetEnvironmentVariable("donatedItem");

                if (center != null)
                {
                    var geoAddress = await GetLocationAsync(twilioResponse.Body);

                    if (geoAddress.results.Length > 0)
                    {
                        var address = geoAddress.results.FirstOrDefault();
                        if (string.IsNullOrEmpty(center.DonationPhoneNumber))
                        {
                            var areaCode        = int.Parse(twilioResponse.From.Substring(1, 3));
                            var newTwilioNumber = TwilioUtil.GetTwilioNumber(areaCode)?.PhoneNumber?.ToString();

                            var simulated = bool.Parse(ConfigUtil.GetEnvironmentVariable("Simulated"));
                            if (!simulated)
                            {
                                var phoneSID = TwilioUtil.ProvisionNumber(newTwilioNumber);
                                center.DonationPhoneNumberSID = phoneSID;
                            }
                            center.DonationPhoneNumber = newTwilioNumber;
                        }
                        center.Address = address.formatted_address;
                        center.Active  = true;
                        center.Lat     = address.geometry.location.lat.ToString();
                        center.Lng     = address.geometry.location.lng.ToString();

                        await centerClient.ReplaceAsync(center);

                        TwilioUtil.Notify(twilioResponse.From, string.Format(Messages.DonationAddress, center.Address, donatedItem, center.DonationPhoneNumber), log);

                        var broadcastStat = new { Lat = center.Lat, Lng = center.Lng, PhoneNumber = center.DonationPhoneNumber };
                        await signalRMessages.AddAsync(
                            new SignalRMessage
                        {
                            Target    = "BroadcastMessage",
                            Arguments = new[] { broadcastStat }
                        });
                    }
                    else
                    {
                        TwilioUtil.Notify(twilioResponse.From, string.Format(Messages.DonationAddressNotFound, center.Address), log);
                    }
                }
            }
            catch (Exception ex)
            {
                msgProcessed    = false;
                responseMessage = $"{ex.Message} {ex.StackTrace} {ex?.InnerException?.StackTrace ?? ""}";
            }
            return(msgProcessed ? new OkObjectResult(msgProcessed) : (IActionResult) new BadRequestObjectResult(responseMessage));
        }