Ejemplo n.º 1
0
        static BotMessage GetReplay(SlackMessage msg)
        {
            var a = new SlackAttachmentAction {
                Type  = "button",
                Style = SlackAttachmentActionStyle.Danger,
                Name  = "My action name",
                Text  = "My action text",
                Value = "42"
            };

            var sel = new SelectAction {
                Name    = "My action name",
                Text    = "My action text",
                Type    = "select",
                Options = new[] {
                    new Option {
                        Text = "Option one", Value = "1"
                    },
                    new Option {
                        Text = "Option two", Value = "2"
                    },
                },
                //DataSource = SelectAction.DS_USERS,
            };

            var att = new SlackAttachment {
                CallbackId = "my_msg",
                Text       = "My att",
                Title      = "My title",
                Actions    = new[] { sel, a }
            };

            return(new BotMessage {
                //Text = $"RTM message from: {msg.User.Name}\n  ```\n  {JsonConvert.SerializeObject(msg)} \n  ```",
                Text = $"Hej! {msg.Text}",
                ChatHub = msg.ChatHub,
                Attachments = new[] { att }
            });
        }
        public override string GenerateAttachment(IEnumerable <Note> notes, string cacheItemId, int page = 1)
        {
            if (page < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(page));
            }

            int totalPages = GetTotalPageCount(notes.Count(), Constants.Constants.ReturnedNotesMaxReview);

            int rangeStart = GetPagedItemOffsetStart(page, Constants.Constants.ReturnedNotesMaxReview);

            int pageItemCount = GetPageItemCount(notes.Count(), rangeStart, Constants.Constants.ReturnedNotesMaxReview);

            List <SlackAttachment> listOfAttachments = new List <SlackAttachment>();

            foreach (int i in Enumerable.Range(rangeStart, pageItemCount))
            {
                SlackAttachmentAction slackAction = new SlackAttachmentAction()
                {
                    Name  = "option",
                    Text  = "Delete",
                    Type  = "button",
                    Value = "delete"
                };

                DeleteCallbackId deleteCallbackId = new DeleteCallbackId(
                    Constants.Constants.ActionNameReviewSelection,
                    Constants.Constants.CallbackIdTypeNote,
                    notes.ElementAt(i).DocumentId);

                SlackAttachment slackAttachment = new SlackAttachment()
                {
                    Text       = notes.ElementAt(i).Text,
                    Fallback   = Constants.Constants.MessagePleaseUpgrade,
                    Actions    = new SlackAttachmentAction[] { slackAction },
                    CallbackId = deleteCallbackId.ToJson(),
                    Ts         = notes.ElementAt(i).Timestamp
                };

                listOfAttachments.Add(slackAttachment);
            }

            List <SlackAttachmentAction> slackAttachmentActions = new List <SlackAttachmentAction>();

            if (totalPages > page)
            {
                SlackAttachmentAction slackAction = new SlackAttachmentAction()
                {
                    Name  = "page",
                    Text  = "Next Page",
                    Type  = "button",
                    Value = "nextpage"
                };

                slackAttachmentActions.Add(slackAction);
            }

            if (page > 1)
            {
                SlackAttachmentAction slackAction = new SlackAttachmentAction()
                {
                    Name  = "page",
                    Text  = "Prev Page",
                    Type  = "button",
                    Value = "prevpage"
                };

                slackAttachmentActions.Add(slackAction);
            }

            if (slackAttachmentActions.Any())
            {
                PageCallbackId pageCallbackId = new PageCallbackId(
                    Constants.Constants.ActionNameReviewSelection,
                    Constants.Constants.CallbackIdTypePage,
                    page.ToString(),
                    cacheItemId);

                SlackAttachment slackAttachment = new SlackAttachment()
                {
                    Fallback   = Constants.Constants.MessagePleaseUpgrade,
                    Actions    = slackAttachmentActions.ToArray <SlackAttachmentAction>(),
                    CallbackId = pageCallbackId.ToJson(),
                    Color      = GetRandomHexColour()
                };

                listOfAttachments.Add(slackAttachment);
            }

            return(JsonConvert.SerializeObject(listOfAttachments));
        }