Exemplo n.º 1
0
        /// <summary>
        /// Creates an activity based on a slack event related to a slash command.
        /// </summary>
        /// <param name="slackBody">The data of the slack event.</param>
        /// <param name="client">The Slack client.</param>
        /// <param name="cancellationToken">A cancellation token for the task.</param>
        /// <returns>An activity containing the event data.</returns>
        public static async Task <Activity> CommandToActivityAsync(SlackRequestBody slackBody, SlackClientWrapper client, CancellationToken cancellationToken)
        {
            if (slackBody == null)
            {
                throw new ArgumentNullException(nameof(slackBody));
            }

            var activity = new Activity()
            {
                Id           = slackBody.TriggerId,
                Timestamp    = default(DateTime),
                ChannelId    = "slack",
                Conversation = new ConversationAccount()
                {
                    Id = slackBody.ChannelId,
                },
                From = new ChannelAccount()
                {
                    Id = slackBody.UserId,
                },
                Recipient = new ChannelAccount()
                {
                    Id = null,
                },
                ChannelData = slackBody,
                Text        = slackBody.Text,
                Type        = ActivityTypes.Event,
            };

            activity.Recipient.Id = await client.GetBotUserByTeamAsync(activity, cancellationToken).ConfigureAwait(false);

            activity.Conversation.Properties["team"] = slackBody.TeamId;

            return(activity);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates an activity based on the slack event data.
        /// </summary>
        /// <param name="slackEvent">The data of the slack event.</param>
        /// <param name="client">The Slack client.</param>
        /// <param name="cancellationToken">A cancellation token for the task.</param>
        /// <returns>An activity containing the event data.</returns>
        public static async Task <Activity> EventToActivityAsync(SlackEvent slackEvent, SlackClientWrapper client, CancellationToken cancellationToken)
        {
            if (slackEvent == null)
            {
                throw new ArgumentNullException(nameof(slackEvent));
            }

            var activity = new Activity()
            {
                Id           = slackEvent.EventTs,
                Timestamp    = default(DateTime),
                ChannelId    = "slack",
                Conversation = new ConversationAccount()
                {
                    Id = slackEvent.Channel ?? slackEvent.ChannelId,
                },
                From = new ChannelAccount()
                {
                    Id = slackEvent.BotId ?? slackEvent.UserId,
                },
                Recipient = new ChannelAccount()
                {
                    Id = null,
                },
                ChannelData = slackEvent,
                Text        = null,
                Type        = ActivityTypes.Event,
            };

            if (slackEvent.ThreadTs != null)
            {
                activity.Conversation.Properties["thread_ts"] = slackEvent.ThreadTs;
            }

            if (activity.Conversation.Id == null)
            {
                if (slackEvent.Item != null && slackEvent.ItemChannel != null)
                {
                    activity.Conversation.Id = slackEvent.ItemChannel;
                }
                else
                {
                    activity.Conversation.Id = slackEvent.Team;
                }
            }

            activity.Recipient.Id = await client.GetBotUserByTeamAsync(activity, cancellationToken).ConfigureAwait(false);

            // If this is conclusively a message originating from a user, we'll mark it as such
            if (slackEvent.Type == "message" && slackEvent.SubType == null)
            {
                activity.Type = ActivityTypes.Message;
                activity.Text = slackEvent.Text;
            }

            return(activity);
        }