Skip to content
This repository has been archived by the owner on Jun 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #65 from AshleyPoole/f/new-channel-methods
Browse files Browse the repository at this point in the history
F/new channel methods
  • Loading branch information
Workshop2 authored Oct 16, 2017
2 parents 451154c + 322dbc7 commit 29ece92
Show file tree
Hide file tree
Showing 17 changed files with 882 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ internal class FlurlChannelClient : IChannelClient
{
private readonly IResponseVerifier _responseVerifier;
internal const string JOIN_DM_PATH = "/api/im.open";
internal const string CHANNEL_CREATE_PATH = "/api/channels.create";
internal const string CHANNEL_JOIN_PATH = "/api/channels.join";
internal const string CHANNEL_ARCHIVE_PATH = "/api/channels.archive";
internal const string CHANNEL_SET_PURPOSE_PATH = "/api/channels.setPurpose";
internal const string CHANNEL_SET_TOPIC_PATH = "/api/channels.setTopic";
internal const string CHANNELS_LIST_PATH = "/api/channels.list";
internal const string GROUPS_LIST_PATH = "/api/groups.list";
internal const string USERS_LIST_PATH = "/api/users.list";
Expand All @@ -32,6 +37,72 @@ public FlurlChannelClient(IResponseVerifier responseVerifier)
return response.Channel;
}

public async Task<Models.Channel> CreateChannel(string slackKey, string channelName)
{
var response = await ClientConstants
.SlackApiHost
.AppendPathSegment(CHANNEL_CREATE_PATH)
.SetQueryParam("token", slackKey)
.SetQueryParam("name", channelName)
.GetJsonAsync<ChannelResponse>();

_responseVerifier.VerifyResponse(response);
return response.Channel;
}

public async Task<Models.Channel> JoinChannel(string slackKey, string channelName)
{
var response = await ClientConstants
.SlackApiHost
.AppendPathSegment(CHANNEL_JOIN_PATH)
.SetQueryParam("token", slackKey)
.SetQueryParam("name", channelName)
.GetJsonAsync<ChannelResponse>();

_responseVerifier.VerifyResponse(response);
return response.Channel;
}

public async Task ArchiveChannel(string slackKey, string channelName)
{
var response = await ClientConstants
.SlackApiHost
.AppendPathSegment(CHANNEL_ARCHIVE_PATH)
.SetQueryParam("token", slackKey)
.SetQueryParam("channel", channelName)
.GetJsonAsync<StandardResponse>();

_responseVerifier.VerifyResponse(response);
}

public async Task<string> SetPurpose(string slackKey, string channelName, string purpose)
{
var response = await ClientConstants
.SlackApiHost
.AppendPathSegment(CHANNEL_SET_PURPOSE_PATH)
.SetQueryParam("token", slackKey)
.SetQueryParam("channel", channelName)
.SetQueryParam("purpose", purpose)
.GetJsonAsync<PurposeResponse>();

_responseVerifier.VerifyResponse(response);
return response.Purpose;
}

public async Task<string> SetTopic(string slackKey, string channelName, string topic)
{
var response = await ClientConstants
.SlackApiHost
.AppendPathSegment(CHANNEL_SET_TOPIC_PATH)
.SetQueryParam("token", slackKey)
.SetQueryParam("channel", channelName)
.SetQueryParam("topic", topic)
.GetJsonAsync<TopicResponse>();

_responseVerifier.VerifyResponse(response);
return response.Topic;
}

public async Task<Models.Channel[]> GetChannels(string slackKey)
{
var response = await ClientConstants
Expand Down
11 changes: 11 additions & 0 deletions src/SlackConnector/Connections/Clients/Channel/IChannelClient.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
using System.Threading.Tasks;
using SlackConnector.Connections.Responses;

namespace SlackConnector.Connections.Clients.Channel
{
internal interface IChannelClient
{
Task<Models.Channel> JoinDirectMessageChannel(string slackKey, string user);

Task<Models.Channel> CreateChannel(string slackKey, string channelName);

Task<Models.Channel> JoinChannel(string slackKey, string channelName);

Task ArchiveChannel(string slackKey, string channelName);

Task<string> SetPurpose(string slackKey, string channelName, string purpose);

Task<string> SetTopic(string slackKey, string channelName, string topic);

Task<Models.Channel[]> GetChannels(string slackKey);

Task<Models.Group[]> GetGroups(string slackKey);
Expand Down
2 changes: 0 additions & 2 deletions src/SlackConnector/Connections/Clients/ResponseVerifier.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Net;
using Newtonsoft.Json;
using SlackConnector.Connections.Responses;
using SlackConnector.Exceptions;

Expand Down
9 changes: 9 additions & 0 deletions src/SlackConnector/Connections/Responses/ChannelResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using SlackConnector.Connections.Models;

namespace SlackConnector.Connections.Responses
{
internal class ChannelResponse : StandardResponse
{
public Channel Channel { get; set; }
}
}
7 changes: 7 additions & 0 deletions src/SlackConnector/Connections/Responses/PurposeResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SlackConnector.Connections.Responses
{
internal class PurposeResponse : StandardResponse
{
public string Purpose { get; set; }
}
}
7 changes: 7 additions & 0 deletions src/SlackConnector/Connections/Responses/TopicResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace SlackConnector.Connections.Responses
{
internal class TopicResponse : StandardResponse
{
public string Topic { get; set; }
}
}
27 changes: 26 additions & 1 deletion src/SlackConnector/ISlackConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,36 @@ public interface ISlackConnection
/// <returns>Users.</returns>
Task<IEnumerable<SlackUser>> GetUsers();

/// <summary>
/// <summary>
/// Opens a DM channel to a user. Required to PM someone.
/// </summary>
Task<SlackChatHub> JoinDirectMessageChannel(string user);

/// <summary>
/// Joins a channel.
/// </summary>
Task<SlackChatHub> JoinChannel(string channelName);

/// <summary>
/// Create a channel.
/// </summary>
Task<SlackChatHub> CreateChannel(string channelName);

/// <summary>
/// Archives a channel.
/// </summary>
Task ArchiveChannel(string channelName);

/// <summary>
/// Sets a channel purpose.
/// </summary>
Task<SlackPurpose> SetChannelPurpose(string channelName, string purpose);

/// <summary>
/// Sets a channel topic.
/// </summary>
Task<SlackTopic> SetChannelTopic(string channelName, string topic);

/// <summary>
/// Indicate to the users on the channel that the bot is 'typing' on the keyboard.
/// </summary>
Expand Down
11 changes: 11 additions & 0 deletions src/SlackConnector/Models/SlackPurpose.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace SlackConnector.Models
{
/// <summary>
/// This represents the purpose value of the requested channel.
/// </summary>
public class SlackPurpose
{
public string ChannelName { get; set; }
public string Purpose { get; set; }
}
}
11 changes: 11 additions & 0 deletions src/SlackConnector/Models/SlackTopic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace SlackConnector.Models
{
/// <summary>
/// This represents the topic value of the requested channel.
/// </summary>
public class SlackTopic
{
public string ChannelName { get; set; }
public string Topic { get; set; }
}
}
91 changes: 91 additions & 0 deletions src/SlackConnector/SlackConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,97 @@ public async Task<SlackChatHub> JoinDirectMessageChannel(string user)
};
}

public async Task<SlackChatHub> JoinChannel(string channelName)
{
if (string.IsNullOrEmpty(channelName))
{
throw new ArgumentNullException(nameof(channelName));
}

IChannelClient client = _connectionFactory.CreateChannelClient();
Channel channel = await client.JoinChannel(SlackKey, channelName);

return new SlackChatHub
{
Id = channel.Id,
Name = channel.Name,
Type = SlackChatHubType.Channel
};
}

public async Task<SlackChatHub> CreateChannel(string channelName)
{
if (string.IsNullOrEmpty(channelName))
{
throw new ArgumentNullException(nameof(channelName));
}

IChannelClient client = _connectionFactory.CreateChannelClient();
Channel channel = await client.CreateChannel(SlackKey, channelName);

return new SlackChatHub
{
Id = channel.Id,
Name = channel.Name,
Type = SlackChatHubType.Channel
};
}

public async Task ArchiveChannel(string channelName)
{
if (string.IsNullOrEmpty(channelName))
{
throw new ArgumentNullException(nameof(channelName));
}

IChannelClient client = _connectionFactory.CreateChannelClient();
await client.ArchiveChannel(SlackKey, channelName);
}

public async Task<SlackPurpose> SetChannelPurpose(string channelName, string purpose)
{
if (string.IsNullOrEmpty(channelName))
{
throw new ArgumentNullException(nameof(channelName));
}

if (string.IsNullOrEmpty(purpose))
{
throw new ArgumentNullException(nameof(purpose));
}

IChannelClient client = _connectionFactory.CreateChannelClient();
string purposeSet = await client.SetPurpose(SlackKey, channelName, purpose);

return new SlackPurpose
{
ChannelName = channelName,
Purpose = purposeSet
};
}

public async Task<SlackTopic> SetChannelTopic(string channelName, string topic)
{
if (string.IsNullOrEmpty(channelName))
{
throw new ArgumentNullException(nameof(channelName));
}

if (string.IsNullOrEmpty(topic))
{
throw new ArgumentNullException(nameof(topic));
}

IChannelClient client = _connectionFactory.CreateChannelClient();
string topicSet = await client.SetTopic(SlackKey, channelName, topic);

return new SlackTopic
{
ChannelName = channelName,
Topic = topicSet
};
}

public async Task IndicateTyping(SlackChatHub chatHub)
{
var message = new TypingIndicatorMessage
Expand Down
Loading

0 comments on commit 29ece92

Please sign in to comment.