コード例 #1
0
        public async void PerformNotifications(object autoEvent)
        {
            await semaphoreSlim.WaitAsync(); // working as a lock (){}

            try
            {
                var userNotifications = await listener.GetNotificationsAsync(NotificationKinds.Toast);

                foreach (UserNotification userNotification in userNotifications)
                {
                    var waNotification = new WaNotification(userNotification);
                    if (Helpers.IsValid(waNotification))
                    {
                        var passed = await ProcessNotification(waNotification);

                        if (passed)
                        {
                            listener.RemoveNotification(userNotification.Id);
                        }
                    }
                }
            }
            catch (Exception e)
            {
                ConsoleProxy.WriteLine(null, ConsoleColor.DarkRed, e.Message);
            }
            finally
            {
                semaphoreSlim.Release();
            }
        }
コード例 #2
0
        public static bool IsValid(WaNotification waNotification)
        {
            // whether the app is Chrome only! "Microsoft Edge" creates notifications with other DateTime format
            if (waNotification.DisplayName != "Google Chrome")
            {
                ConsoleProxy.WriteLine(null, ConsoleColor.DarkGray,
                                       $"{waNotification.Id} Wrong application name ({waNotification.DisplayName})");
                return(false);
            }

            // where the group is parking group
            var config = new Config.AppConfig();

            if (!config.Whatsapp.Groups.Exists(group => waNotification.TitleText.Contains(group, StringComparison.OrdinalIgnoreCase)))
            {
                ConsoleProxy.WriteLine(null, ConsoleColor.DarkGray,
                                       $"{waNotification.Id} Wrong groupName ({waNotification.TitleText})");
                return(false);
            }

            // where the body text is enouth length - street name, space and house number ~ 4
            // and where the body contains address
            var cellPhoneLength = waNotification.BodyText.Split(':')[0].Length;
            var bodyText        = waNotification.BodyText.Substring(cellPhoneLength + 1).Trim();

            if ((bodyText.Length < 4) || !bodyText.Any(char.IsDigit))
            {
                ConsoleProxy.WriteLine(null, ConsoleColor.DarkGray,
                                       $"{waNotification.Id} Wrong address ({bodyText})");
                return(false);
            }

            return(true);
        }
コード例 #3
0
        private async Task <bool> ProcessNotification(WaNotification waNotification)
        {
            var request = Helpers.GetRequest(waNotification);
            var client  = new HttpClient {
                BaseAddress = new Uri(config.Server.Url)
            };
            var content = new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");

            if (!config.App.DryRun)
            {
                var result = await client.PostAsync("chance", content);

                var resultJsonString = await result.Content.ReadAsStringAsync();

                var resultBody = JsonConvert.DeserializeObject <ChanceRes>(resultJsonString);

                if (result.StatusCode != System.Net.HttpStatusCode.OK)
                {
                    ConsoleProxy.WriteLine(null, ConsoleColor.DarkRed,
                                           $"Error has ocurred while sending WA notification: {waNotification.Id.ToString()}, HttpStatus is: {result.StatusCode}");
                    return(false);
                }

                if (resultBody.StatusCode != 200)
                {
                    ConsoleProxy.WriteLine(null, ConsoleColor.Red,
                                           $"Error has ocurred while processing WA notification: {waNotification.Id.ToString()}, " +
                                           $"StatusCode: '{resultBody.StatusCode}', StatusText: '{resultBody.StatusText}'");
                    return(false);
                }
            }

            return(true);
        }
コード例 #4
0
        private async void Listener_NotificationChanged(UserNotificationListener sender, UserNotificationChangedEventArgs args)
        {
            var userNotifications = await listener.GetNotificationsAsync(NotificationKinds.Toast);

            foreach (UserNotification userNotification in userNotifications)
            {
                if (userNotification.Id == args.UserNotificationId)
                {
                    var waNotification = new WaNotification(userNotification);

                    ConsoleProxy.WriteLine(null, ConsoleColor.Yellow, $"Notification arrived " +
                                           $"at: {DateTime.Now.ToLocalTime().ToShortTimeString()}, id: {args.UserNotificationId}");
                    if (Helpers.IsValid(waNotification))
                    {
                        var passed = await ProcessNotification(waNotification);

                        if (passed)
                        {
                            listener.RemoveNotification(userNotification.Id);
                        }
                    }
                }
            }
        }
コード例 #5
0
        public static Models.ChanceReq GetRequest(WaNotification waNotification)
        {
            Console.WriteLine();
            ConsoleProxy.WriteLine(null, ConsoleColor.DarkGray, $"userNotification.Id: ", $"{waNotification.Id}");
            ConsoleProxy.WriteLine(null, ConsoleColor.DarkGray, $"CreationTime: ", $"{waNotification.CreationTime}");
            ConsoleProxy.WriteLine(null, ConsoleColor.DarkGray, $"Description: ", $"{waNotification.Description}");
            ConsoleProxy.WriteLine(null, ConsoleColor.DarkGray, $"DisplayName: ", $"{waNotification.DisplayName}");
            ConsoleProxy.WriteLine(null, ConsoleColor.DarkGray, $"TitleText: ", $"{waNotification.TitleText}");
            ConsoleProxy.WriteLine(null, ConsoleColor.DarkGray, $"BodyText: ", $"{waNotification.BodyText}");
            Console.WriteLine();

            var cellPhone = waNotification.BodyText.Split(':')[0].Trim();
            var bodyText  = waNotification.BodyText.Substring(cellPhone.Length + 1).Trim();

            return(new Models.ChanceReq
            {
                Address = new Models.Address
                {
                    Text = CleanString(bodyText),
                    CityId = 1,
                    CountryId = 367
                },
                Driver = new Models.Driver
                {
                    MobileNum = CleanString(cellPhone)
                },
                WhatsApp = new WhatsApp
                {
                    GroupName = CleanString(waNotification.TitleText)
                },
                Chance = new Models.Chance
                {
                    DateStart = waNotification.CreationTime.ToString()
                }
            });
        }