Exemple #1
0
 public void Deconstruct(
     out JokeType type,
     out BotCommand command,
     out string lang)
 {
     type    = JokeType;
     command = BotCommand;
     lang    = Language;
 }
Exemple #2
0
 private Command(
     JokeType type,
     BotCommand command,
     string lang)
 {
     JokeType   = type;
     BotCommand = command;
     Language   = lang;
 }
Exemple #3
0
        private string getJokeWithOptions(string replacementText, JokeType jokeType)
        {
            string     jokeToReturn        = string.Empty;
            RestClient restClient          = new RestClient(chuckJokeURL);
            string     initialJokeEndpoint = "jokes/random";

            if ((int)jokeType == (int)(JokeType.Any))
            {
                initialJokeEndpoint = "jokes/random";
            }
            if ((int)jokeType == (int)JokeType.Nerdy)
            {
                initialJokeEndpoint += "jokes/random?limitTo=[nerdy]";
            }
            if ((int)jokeType == (int)JokeType.Explicit)
            {
                initialJokeEndpoint += "jokes/random?limitTo=[explicit]";
            }

            logger.LogInformation("Joke Endpoint: " + initialJokeEndpoint);
            RestRequest   request  = new RestRequest(initialJokeEndpoint, Method.GET);
            IRestResponse response = restClient.Execute(request);

            string jokeText = string.Empty;

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                logger.LogInformation("Return from ChuckDB = 200 OK");
                ChuckJokeInfo jokeInfo = new ChuckJokeInfo();
                JsonConvert.PopulateObject(response.Content, jokeInfo);
                jokeText = jokeInfo.value.joke;
            }
            else
            {
                jokeText = "Something went wrong. Chuck is going to kick someone's ass";
            }

            if (replacementText != null)
            {
                jokeText = GetJokeWithReplacement(replacementText, jokeText);
            }

            if (jokeText.Contains("""))
            {
                jokeText = HandleJokeWithQuote(jokeText);
            }

            return(jokeText);
        }
        async Task <(string joke, string user)> PrepareJokeAsync(JokeType type, string lang, CancellationToken cancellationToken)
        {
            var svc = _jokeServiceProvider.Get(type);
            var bot = svc.Actor;

            await ToggleIsTypingAsync(true, bot, cancellationToken);

            await Task.Delay(_random.Next(1000, 3000), cancellationToken);

            var joke = await svc.GetJokeAsync();

            await ToggleIsTypingAsync(false, bot, cancellationToken);

            if (lang != "en-US")
            {
                var(translatedJoke, _) = await _translationService.TranslateAsync(joke, lang);

                return(translatedJoke, bot);
            }

            return(joke, bot);
        }
Exemple #5
0
        public bool IsRecognizedCommand(string message)
        {
            if (string.IsNullOrWhiteSpace(message))
            {
                return(false);
            }

            var commandAndLang = message.Split(":");
            var command        = commandAndLang[0];

            _activeJokeType = commandAndLang.Length > 1 ? (JokeType)Enum.Parse(typeof(JokeType), commandAndLang[1], true) : JokeType.Dad;
            _lang           = commandAndLang.Length > 2 ? commandAndLang[2] : "en";

            switch (command)
            {
            case "joke":
                _activeCommand = BotCommand.TellJoke;
                break;

            case "jokes":
                _activeCommand = BotCommand.SayJokes;
                break;

            case "stop":
                _activeCommand = BotCommand.None;
                break;

            default:
                return(false);
            }

            if (_activeCommand != BotCommand.None)
            {
                _signal.Set();
            }

            return(true);
        }