/// <summary>
        /// Write a vote callback query to the stream.
        /// </summary>
        /// <param name="stream">The stream to write to.</param>
        /// <param name="voteType">The type of the vote.</param>
        /// <param name="messageId">The id of the message that the callback was triggered by.</param>
        /// <param name="messageText">The text of the message that the callback was triggered by.</param>
        /// <param name="trackUrl">The url in de original message the bot replied to.</param>
        public Task WriteVoteCallbackQueryToStream(Stream stream, VoteType voteType, int messageId, string messageText, string trackUrl)
        {
            var update = new Telegram.Bot.Types.Update
            {
                CallbackQuery = new Telegram.Bot.Types.CallbackQuery
                {
                    Id      = "testId",
                    Data    = KeyboardService.GetVoteButtonText(voteType),
                    From    = GetTestUser(),
                    Message = new Telegram.Bot.Types.Message()
                    {
                        Text           = messageText,
                        Date           = DateTime.UtcNow,
                        From           = GetTestUser(isBot: true),
                        Chat           = GetTestChat(),
                        Entities       = new Telegram.Bot.Types.MessageEntity[0],
                        ReplyMarkup    = _keyboardService.CreatePostedTrackResponseKeyboard(),
                        ReplyToMessage = new Telegram.Bot.Types.Message
                        {
                            From = GetTestUser(),
                            Chat = GetTestChat(),
                            Date = DateTime.UtcNow,
                            Text = trackUrl
                        },
                        MessageId = messageId,
                    },
                    ChatInstance = "testChatInstance"
                }
            };

            return(WriteUpdateToStream(stream, update));
        }
示例#2
0
        /// <summary>
        /// Check if an update contains a Vote.
        /// </summary>
        /// <returns>True if the update is a Vote callback.</returns>
        private bool IsVoteCallback(UpdateDto updateDto, VoteType voteType)
        {
            if (string.IsNullOrEmpty(updateDto.ParsedData))
            {
                return(false);
            }

            return(updateDto.ParsedData.Equals(KeyboardService.GetVoteButtonText(voteType)));
        }
示例#3
0
        /// <summary>
        /// Add the VoteType if it was missing.
        /// </summary>
        /// <returns>The text for the VoteType.</returns>
        private static string AddNewVoteText(VoteType voteType, bool?shouldIncrement)
        {
            var useNegativeOperator = UseNegativeOperator(voteType);

            if (shouldIncrement.Value && useNegativeOperator)
            {
                throw new NotSupportedException("A negative voteType cannot be incremented when it isn't added yet.");
            }
            else if (!shouldIncrement.Value && !useNegativeOperator)
            {
                throw new NotSupportedException("A positive voteTypecannot be decremented when it isn't added yet.");
            }

            var       voteCountOperator = useNegativeOperator ? "-" : "+";
            const int voteCount         = 1;

            // Add a space, then the voteTypeText and operator, then the count.
            return($" {KeyboardService.GetVoteButtonText(voteType)}{voteCountOperator}{voteCount}");
        }
示例#4
0
        /// <summary>
        /// Create a Regex to match with at least one VoteType.
        /// </summary>
        private Regex GetRegex(List <VoteType> voteTypes)
        {
            // Match the textstring and at least one vote.
            var pattern = "(.+?)(?!$)";

            foreach (var voteType in voteTypes)
            {
                // Some voteTypes use a negative operator.
                var voteCountOperator = UseNegativeOperator(voteType)
                    ? "-"
                    : "\\+";

                var voteTypeText = KeyboardService.GetVoteButtonText(voteType);

                // Match a space, then the voteTypeText and operator, then the count.
                pattern += $"(?:(\\s{voteTypeText}{voteCountOperator})(\\d+))?";
            }

            pattern += "$";

            return(new Regex(pattern));
        }