private async Task InitAsync()
        {
            string apiToken = ConfigurationProvider.TestConfigurations.ApiToken;

            BotClient = new TelegramBotClient(apiToken);
            BotUser   = await BotClient.GetMeAsync(CancellationToken);

            await BotClient.DeleteWebhookAsync(CancellationToken);

            SupergroupChat = await FindSupergroupTestChatAsync();

            var allowedUserNames = await FindAllowedTesterUserNames();

            UpdateReceiver = new UpdateReceiver(BotClient, allowedUserNames);

            await BotClient.SendTextMessageAsync(
                SupergroupChat.Id,
                "```\nTest execution is starting...\n```\n" +
                "#testers\n" +
                "These users are allowed to interact with the bot:\n\n" + UpdateReceiver.GetTesters(),
                ParseMode.Markdown,
                disableNotification : true,
                cancellationToken : CancellationToken
                );

#if DEBUG
            BotClient.MakingApiRequest    += OnMakingApiRequest;
            BotClient.ApiResponseReceived += OnApiResponseReceived;
#endif
        }
Esempio n. 2
0
        private async Task InitAsync()
        {
            string apiToken = ConfigurationProvider.TestConfigurations.ApiToken;

            var httpClientHandler = new RetryHttpMessageHandler(3, _diagnosticMessageSink);
            var httpClient        = new HttpClient(httpClientHandler);

            BotClient = new TelegramBotClient(apiToken, httpClient);
            BotUser   = await BotClient.GetMeAsync(CancellationToken);

            await BotClient.DeleteWebhookAsync(CancellationToken);

            SupergroupChat = await FindSupergroupTestChatAsync();

            var allowedUserNames = await FindAllowedTesterUserNames();

            UpdateReceiver = new UpdateReceiver(BotClient, allowedUserNames);

            await BotClient.SendTextMessageAsync(
                SupergroupChat.Id,
                "```\nTest execution is starting...\n```\n" +
                "#testers\n" +
                "These users are allowed to interact with the bot:\n\n" + UpdateReceiver.GetTesters(),
                ParseMode.Markdown,
                disableNotification : true,
                cancellationToken : CancellationToken
                );
        }
Esempio n. 3
0
        public void Dispose()
        {
            UpdateReceiver.DiscardNewUpdatesAsync(CancellationToken).GetAwaiter().GetResult();

            int passed = RunSummary.Total - RunSummary.Skipped - RunSummary.Skipped;

            BotClient.SendTextMessageAsync(
                SupergroupChat.Id,
                string.Format(
                    Constants.TestExecutionResultMessageFormat,
                    RunSummary.Total,
                    passed,
                    RunSummary.Skipped,
                    RunSummary.Failed
                    ),
                ParseMode.Markdown,
                cancellationToken: CancellationToken
                ).GetAwaiter().GetResult();
        }
        public async Task <Chat> GetChatFromAdminAsync()
        {
            bool IsMatch(Update u) => (
                u.Message.Type == MessageType.Contact ||
                u.Message.ForwardFrom?.Id != null
                );

            var update = await UpdateReceiver
                         .GetUpdatesAsync(IsMatch, updateTypes : UpdateType.Message)
                         .ContinueWith(t => t.Result.Single());

            await UpdateReceiver.DiscardNewUpdatesAsync();

            int userId = update.Message.Type == MessageType.Contact
                ? update.Message.Contact.UserId
                : update.Message.ForwardFrom.Id;

            return(await BotClient.GetChatAsync(userId));
        }
Esempio n. 5
0
        public async Task <Chat> GetChatFromTesterAsync(ChatType chatType)
        {
            bool IsMatch(Update u) => (
                u.Message.Chat.Type == chatType &&
                u.Message.Text?.StartsWith("/test", StringComparison.OrdinalIgnoreCase) == true
                ) || (
                ChatType.Channel == chatType &&
                ChatType.Channel == u.Message.ForwardFromChat?.Type
                );

            var update = await UpdateReceiver
                         .GetUpdatesAsync(IsMatch, updateTypes : UpdateType.Message)
                         .ContinueWith(t => t.Result.Single());

            await UpdateReceiver.DiscardNewUpdatesAsync();

            return(chatType == ChatType.Channel
                ? update.Message.ForwardFromChat
                : update.Message.Chat);
        }
        public async Task <Chat> GetChatFromTesterAsync(ChatType chatType, CancellationToken cancellationToken = default)
        {
            bool IsMatch(Update u) =>
            (
                u.Message.Chat.Type == chatType &&
                u.Message.Text?.StartsWith("/test", StringComparison.OrdinalIgnoreCase) == true
            ) || (
                ChatType.Channel == chatType &&
                ChatType.Channel == u.Message.ForwardFromChat?.Type
                );

            var updates = await UpdateReceiver
                          .GetUpdatesAsync(IsMatch, updateTypes : UpdateType.Message, cancellationToken : cancellationToken);

            var update = updates.Single();

            await UpdateReceiver.DiscardNewUpdatesAsync(cancellationToken);

            return(chatType == ChatType.Channel
                ? update.Message.ForwardFromChat
                : update.Message.Chat);
        }
Esempio n. 7
0
        private async Task InitAsync()
        {
            string apiToken = ConfigurationProvider.TestConfigurations.ApiToken;

            BotClient = new TelegramBotClient(apiToken);
            BotUser   = await BotClient.GetMeAsync(CancellationToken);

            await BotClient.DeleteWebhookAsync(CancellationToken);

            SupergroupChat = await FindSupergroupTestChatAsync();

            var allowedUserNames = await FindAllowedTesterUserNames();

            UpdateReceiver = new UpdateReceiver(BotClient, allowedUserNames);

            await BotClient.SendTextMessageAsync(
                SupergroupChat.Id,
                "```\nTest execution is starting...\n```" +
                "Testers are: \n" + UpdateReceiver.GetTesterMentions(),
                ParseMode.Markdown,
                cancellationToken : CancellationToken
                );
        }
Esempio n. 8
0
        public TestsFixture()
        {
            string apiToken = ConfigurationProvider.TestConfigurations.ApiToken;

            BotClient = new TelegramBotClient(apiToken);

            BotUser = BotClient.GetMeAsync().GetAwaiter().GetResult();

            BotClient.DeleteWebhookAsync().GetAwaiter().GetResult();

            AllowedUserNames = ConfigurationProvider.TestConfigurations.AllowedUserNamesArray;
            UpdateReceiver   = new UpdateReceiver(BotClient, AllowedUserNames);

            string supergroupChatId = ConfigurationProvider.TestConfigurations.SuperGroupChatId;

            #region Validations

            if (string.IsNullOrWhiteSpace(supergroupChatId))
            {
                UpdateReceiver.DiscardNewUpdatesAsync().GetAwaiter().GetResult();
                SupergroupChat = GetChatFromTesterAsync(ChatType.Supergroup).GetAwaiter().GetResult();
            }
            else
            {
                SupergroupChat = BotClient.GetChatAsync(supergroupChatId)
                                 .GetAwaiter().GetResult();
            }

            #endregion

            BotClient.SendTextMessageAsync(
                SupergroupChat.Id,
                "```\nTest execution is starting...\n```",
                ParseMode.Markdown,
                cancellationToken: CancellationToken
                ).GetAwaiter().GetResult();
        }
Esempio n. 9
0
 public static string GetTesters(this UpdateReceiver updateReceiver)
 {
     return(string.Join(", ",
                        updateReceiver.AllowedUsernames.Select(username => username.Replace("_", "\\_"))
                        ));
 }