private IEnumerable<ResponseMessage> FlickrHandler(IncomingMessage message, string matchedHandle) { string searchTerm = message.TargetedText.Substring(matchedHandle.Length).Trim(); if (string.IsNullOrEmpty(searchTerm)) { yield return message.ReplyToChannel($"Please give me something to search, e.g. {matchedHandle} trains"); } else { yield return message.IndicateTypingOnChannel(); string apiKey = _configReader.GetConfigEntry<string>("flickr:apiKey"); if (string.IsNullOrEmpty(apiKey)) { _statsPlugin.IncrementState("Flickr:Failed"); yield return message.ReplyToChannel("Woops, looks like a Flickr API Key has not been entered. Please ask the admin to fix this"); } else { var flickr = new Flickr(apiKey); var options = new PhotoSearchOptions { Tags = searchTerm, PerPage = 50, Page = 1}; PhotoCollection photos = flickr.PhotosSearch(options); if (photos.Any()) { _statsPlugin.IncrementState("Flickr:Sent"); int i = new Random().Next(0, photos.Count); Photo photo = photos[i]; var attachment = new Attachment { AuthorName = photo.OwnerName, Fallback = photo.Description, ImageUrl = photo.LargeUrl, ThumbUrl = photo.ThumbnailUrl }; yield return message.ReplyToChannel($"Here is your picture about '{searchTerm}'", attachment); } else { _statsPlugin.IncrementState("Flickr:Failed"); yield return message.ReplyToChannel($"Sorry @{message.Username}, I couldn't find anything about {searchTerm}"); } } } }
private IEnumerable<ResponseMessage> JokeHandler(IncomingMessage message, string matchedHandle) { yield return message.IndicateTypingOnChannel(); IRestResponse jokeResponse = new Random().Next(0, 100) < 80 ? GetTambalJoke() : GetMommaJoke(); if (jokeResponse.StatusCode == HttpStatusCode.OK) { _statsPlugin.IncrementState("Jokes:Told"); var joke = JsonConvert.DeserializeObject<JokeContainer>(jokeResponse.Content); yield return message.ReplyToChannel(joke.Joke); } else { _statsPlugin.IncrementState("Jokes:Failed"); yield return message.ReplyToChannel($"Dam, I can't think of one. [{jokeResponse.StatusCode}]"); } }