public static async Task <Stream> GetRandomImage(string topic) { var uri = (await GoogleImages.GetImages(topic)).GetRandomElement(); var request = WebRequest.CreateHttp(uri); using (var response = await request.GetResponseAsync()) { var retVal = new MemoryStream(); await response.GetResponseStream().CopyToAsync(retVal); return(retVal); } }
private async void TimerTick(object state) { if (stayHidratedMessageLastSent != DateTime.Today && DateTime.Now.Hour > 7 && rnd.Next(0, 10) == 0) { foreach (var id in DB.Chats) { await Program.Bot.SendTextMessageAsync(id, Resources.GetString("StayHidratedMessage")); } stayHidratedMessageLastSent = DateTime.Today; } if (DateTime.Now.Hour < 7) { return; } switch (DateTime.Now.DayOfWeek) { case DayOfWeek.Wednesday: if (!wednesdayMessageSent) { wednesdayMessageSent = true; foreach (var id in DB.Chats) { await SendWednesdayMessage(id); } } break; case DayOfWeek.Thursday: if (!thursdayMessageSent) { thursdayMessageSent = true; foreach (var id in DB.Chats) { await SendThurdayMessage(id); } } break; default: wednesdayMessageSent = false; thursdayMessageSent = false; if (DateTime.Now.DayOfWeek != goodMorningMessageLastSent) { var sendHotAudio = await weather.GetMaxTempForCity(WeatherManager.PortoAlegreId) >= 25 && await weather.GetMaxTempForCity(WeatherManager.TresDeMaioId) >= 25; if (sendHotAudio) { using (var stream = Resources.GetAudio("gonna-be-hot.mp3")) { foreach (var id in DB.Chats) { stream.Position = 0; await Program.Bot.SendChatActionAsync(id, ChatAction.UploadAudio); await Program.Bot.SendAudioAsync(id, stream, title : "Pretty much everywhere", performer : "Arthur"); } } } else { Stream image; try { image = await GoogleImages.GetRandomImage("bom+dia"); using (image) { foreach (var id in DB.Chats) { image.Position = 0; await Program.Bot.SendPhotoAsync(id, image); } } } catch { return; } } goodMorningMessageLastSent = DateTime.Now.DayOfWeek; } break; } }