Beispiel #1
0
        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}"));
                    }
                }
            }
        }
Beispiel #2
0
        public void Start()
        {
            _adminPin = _configReader.GetConfigEntry <int?>("adminPin");

            if (_adminPin.HasValue)
            {
                _log.Info($"Admin pin is '{_adminPin.Value}'");
            }
            else
            {
                _log.Info("No admin pin detected. Admin mode deactivated.");
            }
        }
Beispiel #3
0
        /// <summary>
        /// Requires config entry 'jira:prefix', 'jira:url' and 'jira:base64Token' to be populated
        /// </summary>
        public JiraMiddleware(IMiddleware next, IConfigReader configReader, StatsPlugin statsPlugin) : base(next)
        {
            _configReader = configReader;
            _statsPlugin  = statsPlugin;

            string prefix = _configReader.GetConfigEntry <string>("jira:prefix");

            HandlerMappings = new[]
            {
                new HandlerMapping
                {
                    ValidHandles  = ContainsTextHandle.For(prefix),
                    Description   = "Gets information about Jira ticket",
                    EvaluatorFunc = JiraHandler,
                }
            };
        }