示例#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}");
                    }
                }
            }
        }
示例#2
0
        private IList<SlackAttachmentField> GetAttachmentFields(Attachment attachment)
        {
            var attachmentFields = new List<SlackAttachmentField>();

            if (attachment != null && attachment.AttachmentFields != null)
            {
                foreach (var attachmentField in attachment.AttachmentFields)
                {
                    attachmentFields.Add(new SlackAttachmentField
                    {
                        Title = attachmentField.Title,
                        Value = attachmentField.Value,
                        IsShort = attachmentField.IsShort
                    });
                }
            }

            return attachmentFields;
        }
示例#3
0
        private IList<SlackAttachment> GetAttachments(Attachment attachment)
        {
            var attachments = new List<SlackAttachment>();

            if (attachment != null)
            {
                attachments.Add(new SlackAttachment
                {
                    Text = attachment.Text,
                    Title = attachment.Title,
                    Fallback = attachment.Fallback,
                    ImageUrl = attachment.ImageUrl,
                    ThumbUrl = attachment.ThumbUrl,
                    AuthorName = attachment.AuthorName,
                    ColorHex = attachment.Color,
                    Fields = GetAttachmentFields(attachment)
                });
            }

            return attachments;
        }
示例#4
0
 /// <summary>
 /// Will generate a message to be sent the current channel the message arrived from
 /// </summary>
 public ResponseMessage ReplyToChannel(string text, Attachment attachment = null)
 {
     return ResponseMessage.ChannelMessage(Channel, text, attachment);
 }