Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for React messages #7

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 57 additions & 8 deletions MargieBot/Bot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ private async Task ListenTo(string json)
SlackMessage message = new SlackMessage() {
ChatHub = hub,
MentionsBot = (messageText != null ? Regex.IsMatch(messageText, BotNameRegex, RegexOptions.IgnoreCase) : false),
Timestamp = (jObject["ts"] != null ? jObject["ts"].Value<string>() : null),
RawData = json,
// some messages may not have text or a user (like unfurled data from URLs)
Text = messageText,
Expand Down Expand Up @@ -264,18 +265,11 @@ public async Task Say(BotMessage message)

private async Task Say(BotMessage message, ResponseContext context)
{
string chatHubID = null;
if (message == null)
{
return;
}

if(message.ChatHub != null) {
chatHubID = message.ChatHub.ID;
}
else if(context != null && context.Message.ChatHub != null) {
chatHubID = context.Message.ChatHub.ID;
}
var chatHubID = GetChatHubId(message, context);

if(chatHubID != null) {
NoobWebClient client = new NoobWebClient();
Expand Down Expand Up @@ -303,6 +297,61 @@ await client.GetResponse(
}
}

public async Task React(BotMessage message, ResponseContext context)
{
if (message == null)
{
return;
}

if (context == null || context.Message == null || context.Message.Timestamp.IsNullOrEmpty())
{
throw new ArgumentException("When calling the React() method, the context must have a Message and Timestamp.");
}

var chatHubID = GetChatHubId(message, context);
if (chatHubID != null)
{
NoobWebClient client = new NoobWebClient();

List<string> values = new List<string>() {
"token", this.SlackKey,
"channel", chatHubID,
"timestamp", context.Message.Timestamp,
"name", message.Text
};

await client.GetResponse(
"https://slack.com/api/reactions.add",
RequestMethod.Post,
values.ToArray()
);
}
else
{
throw new ArgumentException("When calling the React() method, the message parameter must have its ChatHub property set.");
}
}

private static string GetChatHubId(BotMessage message, ResponseContext context)
{
string chatHubID = null;
if (message == null)
{
return chatHubID;
}

if (message.ChatHub != null)
{
chatHubID = message.ChatHub.ID;
}
else if (context != null && context.Message.ChatHub != null)
{
chatHubID = context.Message.ChatHub.ID;
}
return chatHubID;
}

#region Events
public event MargieConnectionStatusChangedEventHandler ConnectionStatusChanged;
private void RaiseConnectionStatusChanged()
Expand Down
1 change: 1 addition & 0 deletions MargieBot/Models/SlackMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public class SlackMessage
public bool MentionsBot { get; set; }
public string RawData { get; set; }
public string Text { get; set; }
public string Timestamp { get; set; }
public SlackUser User { get; set; }
}
}