예제 #1
0
        public string ProcessCommand(IApiRequest apiRequest, IDataManager dataManager, UpdateObject updateObject, UserData userData)
        {
            if (string.Equals(updateObject.message.text.Trim(), commands[0], StringComparison.InvariantCultureIgnoreCase))
            {
                apiRequest.ExecuteMethod(new sendMessage()
                {
                    chat_id      = updateObject.message.chat.id,
                    text         = strings.LanguageCommandHandler_ProcessCommand_Select_language,
                    reply_markup = new ReplyKeyboardMarkup
                    {
                        keyboard = commands.Skip(1).Select(s => new[] { new KeyboardButton()
                                                                        {
                                                                            text = s
                                                                        }, }).ToArray(),
                        one_time_keyboard = false
                    }
                });
            }
            else
            {
                userData.Language = GetLanguage(updateObject.message.text.Trim());
                Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(userData.Language);

                apiRequest.ExecuteMethod(new sendMessage()
                {
                    chat_id = updateObject.message.chat.id,
                    text    = strings.languageChanged
                });
                return("/settings");
            }
            return(null);
        }
예제 #2
0
        public string ProcessCommand(IApiRequest apiRequest, IDataManager dataManager, UpdateObject updateObject, UserData userData)
        {
            var text = updateObject.message.text;

            if (text == EmojiUtils.HomeIcon + strings.Home)
            {
                userData.OperationDate = DateTime.Now.Date;
                return("/mainmenu");
            }
            if (text == strings.Today)
            {
                userData.OperationDate = DateTime.Now.Date;
            }
            else
            {
                DateTime date;
                userData.OperationDate = DateTime.TryParseExact(text, "dd.MM", null, DateTimeStyles.None, out date) ? date : DateTime.Now.Date;
            }

            apiRequest.ExecuteMethod(new sendMessage()
            {
                chat_id             = updateObject.message.chat.id,
                reply_to_message_id = updateObject.message.message_id,
                text         = GetTitle() + " " + userData.OperationDate.ToString("dd.MM"),
                reply_markup = new ReplyKeyboardMarkup()
                {
                    one_time_keyboard = false,
                    keyboard          = GetDatesKeyboard(DateTime.Now)
                }
            });
            return(ProcessCommandInternal(apiRequest, dataManager, updateObject, userData.OperationDate));
        }
예제 #3
0
 public string ProcessCommand(IApiRequest apiRequest, IDataManager dataManager, UpdateObject updateObject, UserData userData)
 {
     apiRequest.ExecuteMethod(new sendMessage
     {
         chat_id = updateObject.message.chat.id,
         text    = strings.unknownCommand
     });
     return(null);
 }
예제 #4
0
        public string ProcessCommand(IApiRequest apiRequest, IDataManager dataManager, UpdateObject updateObject, UserData userData)
        {
            var result = dataManager.ExecuteCommand(new TopOnlyLeaguesByDate(DateTime.Now, true), userData.Language, userData.TimeZoneOffset);

            apiRequest.ExecuteMethod(new sendMessage()
            {
                chat_id             = updateObject.message.chat.id,
                reply_to_message_id = updateObject.message.message_id,
                text = strings.Live_now
            });
            var leagues = result.SingleOrDefault().Value;

            foreach (var league in leagues)
            {
                var builder = new StringBuilder();

                var list = league.Events.Values.Where(e => e.IsLive && e.StartUDateTime.Date == DateTime.Now.Date).ToList();
                if (list.Count > 0)
                {
                    builder.AppendLine($"*{league.Title}*");
                    foreach (var ev in list.OrderBy(e => e.StartUTime))
                    {
                        builder.AppendLine($"{ev.StartUDateTime.ToString("HH:mm dd.MM")} | {ev.HomeName} *{ev.HomeScore}* - *{ev.AwayScore}* {ev.AwayName}");
                    }
                    apiRequest.ExecuteMethod(new sendMessage()
                    {
                        chat_id    = updateObject.message.chat.id,
                        text       = builder.ToString(),
                        parse_mode = "Markdown"
                    });
                }
            }

            apiRequest.ExecuteMethod(new sendMessage()
            {
                chat_id = updateObject.message.chat.id,
                text    = "/other_leagues",
            });
            return(null);
        }
예제 #5
0
        protected override string ProcessCommandInternal(IApiRequest apiRequest, IDataManager dataManager, UpdateObject updateObject,
                                                         DateTime operationDate)
        {
            var userData = UserSettings.GetUserData([email protected]);
            var result   = dataManager.ExecuteCommand(new TopOnlyLeaguesByDate(operationDate), userData.Language, userData.TimeZoneOffset);


            var leagues = result.SingleOrDefault().Value;

            foreach (var league in leagues)
            {
                var builder = new StringBuilder();

                var list = league.Events.Values.Where(e => e.IsFinished && e.StartUDateTime.Date == operationDate).ToList();
                if (list.Count > 0)
                {
                    builder.AppendLine($"*{league.Title}*");
                    foreach (var ev in list.OrderBy(e => e.StartUTime))
                    {
                        builder.AppendLine(
                            $"{ev.StartUDateTime.ToString("HH:mm dd.MM")} | {ev.HomeName} *{ev.HomeScore}* - *{ev.AwayScore}* {ev.AwayName}");
                    }
                    apiRequest.ExecuteMethod(new sendMessage()
                    {
                        chat_id    = updateObject.message.chat.id,
                        text       = builder.ToString(),
                        parse_mode = "Markdown"
                    });
                }
            }

            apiRequest.ExecuteMethod(new sendMessage()
            {
                chat_id = updateObject.message.chat.id,
                text    = "/other_leagues"
            });
            return(null);
        }
예제 #6
0
 public string ProcessCommand(IApiRequest apiRequest, IDataManager dataManager, UpdateObject updateObject, UserData userData)
 {
     string[] commands = { EmojiUtils.ScheduleIcon + strings.Schedule, EmojiUtils.ResultsIcon + strings.Results, EmojiUtils.NowIcon + strings.Now, EmojiUtils.SettingsIcon + strings.Settings };
     apiRequest.ExecuteMethod(new sendMessage()
     {
         text                = strings.Available_commands,
         chat_id             = updateObject.message.chat.id,
         reply_to_message_id = updateObject.message.message_id,
         reply_markup        = new ReplyKeyboardMarkup()
         {
             one_time_keyboard = false,
             keyboard          = commands.Select(s => new [] { new KeyboardButton()
                                                               {
                                                                   text = s
                                                               } }).ToArray()
         }
     });
     return(null);
 }
예제 #7
0
        public string ProcessCommand(IApiRequest apiRequest, IDataManager dataManager, UpdateObject updateObject, UserData userData)
        {
            var text = updateObject.message.text;

            string[] _commands = GetCommands();
            if (text.Equals(EmojiUtils.SettingsIcon + strings.Settings, StringComparison.InvariantCultureIgnoreCase) || text.Equals("/settings", StringComparison.InvariantCultureIgnoreCase))
            {
                apiRequest.ExecuteMethod(new sendMessage()
                {
                    chat_id      = updateObject.message.chat.id,
                    text         = strings.Available_settings,
                    reply_markup = new ReplyKeyboardMarkup
                    {
                        keyboard = _commands.Select(s => new[] { new KeyboardButton()
                                                                 {
                                                                     text = s
                                                                 }, }).ToArray(),
                        one_time_keyboard = false
                    }
                });
            }
            else if (text.Equals(_commands.Last(), StringComparison.InvariantCultureIgnoreCase))
            {
                return("/mainmenu");
            }
            else if (text.Equals(_commands[0], StringComparison.InvariantCultureIgnoreCase))
            {
                return("/language");
            }

            else if (text.Equals(_commands[1], StringComparison.InvariantCultureIgnoreCase))
            {
                return("/timezone");
            }
            else
            {
                throw new NotImplementedException();
            }
            return(null);
        }
예제 #8
0
        public string ProcessCommand(IApiRequest apiRequest, IDataManager dataManager, UpdateObject updateObject, UserData userData)
        {
            var text = updateObject.message.text;

            if (string.Equals(text, "/timezone", StringComparison.InvariantCultureIgnoreCase))
            {
                apiRequest.ExecuteMethod(new sendMessage()
                {
                    chat_id      = updateObject.message.chat.id,
                    text         = string.Format(strings.Select_your_timezone, EmojiUtils.PinIcon, EmojiUtils.MyLocationIcon),
                    reply_markup = new ReplyKeyboardMarkup
                    {
                        keyboard          = BuildTimezoneKeyboard(),
                        one_time_keyboard = true,
                        resize_keyboard   = true
                    }
                });
            }
            else if (string.Equals(text, EmojiUtils.CancelIcon + strings.Cancel))
            {
                return("/settings");
            }
            else if (updateObject.message.location != null)
            {
                var identifyTimeZone = IdentifyTimeZone(updateObject.message.location);
                if (!identifyTimeZone.Item2.HasValue)
                {
                    apiRequest.ExecuteMethod(new sendMessage()
                    {
                        chat_id = updateObject.message.chat.id,
                        text    = strings.Timezone_was_not_identified__Continue_to_use_default_one
                    });
                }
                else
                {
                    userData.TimeZoneOffset = identifyTimeZone.Item2.Value;
                    apiRequest.ExecuteMethod(new sendMessage()
                    {
                        chat_id = updateObject.message.chat.id,
                        text    = string.Format(strings.Timezone_identified_as, userData.TimeZoneOffset, identifyTimeZone.Item1)
                    });
                }

                return("/settings");
            }
            else
            {
                int value;
                if (int.TryParse(text, out value) && value >= -12 && value <= 12)
                {
                    userData.TimeZoneOffset = value;
                    apiRequest.ExecuteMethod(new sendMessage()
                    {
                        chat_id = updateObject.message.chat.id,
                        text    = strings.Timezone_was_updated
                    });
                    return("/settings");
                }
                else
                {
                    apiRequest.ExecuteMethod(new sendMessage()
                    {
                        chat_id = updateObject.message.chat.id,
                        text    = strings.Invalid_value_was_entered,
                    });
                    return("/settings");
                }
            }

            return(null);
        }
예제 #9
0
        public string ProcessCommand(IApiRequest apiRequest, IDataManager dataManager, UpdateObject updateObject, UserData userData)
        {
            var  game15Data = userData.Game15Data;
            bool hasChanges = false;

            if (game15Data == null || updateObject.message.text == "/15")
            {
                game15Data = new int[16];
                for (var i = 0; i < 16; i++)
                {
                    game15Data[i] = i;
                }
                Random r = new Random(Environment.TickCount);
                game15Data = game15Data.Select(i => new { i, ran = r.Next(1000) })
                             .OrderBy(arg => arg.ran)
                             .Select(arg => arg.i)
                             .ToArray();
                hasChanges = true;
            }
            else
            {
                var i = int.Parse(updateObject.message.text);
                if (i > 0)
                {
                    var indexOf = Array.IndexOf(game15Data, i);
                    var offset  = new[] { -1, 1, -4, 4 };
                    foreach (int t in offset)
                    {
                        var index = indexOf + t;
                        if (index >= 0 && index < 16 && game15Data[index] == 0)
                        {
                            game15Data[index]   = i;
                            game15Data[indexOf] = 0;
                            hasChanges          = true;
                        }
                    }
                }
            }
            if (hasChanges)
            {
                userData.Game15Data = game15Data;
                var d = game15Data.Select(i => i == 0 ? " " : i.ToString()).ToArray();
                KeyboardButton[][] b =
                {
                    new[]
                    {
                        new KeyboardButton()
                        {
                            text = d[0]
                        }, new KeyboardButton()
                        {
                            text = d[1]
                        },
                        new KeyboardButton()
                        {
                            text = d[2]
                        }, new KeyboardButton()
                        {
                            text = d[3]
                        },
                    },
                    new[]
                    {
                        new KeyboardButton()
                        {
                            text = d[4]
                        }, new KeyboardButton()
                        {
                            text = d[5]
                        },
                        new KeyboardButton()
                        {
                            text = d[6]
                        }, new KeyboardButton()
                        {
                            text = d[7]
                        },
                    },
                    new[]
                    {
                        new KeyboardButton()
                        {
                            text = d[8]
                        }, new KeyboardButton()
                        {
                            text = d[9]
                        },
                        new KeyboardButton()
                        {
                            text = d[10]
                        }, new KeyboardButton()
                        {
                            text = d[11]
                        },
                    },
                    new[]
                    {
                        new KeyboardButton()
                        {
                            text = d[12]
                        }, new KeyboardButton()
                        {
                            text = d[13]
                        },
                        new KeyboardButton()
                        {
                            text = d[14]
                        }, new KeyboardButton()
                        {
                            text = d[15]
                        },
                    },
                };
                bool isWin = true;
                for (int i = 0; i < 16; i++)
                {
                    if (game15Data[i] != i + 1 && (i != 15 || game15Data[i] != 0))
                    {
                        isWin = false;
                        break;
                        ;
                    }
                }

                apiRequest.ExecuteMethod(new sendMessage()
                {
                    chat_id      = updateObject.message.chat.id,
                    text         = isWin ? strings.Game15CommandHandler_ProcessCommand_you_won___15_to_restart : strings.Game15CommandHandler_ProcessCommand_your_move___15_to_restart,
                    reply_markup = new ReplyKeyboardMarkup
                    {
                        keyboard          = b,
                        one_time_keyboard = false
                    },
                    disable_notification = true,
                });
            }
            return(null);
        }
예제 #10
0
        public void Process(UpdateObject input)
        {
            var stamp = DateTimeManager.FromTimeStamp(input.message.date).ToLocalTime();

            if ((DateTime.Now - stamp).TotalMinutes > 10)
            {
                return;
            }

            var userData = UserSettings.GetUserData(input.message.from.id);

            using (var context = new DataAccess.SportParser())
            {
                var singleOrDefault = context.Users.SingleOrDefault(user => user.userIdRef == [email protected]);
                userData.Language = singleOrDefault?.Settings.SingleOrDefault(setting => setting.name == "Language")?.value ?? "en-US";
                int timeZoneOffset;
                userData.TimeZoneOffset = int.TryParse(
                    singleOrDefault?.Settings.SingleOrDefault(setting => setting.name == "TimeZoneOffset")?.value,
                    out timeZoneOffset) ? timeZoneOffset : 3;

                Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo(userData.Language);
                string commandResult = null;
                do
                {
                    var resolveCommandHandler = _botCommandFactory.ResolveCommandHandler(input);

                    try
                    {
                        if (resolveCommandHandler != null)
                        {
                            Botan.Track([email protected](), input.message.text, resolveCommandHandler.GetType().Name);
                            if (resolveCommandHandler.SupportContext)
                            {
                                userData.CurrectHandlerContext = resolveCommandHandler.GetType().Name;
                            }
                            commandResult = resolveCommandHandler.ProcessCommand(_apiRequest, _dataManager, input, userData);
                            if (commandResult != null)
                            {
                                userData.CurrectHandlerContext = null;
                                input.message.text             = commandResult;
                                input.message.location         = null;
                            }
                            userData.CommandsHistory.Add(input);
                        }
                    }
                    catch (NotImplementedException)
                    {
                        _apiRequest.ExecuteMethod(new sendMessage()
                        {
                            text                = strings.This_feature_is_under_development,
                            chat_id             = input.message.chat.id,
                            reply_to_message_id = input.message.message_id
                        });
                    }
                } while (commandResult != null);

                if (singleOrDefault == null)
                {
                    singleOrDefault = new DataAccess.User()
                    {
                        userIdRef = [email protected]
                    };
                    context.Users.Add(singleOrDefault);
                }
                var languageSettings = singleOrDefault?.Settings.SingleOrDefault(setting => setting.name == "Language");
                if (languageSettings == null)
                {
                    languageSettings = new Setting()
                    {
                        name = "Language", User = singleOrDefault
                    };
                    singleOrDefault.Settings.Add(languageSettings);
                }
                languageSettings.value = userData.Language;

                var timeZoneSettings = singleOrDefault?.Settings.SingleOrDefault(setting => setting.name == "TimeZoneOffset");
                if (timeZoneSettings == null)
                {
                    timeZoneSettings = new Setting()
                    {
                        name = "TimeZoneOffset", User = singleOrDefault
                    };
                    singleOrDefault.Settings.Add(timeZoneSettings);
                }
                timeZoneSettings.value = userData.TimeZoneOffset.ToString();

                context.SaveChanges();
            }
        }