Beispiel #1
0
 public Task PostTextMessageToChannelWithAttachment(string message, SlackAPI.Attachment attachment, string channelId)
 {
     return(this.slackClient.PostMessageAsync(
                null,
                channelId,
                message,
                attachments: new SlackAPI.Attachment[] { attachment }));
 }
Beispiel #2
0
        public Task PostTextMessageToChannelAsAttachment(string message, string channelId)
        {
            var attachment = new SlackAPI.Attachment();

            attachment.text = message;

            return(this.PostTextMessageToChannelWithAttachment(string.Empty, attachment, channelId));
        }
        /// <summary>
        /// Formats a BotBuilder activity into an outgoing Slack message.
        /// </summary>
        /// <param name="activity">A BotBuilder Activity object.</param>
        /// <returns>A Slack message object with {text, attachments, channel, thread ts} as well as any fields found in activity.channelData.</returns>
        public static NewSlackMessage ActivityToSlack(Activity activity)
        {
            if (activity == null)
            {
                throw new ArgumentNullException(nameof(activity));
            }

            var message = new NewSlackMessage();

            if (activity.Timestamp != null)
            {
                message.Ts = activity.Timestamp.Value.DateTime.ToString(CultureInfo.InvariantCulture);
            }

            message.Text = activity.Text;

            if (activity.Attachments != null)
            {
                var attachments = new List <SlackAPI.Attachment>();

                foreach (var att in activity.Attachments)
                {
                    if (att.Name == "blocks")
                    {
                        message.Blocks = (List <Block>)att.Content;
                    }
                    else
                    {
                        var newAttachment = new SlackAPI.Attachment()
                        {
                            author_name = att.Name,
                            thumb_url   = att.ThumbnailUrl,
                        };
                        attachments.Add(newAttachment);
                    }
                }

                if (attachments.Count > 0)
                {
                    message.Attachments = attachments;
                }
            }

            message.Channel = activity.Conversation.Id;

            if (!string.IsNullOrWhiteSpace(activity.Conversation.Properties["thread_ts"]?.ToString()))
            {
                message.ThreadTs = activity.Conversation.Properties["thread_ts"].ToString();
            }

            // if channelData is specified, overwrite any fields in message object
            if (activity.ChannelData != null)
            {
                message = activity.GetChannelData <NewSlackMessage>();
            }

            // should this message be sent as an ephemeral message
            if (!string.IsNullOrWhiteSpace(message.Ephemeral))
            {
                message.User = activity.Recipient.Id;
            }

            if (message.IconUrl != null || !string.IsNullOrWhiteSpace(message.Icons?.status_emoji) || !string.IsNullOrWhiteSpace(message.Username))
            {
                message.AsUser = false;
            }

            return(message);
        }