Ejemplo n.º 1
0
        // static List<string> curr = new List<string>();

        public static async void getCurency()
        {
            // Обязательно! убираем старую привязку к вебхуку для бота

            try
            {
                var updates = await bot.GetUpdatesAsync(offset); // получаем массив обновлений

                foreach (var update in updates)                  // Перебираем все обновления
                {
                    if (update != null && update.Message.Text != null)
                    {
                        if (update.Message.Text.ToString() == "/saycur")
                        {
                            Market m = KunaExplorer.GetTicker("btcuah").Result;
                            SendMes("btcuah: " + m.ticker.last + " ");
                        }
                        if (update.Message.Text.ToString() == "/nanopoolpr")
                        {
                            NanopoolPrice m = NanopoolExplorer.ETNNanopoolPrice("etnjzKFU6ogESSKRZZbdqraPdcKVxEC17Cm1Xvbyy76PARQMmgrgceH4krAH6xmjKwJ3HtSAKuyFm1BBWYqtchtq9tBap8Qr4M.3372294e90bbf9e0159556d0058c528b19d179906fdce413357dd4905caee9b7").Result;
                            SendMes("btc_etn: " + m.data.price_btc + "\r\n " + "btc_etn: " + m.data.price_usd);
                        }

                        if (update.Message.Text.ToString() == "/but")
                        {
                            var keyboard = new Telegram.Bot.Types.ReplyMarkups.ReplyKeyboardMarkup
                            {
                                Keyboard = new[] {
                                    new[]             // row 1
                                    {
                                        new Telegram.Bot.Types.KeyboardButton("Накатим!"),
                                        new Telegram.Bot.Types.KeyboardButton("Рря!")
                                    },
                                },
                                ResizeKeyboard = true
                            };

                            await bot.SendTextMessageAsync(chatId, "Давай накатим, товарищ, мой!", Telegram.Bot.Types.Enums.ParseMode.Default, false, false, 0, keyboard);
                        }
                        if (update.Message.Text.ToString() == "/delkeyb")
                        {
                            var keyboard = new Telegram.Bot.Types.ReplyMarkups.ReplyKeyboardRemove();
                            await bot.SendTextMessageAsync(chatId, "клавиатура отключена", Telegram.Bot.Types.Enums.ParseMode.Default, false, false, 0, keyboard);
                        }
                    }


                    offset = update.Id + 1;
                }
            }

            catch (Telegram.Bot.Exceptions.ApiRequestException ex)
            {
                //Console.WriteLine(ex.Message); // если ключ не подошел - пишем об этом в консоль отладки
            }
        }
Ejemplo n.º 2
0
        private static async void OnMessageHandler(object sender, Telegram.Bot.Args.MessageEventArgs e)
        {
            var text   = e.Message.Text;
            var userId = e.Message.Chat.Id;

            if (text == null)
            {
                return;
            }
            try
            {
                //logging messages
                File.AppendAllText(logPath, $"{userId}({e.Message.Chat.FirstName} {e.Message.Chat.LastName}): {text}, {e.Message.Date}\n");
            }
            catch
            {
                File.AppendAllText(logPath, $"{userId}({e.Message.Chat.FirstName}): {text}, {e.Message.Date}\n");
            }
            Console.WriteLine($"User {userId}({e.Message.Chat.FirstName}) sent {text}");
            if (!states.ContainsKey(userId))
            {
                states.Add(userId, State.CommandReciever);
            }

            if (!tempEvents.ContainsKey(userId))
            {
                tempEvents[userId] = new Tuple <Event, int>(new Event(), 0);
            }
            switch (states[userId])
            {
            case State.CommandReciever:
                switch (text)
                {
                case "/start":
                    Send(userId, "This bot helps you to plan your businesses!\n" +
                         "Press /add to start planning!\nPress /help to read more about commands");
                    break;

                case "/help":
                    Send(userId, "This bot helps you to plan your businesses!\n" +
                         "/add adds new plan" +
                         "\n/show shows all records\n" +
                         "/mark then choose a bussiness to mark it done\n" +
                         "/delay then choose a different time for your business\n" +
                         "/remove then choose a record to remove\n" +
                         "(You'll receive notifications and daily statistics)");
                    break;

                case "/show":
                    if (!planner.Contains(userId) || planner.Get(userId).Count() == 0)
                    {
                        Send(userId, "You have no plans!");
                    }
                    else
                    {
                        var    plans = planner.Get(userId);
                        string res   = $"You have planned {plans.Count()} things!\n";
                        for (int i = 0; i < plans.Count(); ++i)
                        {
                            res += $"{i + 1}. {plans[i].name} {plans[i].time} {plans[i].importance}\n";
                        }
                        Send(userId, res);
                    }
                    break;

                case "/add":
                    states[userId] = State.AddName;
                    Send(userId, "Type business name");
                    break;

                case "/mark":
                    states[userId] = State.MarkName;
                    var userEventsMarkName = planner.Get(userId);
                    List <List <Telegram.Bot.Types.ReplyMarkups.InlineKeyboardButton> > listMarkName = new List <List <Telegram.Bot.Types.ReplyMarkups.InlineKeyboardButton> >();
                    for (int i = 0; i < userEventsMarkName.Count(); ++i)
                    {
                        string cur        = $"{userEventsMarkName[i].name} {userEventsMarkName[i].time} {userEventsMarkName[i].importance}";
                        var    addingList = new List <Telegram.Bot.Types.ReplyMarkups.InlineKeyboardButton>
                        {
                            Telegram.Bot.Types.ReplyMarkups.InlineKeyboardButton.WithCallbackData($"{cur}", $"{i}")
                        };
                        listMarkName.Add(addingList);
                    }
                    var markupMarkName = new Telegram.Bot.Types.ReplyMarkups.InlineKeyboardMarkup(listMarkName);
                    try
                    {
                        await bot.SendTextMessageAsync(userId, "Choose a deal you want to mark:", replyMarkup : markupMarkName);
                    }
                    catch
                    {
                        Console.WriteLine($"Error: forbidden user {userId}");
                        planner.DeleteUser(userId);
                        states.Remove(userId);
                        tempEvents.Remove(userId);
                        break;
                    }
                    break;

                case "/delay":
                    states[userId] = State.DelayName;
                    var userEvnts = planner.Get(userId);
                    List <List <Telegram.Bot.Types.ReplyMarkups.InlineKeyboardButton> > listDelay = new List <List <Telegram.Bot.Types.ReplyMarkups.InlineKeyboardButton> >();
                    for (int i = 0; i < userEvnts.Count(); ++i)
                    {
                        string cur        = $"{userEvnts[i].name} {userEvnts[i].time} {userEvnts[i].importance}";
                        var    addingList = new List <Telegram.Bot.Types.ReplyMarkups.InlineKeyboardButton>
                        {
                            Telegram.Bot.Types.ReplyMarkups.InlineKeyboardButton.WithCallbackData($"{cur}", $"{i}")
                        };
                        listDelay.Add(addingList);
                    }
                    var markupDelay = new Telegram.Bot.Types.ReplyMarkups.InlineKeyboardMarkup(listDelay);
                    try
                    {
                        await bot.SendTextMessageAsync(userId, "Choose a deal you want to delay:", replyMarkup : markupDelay);
                    }
                    catch
                    {
                        Console.WriteLine($"Error: forbidden user {userId}");
                        planner.DeleteUser(userId);
                        states.Remove(userId);
                        tempEvents.Remove(userId);
                        break;
                    }
                    break;

                case "/remove":
                    states[userId] = State.RemoveName;
                    var usrEvnts = planner.Get(userId);
                    List <List <Telegram.Bot.Types.ReplyMarkups.InlineKeyboardButton> > listRemove = new List <List <Telegram.Bot.Types.ReplyMarkups.InlineKeyboardButton> >();
                    for (int i = 0; i < usrEvnts.Count(); ++i)
                    {
                        string cur        = $"{usrEvnts[i].name} {usrEvnts[i].time} {usrEvnts[i].importance}";
                        var    addingList = new List <Telegram.Bot.Types.ReplyMarkups.InlineKeyboardButton>
                        {
                            Telegram.Bot.Types.ReplyMarkups.InlineKeyboardButton.WithCallbackData($"{cur}", $"{i}")
                        };
                        listRemove.Add(addingList);
                    }
                    var markupRemove = new Telegram.Bot.Types.ReplyMarkups.InlineKeyboardMarkup(listRemove);
                    try
                    {
                        await bot.SendTextMessageAsync(userId, "Choose a deal you want to delay:", replyMarkup : markupRemove);
                    }
                    catch
                    {
                        Console.WriteLine($"Error: forbidden user {userId}");
                        planner.DeleteUser(userId);
                        states.Remove(userId);
                        tempEvents.Remove(userId);
                        break;
                    }
                    break;

                default:
                    Send(userId, "Enter a proper command! Type /help to get more info");
                    break;
                }
                break;

            case State.AddName:
                tempEvents[userId].Item1.name = text;
                if (tempEvents[userId].Item1.name == "")
                {
                    Send(userId, "Enter non empty name!");
                }
                else
                {
                    var TimeMarkup = new Telegram.Bot.Types.ReplyMarkups.ReplyKeyboardMarkup(new[]
                    {
                        new []
                        {
                            new Telegram.Bot.Types.ReplyMarkups.KeyboardButton("Today"),
                            new Telegram.Bot.Types.ReplyMarkups.KeyboardButton("Tomorrow"),
                            new Telegram.Bot.Types.ReplyMarkups.KeyboardButton("No Term")
                        }
                    })
                    {
                        OneTimeKeyboard = true
                    };
                    try
                    {
                        await bot.SendTextMessageAsync(userId, "Choose Time below!", replyMarkup : TimeMarkup);
                    }
                    catch
                    {
                        Console.WriteLine($"Error: forbidden user {userId}");
                        planner.DeleteUser(userId);
                        states.Remove(userId);
                        tempEvents.Remove(userId);
                        break;
                    }
                }
                states[userId] = State.AddTime;
                break;

            case State.AddTime:
                switch (text)
                {
                case "Today":
                    tempEvents[userId].Item1.time = Time.Today;
                    break;

                case "Tomorrow":
                    tempEvents[userId].Item1.time = Time.Tomorrow;
                    break;

                case "No Term":
                    tempEvents[userId].Item1.time = Time.NoTerm;
                    break;

                default:
                    Send(userId, "Push the buttons!");
                    return;
                }
                var ImpMarkup = new Telegram.Bot.Types.ReplyMarkups.ReplyKeyboardMarkup(new[]
                {
                    new[]
                    {
                        new Telegram.Bot.Types.ReplyMarkups.KeyboardButton("Important"),
                        new Telegram.Bot.Types.ReplyMarkups.KeyboardButton("Medium"),
                        new Telegram.Bot.Types.ReplyMarkups.KeyboardButton("Casual")
                    }
                })
                {
                    OneTimeKeyboard = true
                };
                try
                {
                    await bot.SendTextMessageAsync(userId, "Choose importance below!", replyMarkup : ImpMarkup);
                }
                catch
                {
                    Console.WriteLine($"Error: forbidden user {userId}");
                    planner.DeleteUser(userId);
                    states.Remove(userId);
                    tempEvents.Remove(userId);
                    break;
                }
                states[userId] = State.AddImportance;
                break;

            case State.AddImportance:
                switch (text)
                {
                case "Important":
                    tempEvents[userId].Item1.importance = Importance.Important;
                    break;

                case "Medium":
                    tempEvents[userId].Item1.importance = Importance.Medium;
                    break;

                case "Casual":
                    tempEvents[userId].Item1.importance = Importance.Casual;
                    break;

                default:
                    Send(userId, "Push the buttons!");
                    return;
                }
                tempEvents[userId].Item1.owner = userId;
                planner.Add(userId, new Event(tempEvents[userId].Item1.name, tempEvents[userId].Item1.time, tempEvents[userId].Item1.importance, tempEvents[userId].Item1.owner));
                tempEvents[userId] = new Tuple <Event, int>(new Event(), 0);

                var markup = new Telegram.Bot.Types.ReplyMarkups.ReplyKeyboardRemove();
                try
                {
                    await bot.SendTextMessageAsync(e.Message.Chat.Id, "Record added!", replyMarkup : markup);
                }
                catch
                {
                    Console.WriteLine($"Error: forbidden user {userId}");
                    planner.DeleteUser(userId);
                    states.Remove(userId);
                    tempEvents.Remove(userId);
                    break;
                }
                states[userId] = State.CommandReciever;
                break;

            case State.MarkDone:
                switch (text)
                {
                case "Done":
                    tempEvents[userId].Item1.done = true;
                    break;

                case "Not done":
                    tempEvents[userId].Item1.done = false;
                    break;

                default:
                    Send(userId, "Push the buttons!");
                    return;
                }
                var markupMarkDone = new Telegram.Bot.Types.ReplyMarkups.ReplyKeyboardRemove();
                try
                {
                    await bot.SendTextMessageAsync(e.Message.Chat.Id, $"State was successfully changed!", replyMarkup : markupMarkDone);
                }
                catch
                {
                    Console.WriteLine($"Error: forbidden user {userId}");
                    planner.DeleteUser(userId);
                    states.Remove(userId);
                    tempEvents.Remove(userId);
                    break;
                }
                planner.Mark(e.Message.Chat.Id, tempEvents[userId].Item2, tempEvents[userId].Item1.done);
                tempEvents[userId] = new Tuple <Event, int>(new Event(), 0);
                states[userId]     = State.CommandReciever;
                break;

            case State.DelayTime:
                switch (text)
                {
                case "Today":
                    tempEvents[userId].Item1.time = Time.Today;
                    break;

                case "Tomorrow":
                    tempEvents[userId].Item1.time = Time.Tomorrow;
                    break;

                case "No Term":
                    tempEvents[userId].Item1.time = Time.NoTerm;
                    break;

                default:
                    Send(userId, "Push the buttons!");
                    return;
                }
                var markupDelayTime = new Telegram.Bot.Types.ReplyMarkups.ReplyKeyboardRemove();
                try
                {
                    await bot.SendTextMessageAsync(userId, $"Changes saved!", replyMarkup : markupDelayTime);
                }
                catch
                {
                    Console.WriteLine($"Error: forbidden user {userId}");
                    planner.DeleteUser(userId);
                    states.Remove(userId);
                    tempEvents.Remove(userId);
                    break;
                }
                planner.Delay(userId, tempEvents[userId].Item2, tempEvents[userId].Item1.time);
                tempEvents[userId] = new Tuple <Event, int>(new Event(), 0);
                states[userId]     = State.CommandReciever;
                break;

            default:
                tempEvents[userId] = new Tuple <Event, int>(new Event(), 0);
                //Send(userId, "Something went wrong! Code: Message");
                Send(userId, "Try to push the buttons! Try again");
                states[userId] = State.CommandReciever;
                return;
            }
        }