Exemplo n.º 1
0
        private static void HandleWsCommand(string dashboardUserId, string groupId, string parameter, string value)
        {
            TelegramGroup group = CacheData.Groups.Values
                                  .SingleOrDefault(x => x.GroupId == groupId);

            if (group == null)
            {
                return;
            }

            if (!UserTools.CanHandleGroup(dashboardUserId, groupId))
            {
                return;
            }

            if (parameter == "WelcomeText")
            {
                CacheData.Groups[group.TelegramChatId].WelcomeText = value;
                return;
            }
            else if (parameter == "RulesText")
            {
                CacheData.Groups[group.TelegramChatId].RulesText = value;
                return;
            }
            else if (parameter == "ReportChatId")
            {
                if (!Int64.TryParse(value, out long reportChatId))
                {
                    return;
                }
                CacheData.Groups[group.TelegramChatId].ReportChatId = reportChatId;
                return;
            }

            ConfigurationParameter config = CacheData.GroupConfigs[group.TelegramChatId]
                                            .Where(x => x.ConfigurationParameterId == parameter)
                                            .SingleOrDefault();

            if (config == null)
            {
                return;
            }

            CacheData.GroupConfigs[group.TelegramChatId]
            [CacheData.GroupConfigs[group.TelegramChatId]
             .IndexOf(config)]
            .Value = value;
        }
Exemplo n.º 2
0
        private static void connectToHub()
        {
            if (CacheData.FatalError ||
                CacheData.IsDisposing)
            {
                return;
            }

            connection = new HubConnectionBuilder()
                         .WithUrl(CacheData.Configuration["HubServerAddress"])
                         .WithAutomaticReconnect()
                         .Build();

            connection.Reconnected += connectionId =>
            {
                Data.Utils.Logging.AddLog(new Models.SystemLog()
                {
                    LoggerName = CacheData.LoggerName,
                    Date       = DateTime.Now,
                    Function   = "connectToHub()",
                    Level      = Models.SystemLog.Levels.Info,
                    Message    = "Reconnecting Hub Server",
                    UserId     = -1
                });

                connection.InvokeAsync("Identification", CacheData.Configuration["HubServerToken"]);

                return(Task.CompletedTask);
            };

            Data.Utils.Logging.AddLog(new Models.SystemLog()
            {
                LoggerName = CacheData.LoggerName,
                Date       = DateTime.Now,
                Function   = "connectToHub()",
                Level      = Models.SystemLog.Levels.Info,
                Message    = "Connecting and autenticating to Hub Server",
                UserId     = -1
            });
            connection.StartAsync().Wait();
            connection.InvokeAsync("Identification", CacheData.Configuration["HubServerToken"]);

            connection.On <string, string>("MessageToBot",
                                           (functionName, data) =>
            {
                switch (functionName)
                {
                case "Identification":
                    if (data == "KO")
                    {
                        Data.Utils.Logging.AddLog(new Models.SystemLog()
                        {
                            LoggerName = CacheData.LoggerName,
                            Date       = DateTime.Now,
                            Function   = "MessageToBot",
                            Level      = Models.SystemLog.Levels.Error,
                            Message    = "Hub Server answered KO on authentication",
                            UserId     = -1
                        });
                        connection.DisposeAsync();
                    }
                    break;
                }
            });

            connection.On <string, string, string, string>("UpdateSetting",
                                                           (dashboardUserId, groupId, parameter, value) =>
            {
                if (CacheData.FatalError ||
                    CacheData.IsDisposing)
                {
                    return;
                }

                HandleWsCommand(dashboardUserId, groupId, parameter, value);
            });

            connection.On <string, string, string, bool>("ToggleStatus",
                                                         (dashboardUserId, groupId, parameter, value) =>
            {
                if (CacheData.FatalError ||
                    CacheData.IsDisposing)
                {
                    return;
                }

                TelegramGroup group = CacheData.Groups.Values
                                      .SingleOrDefault(x => x.GroupId == groupId);
                if (group == null)
                {
                    return;
                }

                if (!UserTools.CanHandleGroup(dashboardUserId, groupId))
                {
                    return;
                }

                group.State = value ? TelegramGroup.Status.Active : TelegramGroup.Status.Inactive;
            });
        }