コード例 #1
1
        private static async Task Run()
        {
            Api bot = new Api("146368700:AAEPKl3D6RLUNylmTqOUB-9cxKIwZGUyEfM");

            int offset = 0;
            AiCommand aiCommand = new AiCommand(bot);
            CurrencyCommand currencyCommand = new CurrencyCommand(bot);
            DefaultCommand defaultCommand = new DefaultCommand(bot);
            WeatherCommand weatherCommand = new WeatherCommand(bot);
            TaskAddCommand taskAddCommand=new TaskAddCommand(bot);
            TaskListCommand taskListCommand = new TaskListCommand(bot);
            IBotCommand[] commands = {currencyCommand, weatherCommand,taskListCommand,taskAddCommand, aiCommand, defaultCommand};

            while (true)
            {
                Update[] updates = await bot.GetUpdates(offset);

                foreach (Update update in updates)
                {
                    if (update.Message.Type == MessageType.TextMessage)
                    {
                        string inputMessage = update.Message.Text;
                        List<CommandEntity> commandsRecieved = MessageParser.ParseMessage(inputMessage);
                        foreach (CommandEntity commandRecieved in commandsRecieved)
                        {
                            foreach (IBotCommand command in commands)
                            {
                                if (command.IsExecutable(commandRecieved.Name))
                                {
                                    Dictionary<string, object> context = new Dictionary<string, object>
                                    {
                                        {"commandEntity", commandRecieved},
                                        {"update", update}
                                    };
                                    command.Execute(context);
                                    break;
                                }
                            }
                        }
                    }

                    offset = update.Id + 1;
                }

                await Task.Delay(1000);
            }
        }
コード例 #2
0
        private static void AJula_OnMessage(object sender, Telegram.Bot.Args.MessageEventArgs e)
        {
            Console.WriteLine($"От {e.Message.Chat.Id}:{e.Message.Text}");
            ICommand command;

            switch (e.Message.Text.ToLower())
            {
            case null: return;

            case "привет":
                command = new HelloCommand();
                command.Execute(e.Message);
                break;

            case "время":
                command = new TimeCommand();
                command.Execute(e.Message);
                break;

            case "погода":
                command = new WeatherCommand();
                command.Execute(e.Message);
                break;

            case "курс":
                command = new CurrencyCommand();
                command.Execute(e.Message);
                break;

            case "создание напоминания":
                Bot.Flag = true;
                break;

            case "газета":
                command = new NewsCommand();
                command.Execute(e.Message);
                break;

            case "почта":
                command = new EmailCommand(e.Message.Chat.Id);
                command.Execute(e.Message);
                break;

            default:
                Bot.AssistantJula.SendTextMessageAsync
                (
                    chatId: e.Message.Chat,
                    text: "Я вас не поняла",
                    replyMarkup: KeyboardTemplates.mainKeyboard
                ).ConfigureAwait(false);
                break;
            }
            if (Bot.Flag)
            {
                command = new ReminderCommand();
                command.Execute(e.Message);
            }
        }
コード例 #3
0
        static async Task Run()
        {
            var bot = new Api("94022128:AAEzeODdvW2yNKkUoeQUbrw2zA63SXatu4I");
            var me  = await bot.GetMe();

            var weatherCommand = new WeatherCommand(bot);
            var defaultCommand = new DefaultCommand(bot);
            var commands       = new ICommand[] { weatherCommand, defaultCommand };

            Console.WriteLine("Hello my name is {0}", me.Username);

            var offset = 0;

            while (true)
            {
                var updates = await bot.GetUpdates(offset);

                foreach (var update in updates)
                {
                    foreach (var command in commands)
                    {
                        if (command.IsApplicable(update))
                        {
                            var context = new Dictionary <string, object>()
                            {
                                { "message", update.Message.Text },
                                { "update", update }
                            };

                            command.Execute(context);
                        }
                    }

                    offset = update.Id + 1;
                }

                await Task.Delay(1000);
            }
        }
コード例 #4
0
ファイル: Program.cs プロジェクト: joshwilliams6/dworkin
        private async Task OnMessageReceivedAsync(SocketMessage s)
        {
            var msg = s as SocketUserMessage;     // Ensure the message is from a user/bot

            if (msg == null)
            {
                return;
            }
            if (msg.Author.Id == _client.CurrentUser.Id)
            {
                return;                                              // Ignore self when checking commands
            }
            if (!msg.ToString().StartsWith("~"))
            {
                return;
            }

            var context = new SocketCommandContext(_client, msg);     // Create the command context

            bool   messageChannel = true;
            string response       = "";

            var commands = msg.ToString().Substring(1).Split(' ');

            switch (commands[0].ToLower())
            {
            case "help":
                response      += Help();
                messageChannel = false;
                break;

            case "weather":
            case "w":
                WeatherCommand wc = new WeatherCommand(_rng, _logger);
                response = wc.Generate(commands);
                break;

            case "wildsurge":
            case "ws":
                WildSurgeCommand wcs = new WildSurgeCommand(_rng, _logger);
                response = wcs.Generate(commands);
                break;

            case "npc":
                NPCCommand npcc = new NPCCommand(_rng, _logger);
                response = npcc.Generate(commands);
                break;

            case "injury":
                InjuryCommand injury = new InjuryCommand(_rng, _logger);
                response = injury.Generate(commands);
                break;

            case "madness":
                MadnessCommand madness = new MadnessCommand(_rng, _logger);
                response = madness.Generate(commands);
                break;

            case "bard":
                BardCommand bard = new BardCommand(_rng, _logger);
                response = bard.Generate(commands);
                break;

            default:
                response += "Command not recognized";
                break;
            }

            if (messageChannel)
            {
                await context.Channel.SendMessageAsync(_diceParser.Parse(response));
            }
            else
            {
                await context.User.SendMessageAsync(response);
            }
            await context.Channel.SendMessageAsync("I have sent help to your PMs.");
        }