예제 #1
0
        private async static void BotOnMessageReceived(object sender, MessageEventArgs e)
        {
            var message = e.Message;

            if (message == null || message.Type != MessageType.Text)
            {
                return;
            }
            string name = $"{message.From.FirstName } {message.From.LastName}";

            Console.WriteLine($"{name} sent text: '{message.Text}'");


            User            userrr = new User();
            ClassStatusList st     = JsonConvert.DeserializeObject <ClassStatusList>(File.ReadAllText(stpath));

            int        index     = 0;
            bool       contains  = false;
            UserStatus usrstatus = new UserStatus();


            for (int i = 0; i < st.statuses.Count; i++)
            {
                if (st.statuses[i].Id == Convert.ToString(message.From.Id))
                {
                    contains  = true;
                    usrstatus = st.statuses[i];
                    index     = i;
                    break;
                }
            }

            if (contains == false)
            {
                usrstatus.status1 = 0;
                usrstatus.status2 = 0;
                usrstatus.Id      = Convert.ToString(message.From.Id);
                st.statuses.Add(usrstatus);
            }



            switch (st.statuses[index].status1)
            {
            case 0:
            {
                switch (message.Text)
                {
                case "/start":
                {
                    string text =
                        @"Список команд:
/gettoptracks - Топ-10 треков
/gettrack - Найти трек по названию
/getartistinfo - Информация об исполнителе
/getfavorite - Список избранных";

                    await Bot.SendTextMessageAsync(message.From.Id, text);

                    usrstatus.status1  = 0;
                    usrstatus.status2  = 0;
                    usrstatus.Id       = Convert.ToString(message.From.Id);
                    st.statuses[index] = usrstatus;
                    File.WriteAllText(stpath, JsonConvert.SerializeObject(st));
                    break;
                }

                case "/gettoptracks":
                {
                    var json = JsonConvert.SerializeObject(userrr);
                    var data = new StringContent(json, Encoding.UTF8, "application/json");

                    userrr.Id = Convert.ToString(message.From.Id);

                    HttpClient client   = new HttpClient();
                    var        response = await client.PostAsync($"https://localhost:44348/api/Music/GetTopTracks", data);

                    string      result   = response.Content.ReadAsStringAsync().Result;
                    ClassTracks useritem = JsonConvert.DeserializeObject <ClassTracks>(result);

                    List <List <InlineKeyboardButton> > inlineKeyboardList = new List <List <InlineKeyboardButton> >();


                    foreach (var track in useritem.tracks.track)
                    {
                        //if (track.url.Length <= 60)
                        {
                            List <InlineKeyboardButton> ts = new List <InlineKeyboardButton>();
                            ts.Add(InlineKeyboardButton.WithUrl(track.name + " - " + track.artist.name, track.url));
                            ts.Add(InlineKeyboardButton.WithCallbackData("+", Convert.ToString(message.From.Id) + "⁜" + track.name + "⁜" + track.artist.name));
                            inlineKeyboardList.Add(ts);
                        }
                    }
                    var inline = new InlineKeyboardMarkup(inlineKeyboardList);

                    await Bot.SendTextMessageAsync(message.From.Id, "Топ-10 треков:", replyMarkup : inline);


                    usrstatus.status1  = 0;
                    usrstatus.status2  = 1;
                    st.statuses[index] = usrstatus;

                    File.WriteAllText(stpath, JsonConvert.SerializeObject(st));

                    break;
                }

                case "/getartistinfo":
                {
                    await Bot.SendTextMessageAsync(e.Message.Chat.Id, "Введите исполнителя");

                    st.statuses[index].status1 = 1;
                    File.WriteAllText(stpath, JsonConvert.SerializeObject(st));

                    break;
                }

                case "/gettrack":
                {
                    await Bot.SendTextMessageAsync(e.Message.Chat.Id, "Введите название песни");


                    st.statuses[index].status1 = 2;
                    File.WriteAllText(stpath, JsonConvert.SerializeObject(st));

                    break;
                }

                case "/getfavorite":
                {
                    userrr.Id = Convert.ToString(message.From.Id);

                    var json = JsonConvert.SerializeObject(userrr);
                    var data = new StringContent(json, Encoding.UTF8, "application/json");


                    HttpClient client   = new HttpClient();
                    var        response = await client.PostAsync($"https://localhost:44348/api/Music/GetFavorite", data);

                    string result = response.Content.ReadAsStringAsync().Result;

                    if (result == "BAD")
                    {
                        await Bot.SendTextMessageAsync(e.Message.Chat.Id, "Списка нету");

                        break;
                    }


                    List <SearchTrackMain> js = JsonConvert.DeserializeObject <List <SearchTrackMain> >(result);

                    List <List <InlineKeyboardButton> > inlineKeyboardList = new List <List <InlineKeyboardButton> >();


                    foreach (var track in js)
                    {
                        List <InlineKeyboardButton> ts = new List <InlineKeyboardButton>();
                        ts.Add(InlineKeyboardButton.WithUrl(track.name + " - " + track.artist, track.url));
                        ts.Add(InlineKeyboardButton.WithCallbackData("х", Convert.ToString(message.From.Id) + "⁜" + track.name + "⁜" + track.artist));
                        inlineKeyboardList.Add(ts);
                    }
                    var inline = new InlineKeyboardMarkup(inlineKeyboardList);
                    if (js.Count != 0)
                    {
                        await Bot.SendTextMessageAsync(message.From.Id, "Список фаворитов:", replyMarkup : inline);
                    }
                    else
                    {
                        await Bot.SendTextMessageAsync(message.From.Id, "Список фаворитов пуст.");
                    }

                    usrstatus.status1  = 0;
                    usrstatus.status2  = 2;
                    st.statuses[index] = usrstatus;

                    File.WriteAllText(stpath, JsonConvert.SerializeObject(st));

                    break;
                }

                default:
                {
                    break;
                }
                }
                break;
            }

            case 1:     //для getartistinfo
            {
                try
                {
                    st.statuses[index].status1 = 0;
                    File.WriteAllText(stpath, JsonConvert.SerializeObject(st));

                    userrr.Id     = Convert.ToString(message.From.Id);
                    userrr.Artist = e.Message.Text;

                    var json = JsonConvert.SerializeObject(userrr);
                    var data = new StringContent(json, Encoding.UTF8, "application/json");

                    HttpClient client   = new HttpClient();
                    var        response = await client.PostAsync($"https://localhost:44348/api/Music/GetArtistInfo", data);

                    string result = response.Content.ReadAsStringAsync().Result;
                    ClassListArtistInfo useritem = JsonConvert.DeserializeObject <ClassListArtistInfo>(result);

                    await Bot.SendTextMessageAsync(message.From.Id, $"Artist: {useritem.artist.name}\n\nBio: {useritem.artist.bio.summary}.");

                    break;
                }
                catch
                {
                    await Bot.SendTextMessageAsync(message.From.Id, "Было введено некорректное имя исполнителя.");

                    break;
                }
            }

            case 2:     //для gettrack
            {
                st.statuses[index].status1 = 0;
                st.statuses[index].status2 = 1;

                userrr.Id   = Convert.ToString(message.From.Id);
                userrr.Name = e.Message.Text;

                var json = JsonConvert.SerializeObject(userrr);
                var data = new StringContent(json, Encoding.UTF8, "application/json");


                HttpClient client   = new HttpClient();
                var        response = await client.PostAsync($"https://localhost:44348/api/Music/GetTrack", data);

                string       result   = response.Content.ReadAsStringAsync().Result;
                ClassResults useritem = JsonConvert.DeserializeObject <ClassResults>(result);

                List <List <InlineKeyboardButton> > inlineKeyboardList = new List <List <InlineKeyboardButton> >();


                foreach (var track in useritem.results.trackmatches.track)
                {
                    if (track.url.Length <= 60)
                    {
                        List <InlineKeyboardButton> ts = new List <InlineKeyboardButton>();
                        ts.Add(InlineKeyboardButton.WithUrl(track.name + " - " + track.artist, track.url));
                        ts.Add(InlineKeyboardButton.WithCallbackData("+", Convert.ToString(message.From.Id) + "⁜" + track.name + "⁜" + track.artist));
                        inlineKeyboardList.Add(ts);
                    }
                }
                var inline = new InlineKeyboardMarkup(inlineKeyboardList);
                if (result == "It's bad")
                {
                    await Bot.SendTextMessageAsync(message.From.Id, "Ничего не найдено. Попробуйте ввести название по-другому.");
                }
                else
                {
                    await Bot.SendTextMessageAsync(message.From.Id, "Найденные треки:", replyMarkup : inline);
                }
                File.WriteAllText(stpath, JsonConvert.SerializeObject(st));

                break;
            }

            default:
            {
                goto case 0;
            }
            }
        }