Ejemplo n.º 1
0
        public void GetTrackTest(string userName, string password, string trackName, string expectedResult)
        {
            var yandexApi = new YandexApi();

            yandexApi.Password = password.ToCharArray();
            yandexApi.UserName = userName.ToCharArray();
            Assert.AreEqual(expectedResult, yandexApi.GetTrack(trackName).Title);
        }
Ejemplo n.º 2
0
        private void BotOnSearchTrack(object sender, MessageEventArgs e)
        {
            Bot.OnMessage -= BotOnSearchTrack;

            Log.Information($"Search track: {e.Message.Text}");

            var message = e.Message;

            try
            {
                var tracks      = YandexApi.SearchTrack(message.Text);
                var infoMessage = Bot.SendTextMessageAsync(
                    message.Chat.Id,
                    $"Я пришлю вам 5 треков по запросу \"{message.Text}\", в ближайшее время");

                for (var i = 0; i < 5; i++)
                {
                    if (i > tracks.Count)
                    {
                        break;
                    }

                    var track       = YandexApi.GetTrack(tracks[i].Id);
                    var streamTrack = YandexApi.ExtractStreamTrack(track);
                    var artistName  = track.Artists.FirstOrDefault()?.Name;

                    Log.Information($"[SEND] {track.Title}");

                    Task.WaitAll(streamTrack.Task);

                    var inputStream = new InputOnlineFile(streamTrack, $"{artistName} - {track.Title}");

                    Bot.SendAudioAsync(
                        message.Chat.Id,
                        inputStream, disableNotification: true).GetAwaiter().GetResult();

                    Log.Information($"[SUCCESS] {track.Title}");
                }

                Bot.SendTextMessageAsync(message.Chat.Id, "Еще что-то поищим?",
                                         replyMarkup: KeyboardBuilder.CreateSearchKeyboardMarkup()).GetAwaiter()
                .GetResult();
            }
            catch (Exception ex)
            {
                Log.Error($"Error message: {ex.Message}\nStackTrace: {ex.StackTrace}");
            }
        }