示例#1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SlackAdapter"/> class.
 /// Creates a Slack adapter.
 /// </summary>
 /// <param name="adapterOptions">The adapter options to be used when connecting to the Slack API.</param>
 /// <param name="logger">The ILogger implementation this adapter should use.</param>
 /// <param name="slackClient">The SlackClientWrapper used to connect to the Slack API.</param>
 public SlackAdapter(SlackAdapterOptions adapterOptions, ILogger logger = null, SlackClientWrapper slackClient = null)
 {
     _slackClient = slackClient ?? new SlackClientWrapper(adapterOptions) ?? throw new ArgumentNullException(nameof(adapterOptions));
     _logger      = logger ?? NullLogger.Instance;
 }
示例#2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SlackAdapter"/> class.
 /// Creates a Slack adapter.
 /// </summary>
 /// <param name="slackClient">An initialized instance of the SlackClientWrapper class to connect to.</param>
 /// <param name="logger">The ILogger implementation this adapter should use.</param>
 public SlackAdapter(SlackClientWrapper slackClient, ILogger logger = null)
 {
     _slackClient = slackClient ?? throw new ArgumentNullException(nameof(slackClient));
     _logger      = logger ?? NullLogger.Instance;
 }
示例#3
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);
        }
示例#4
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);
        }
示例#5
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SlackAdapter"/> class.
 /// Creates a Slack adapter.
 /// </summary>
 /// <param name="slackClient">An initialized instance of the SlackClientWrapper class to connect to.</param>
 public SlackAdapter(SlackClientWrapper slackClient)
 {
     _slackClient = slackClient ?? throw new ArgumentNullException(nameof(slackClient));
 }