コード例 #1
0
        /// <summary>
        /// Executes the "Calc" command, which is the main command of the app.
        /// It converts ValueCurrency to DisplayCurrencies and prints the results out.
        /// </summary>
        /// <param name="message">the message a user sent</param>
        /// <param name="client">Bot instance, needed to answer on the message</param>
        /// <param name="repo">Repository for the whole db, allows this command handler to save/read data</param>
        /// <returns>Task to be awaited</returns>
        public override async Task Execute(Message message)
        {
            var messageText = message?.Text;

            if (messageText == null)
            {
                return;
            }

            var content = messageText
                          .TrimStart(charsToIgnore) // "/calc" <- remove that if exists
                          .Replace(',', '.');       // can process both , and . in the number

            if (!decimal.TryParse(content, out var value))
            {
                throw new Exception("Problems occured when parsing your message");
            }

            var messageId = message.MessageId;
            var chatId    = message.Chat.Id;

            var dataTask = CurrenciesDataCaching.GetValCursAsync();

            await Repo.EnsureChatCreatedAsync(chatId); // ensure chat is created, so that it is not added multiple times (in multiple threads)

            // execute them all in parallel and wait for the completion
            var percentsTask          = Repo.GetPercentsAsync(chatId);
            var valueCurrencyTask     = Repo.GetCurrencyEmojiAsync(chatId);
            var displayCurrenciesTask = Repo.GetDisplayCurrenciesEmojisAsync(chatId);

            await Task.WhenAll(dataTask, percentsTask, valueCurrencyTask, displayCurrenciesTask);

            var values =
                await ValuesCalculator.GetCurrenciesValuesAsync(
                    value,
                    valueCurrencyTask.Result,
                    dataTask.Result,
                    displayCurrenciesTask.Result);

            var textToSend = await AnswerBuilder.BuildStringFromValuesAsync(
                values, valueCurrencyTask.Result, percentsTask.Result);

            await Client.SendTextMessageAsync(chatId, textToSend, replyToMessageId : messageId);
        }
コード例 #2
0
        public async Task HandleAsync(InlineQuery q)
        {
            if (IsNullOrEmpty(q.Query))
            {
                return;
            }

            var input = q.Query;

            var dataTask             = GetValCursAsync();
            var currenciesEmojisTask = repo.GetCurrencyEmojisAsync();

            var data             = dataTask.Result;
            var currenciesEmojis = currenciesEmojisTask.Result;

            string currency = null;

            foreach (var i in currenciesEmojis)
            {
                if (input.Contains(i.Currency))
                {
                    currency = i.Currency;
                    break;
                }
            }

            var space    = " "; // format: {decimal}{space}{currency} e.g "53.2 UAH" or just {decimal}
            var argsPart = currency == null ? "" : $"{space}{currency}";

            var pattern = $@"[0-9][0-9]*([\.,][0-9][0-9]*)?{argsPart}";

            if (!Regex.IsMatch(input, pattern))
            {
                var text = "format: {decimal}{space}{currency} e.g \"53.2 UAH\" or just {decimal} e.g \"53.2\"";

                await bot.AnswerInlineQueryAsync(
                    q.Id,
                    await InlineAnswerBuilder.ArticleToQueryResultAsync(
                        "Result", text, text)
                    );

                return;
            }

            var valueToParse = IsNullOrEmpty(argsPart) ? input : input.Replace(argsPart, "");
            var isValid      = decimal.TryParse(valueToParse, out var value);

            if (isValid)
            {
                if (currency == null)
                {
                    currency = DefaultValues.DefaultValueCurrency;
                }

                var currencyEmoji = currenciesEmojis.First(ce => ce.Currency == currency);

                var values =
                    await ValuesCalculator.GetCurrenciesValuesAsync(value, currencyEmoji, data, currenciesEmojis);

                var answer1 = await AnswerBuilder.BuildStringFromValuesAsync(values, currencyEmoji);

                await bot.AnswerInlineQueryAsync(
                    q.Id,
                    await InlineAnswerBuilder.ArticleToQueryResultAsync(
                        "Result", answer1, answer1)
                    );
            }
        }