Beispiel #1
0
        public static async Task <int> Get(CancellationToken cancellationToken = default)
        {
            var(botConfig, loadBotConfigErrMsg) = await BotConfig.LoadBotConfigAsync(cancellationToken);

            if (loadBotConfigErrMsg is not null)
            {
                Console.WriteLine(loadBotConfigErrMsg);
                return(1);
            }

            ConsoleHelper.PrintTableBorder(28, 50);
            Console.WriteLine($"|{"Key",-28}|{"Value",50}|");
            ConsoleHelper.PrintTableBorder(28, 50);

            Console.WriteLine($"|{"Version",-28}|{botConfig.Version,50}|");
            Console.WriteLine($"|{"BotToken",-28}|{botConfig.BotToken,50}|");
            Console.WriteLine($"|{"ServiceName",-28}|{botConfig.ServiceName,50}|");
            Console.WriteLine($"|{"UsersCanSeeAllUsers",-28}|{botConfig.UsersCanSeeAllUsers,50}|");
            Console.WriteLine($"|{"UsersCanSeeAllGroups",-28}|{botConfig.UsersCanSeeAllGroups,50}|");
            Console.WriteLine($"|{"UsersCanSeeGroupDataUsage",-28}|{botConfig.UsersCanSeeGroupDataUsage,50}|");
            Console.WriteLine($"|{"UsersCanSeeGroupDataLimit",-28}|{botConfig.UsersCanSeeGroupDataLimit,50}|");
            Console.WriteLine($"|{"AllowChatAssociation",-28}|{botConfig.AllowChatAssociation,50}|");

            ConsoleHelper.PrintTableBorder(28, 50);

            return(0);
        }
Beispiel #2
0
        public static async Task <int> Set(string?botToken, string?serviceName, bool?usersCanSeeAllUsers, bool?usersCanSeeAllGroups, bool?usersCanSeeGroupDataUsage, bool?usersCanSeeGroupDataLimit, bool?allowChatAssociation, CancellationToken cancellationToken = default)
        {
            var(botConfig, loadBotConfigErrMsg) = await BotConfig.LoadBotConfigAsync(cancellationToken);

            if (loadBotConfigErrMsg is not null)
            {
                Console.WriteLine(loadBotConfigErrMsg);
                return(1);
            }

            if (!string.IsNullOrEmpty(botToken))
            {
                botConfig.BotToken = botToken;
            }
            if (!string.IsNullOrEmpty(serviceName))
            {
                botConfig.ServiceName = serviceName;
            }
            if (usersCanSeeAllUsers is bool canSeeUsers)
            {
                botConfig.UsersCanSeeAllUsers = canSeeUsers;
            }
            if (usersCanSeeAllGroups is bool canSeeGroups)
            {
                botConfig.UsersCanSeeAllGroups = canSeeGroups;
            }
            if (usersCanSeeGroupDataUsage is bool canSeeGroupDataUsage)
            {
                botConfig.UsersCanSeeGroupDataUsage = canSeeGroupDataUsage;
            }
            if (usersCanSeeGroupDataLimit is bool canSeeGroupDataLimit)
            {
                botConfig.UsersCanSeeGroupDataLimit = canSeeGroupDataLimit;
            }
            if (allowChatAssociation is bool allowLinking)
            {
                botConfig.AllowChatAssociation = allowLinking;
            }

            var saveBotConfigErrMsg = await BotConfig.SaveBotConfigAsync(botConfig, cancellationToken);

            if (saveBotConfigErrMsg is not null)
            {
                Console.WriteLine(loadBotConfigErrMsg);
                return(1);
            }

            return(0);
        }
Beispiel #3
0
        public static async Task <int> RunBot(string?botToken, CancellationToken cancellationToken = default)
        {
            var(botConfig, loadBotConfigErrMsg) = await BotConfig.LoadBotConfigAsync(cancellationToken);

            if (loadBotConfigErrMsg is not null)
            {
                Console.WriteLine(loadBotConfigErrMsg);
                return(1);
            }

            // Priority: commandline option > environment variable > config file
            if (string.IsNullOrEmpty(botToken))
            {
                botToken = Environment.GetEnvironmentVariable("TELEGRAM_BOT_TOKEN");
            }
            if (string.IsNullOrEmpty(botToken))
            {
                botToken = botConfig.BotToken;
            }
            if (string.IsNullOrEmpty(botToken))
            {
                Console.WriteLine("Please provide a bot token with command line option `--bot-token`, environment variable `TELEGRAM_BOT_TOKEN`, or in the config file.");
                return(-1);
            }

            try
            {
                var bot = new TelegramBotClient(botToken);
                Console.WriteLine("Created Telegram bot instance with API token.");

                var me = await bot.GetMeAsync(cancellationToken);

                if (string.IsNullOrEmpty(me.Username))
                {
                    throw new Exception("Error: bot username is null or empty.");
                }

                await bot.SetMyCommandsAsync(UpdateHandler.BotCommandsPublic, null, null, cancellationToken);

                Console.WriteLine($"Registered {UpdateHandler.BotCommandsPublic.Length} bot commands for all chats.");

                var privateChatCommands = UpdateHandler.BotCommandsPrivate.Concat(UpdateHandler.BotCommandsPublic);
                await bot.SetMyCommandsAsync(privateChatCommands, BotCommandScope.AllPrivateChats(), null, cancellationToken);

                Console.WriteLine($"Registered {privateChatCommands.Count()} bot commands for private chats.");

                Console.WriteLine($"Started Telegram bot: @{me.Username} ({me.Id}).");

                var updateHandler  = new UpdateHandler(me.Username, botConfig);
                var updateReceiver = new QueuedUpdateReceiver(bot, null, UpdateHandler.HandleErrorAsync);
                await updateHandler.HandleUpdateStreamAsync(bot, updateReceiver, cancellationToken);
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine($"Invalid access token: {ex.Message}");
            }
            catch (HttpRequestException ex)
            {
                Console.WriteLine($"A network error occurred: {ex.Message}");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            return(0);
        }