Beispiel #1
0
        static void Main()
        {
            Console.WriteLine("Starting");

            botClient      = new TelegramBotClient(Configuration.GetTelegramAPIKey());
            commandHandler = new TelegramCommandHandler(botClient);

            botClient.OnMessage += Bot_OnMessage;
            botClient.StartReceiving();

            Console.WriteLine("Started");

            Thread.Sleep(int.MaxValue);
        }
Beispiel #2
0
        static async Task MainAsync(string[] args)
        {
            // Create a bot client.
            var botClient = new TelegramBotClient(Environment.GetEnvironmentVariable("TelegramKey"));
            // Get the bot's User.
            var me = await botClient.GetMeAsync();

            Console.WriteLine(
                $"Hello, World! I am user {me.Id} and my name is {me.FirstName}."
                );
            var commandHandler = new TelegramCommandHandler();

            commandHandler.RegisterCommands <BasicCommands>();
            commandHandler.RegisterCommands <InteractivityCommands>();
            botClient.InitializeCommands(commandHandler);
            botClient.UseInteractivity(new Interactivity.Types.InteractivityConfiguration()
            {
                DefaultTimeOutTime = TimeSpan.FromSeconds(5)
            });
            //MANDATORY
            botClient.StartReceiving();
            Console.ReadKey();
        }
 /// <summary>
 /// Initialize the TelegramCommandHandler with the client
 /// </summary>
 /// <param name="client">Client to initialize commands with</param>
 /// <param name="commandHandler">Command handler</param>
 public static void InitializeCommands(this TelegramBotClient client, TelegramCommandHandler commandHandler)
 {
     foreach (var commandModuleType in commandHandler.RegisteredCommandModules)
     {
         // Get all the modules and the methods that have on a single CommandContext parameter, is async, has a return type of Task,
         // and has the [Command] attribute.
         ModulesAndMethods.Add(commandModuleType,
                               commandModuleType
                               .GetMethods()
                               .Where(method =>
                                      method.GetParameters()
                                      .Length == 1 &&
                                      CommandHandlerUtils.IsSameOrSubclass(method.GetParameters()[0].ParameterType, typeof(CommandContext)) &&
                                      method.IsAsync() &&
                                      CommandHandlerUtils.IsSameOrSubclass(method.ReturnType, typeof(Task)) &&
                                      method.GetCustomAttributes(typeof(CommandAttribute), false)?.Any() == true)
                               .ToList());
     }
     client.OnMessage += async(sender, e) =>
     {
         await OnMessageReceived(sender, e, client, commandHandler);
     };
 }
        private static async Task OnMessageReceived(object sender, MessageEventArgs e, TelegramBotClient client, TelegramCommandHandler commandHandler)
        {
            //Get the message
            Message message       = e.Message;
            bool    caseSensitive = commandHandler.CaseSensitive;

            //Is it a command?
            if (message?.Text?.StartsWith(commandHandler.Prefix) == true)
            {
                //Get the command without the prefix
                string command = e.Message.Text.Substring(commandHandler.Prefix.Length);
                //For every command module in the registered command modules
                //For every method that satisfies the condition above
                foreach (var moduleMethodsPair in ModulesAndMethods)
                {
                    //Create an instance of the command module
                    var commandModule = (CommandModule)Activator.CreateInstance(moduleMethodsPair.Key);
                    //Create a command context
                    foreach (var method in moduleMethodsPair.Value)
                    {
                        var commandAttribute = method
                                               .GetCustomAttribute(typeof(CommandAttribute), false)
                                               as CommandAttribute;
                        var aliasesAttribute = method
                                               .GetCustomAttribute(typeof(AliasesAttribute), false)
                                               as AliasesAttribute;
                        //If the method is linked to the sent command
                        if (commandAttribute
                            ?.CommandInvoker?
                            .ToLowerCaseConditioned(caseSensitive)
                            == command.ToLowerCaseConditioned(caseSensitive) ||
                            aliasesAttribute
                            ?.Aliases
                            ?.Any(alias => alias.ToLowerCaseConditioned(caseSensitive) == command.ToLowerCaseConditioned(caseSensitive))
                            == true)
                        {
                            //Create a CommandContext to be used
                            CommandContext ctx = new CommandContext(e.Message, client);
                            // Call on the Before Execution Async method
                            await commandModule.BeforeExecutionAsync(ctx);

                            //Call on its method.
                            await(Task) method.Invoke(commandModule, new object[] { ctx });
                            //Call on the After Execution Async method
                            await commandModule.AfterExecutionAsync(ctx);

                            //Finish everything up
                            return;
                        }
                    }
                }
            }
        }