Skip to content

Commit

Permalink
Improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
h3xb0y committed Oct 13, 2021
1 parent 4a436fd commit 39a0a48
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 14 deletions.
110 changes: 110 additions & 0 deletions Controllers/ActionController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using SlackThrowReaction.Model;

namespace SlackThrowReaction.Controllers
{
[ApiController]
[Route("[controller]")]
public class ActionController : ControllerBase
{
private readonly ILogger<GetRandomEmojiController> _logger;

public ActionController(ILogger<GetRandomEmojiController> logger)
{
_logger = logger;
}

[
HttpPost,
Consumes("application/x-www-form-urlencoded")
]
public void Post([FromForm] IFormCollection collection)
{
var json = collection["payload"];
var payload = JsonConvert.DeserializeObject<SlashActionPayload>(json);

if (payload == null)
throw new Exception("No action from response payload");

var action = payload.Actions.First();
var imageUrl = action.Value;

object response = null;

if (action.ActionId == ActionType.Send.ToString())
{
response = new
{
response_type = "in_channel", //"in_channel"
delete_original = "true",
attachments = new[]
{
new
{
image_url = imageUrl
}
}
};
}
else if (action.ActionId == ActionType.Remove.ToString())
{
response = new
{
delete_original = "true",
};
}
else if (action.ActionId == ActionType.Shuffle.ToString())
{
response = new
{
text = "emojiInfo.Code",
replace_original = "true",
attachments = new[]
{
new
{
image_url = imageUrl
}
}
};
}

Call(payload.ResponseUrl, response);
}

private void Call(string url, object result)
{
var request = WebRequest.Create(url);
request.Method = "POST";
var json = JsonConvert.SerializeObject(result);

byte[] byteArray = Encoding.UTF8.GetBytes(json);

request.ContentType = "application/json";
request.ContentLength = byteArray.Length;

using var reqStream = request.GetRequestStream();
reqStream.Write(byteArray, 0, byteArray.Length);

using var response = request.GetResponse();
_logger.LogInformation(((HttpWebResponse)response).StatusDescription);

using var respStream = response.GetResponseStream();

using var reader = new StreamReader(respStream);
var data = reader.ReadToEnd();
_logger.LogInformation(data);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ namespace SlackThrowReaction.Controllers
{
[ApiController]
[Route("[controller]")]
public class ThrowController : ControllerBase
public class GetRandomEmojiController : ControllerBase
{
private readonly ILogger<ThrowController> _logger;
private readonly ILogger<GetRandomEmojiController> _logger;
private static readonly Random _random = new Random();

private static readonly Dictionary<string, List<EmojiInfo>> _emojiesByText =
public static readonly Dictionary<string, List<EmojiInfo>> _emojiesByText =
new Dictionary<string, List<EmojiInfo>>();

public ThrowController(ILogger<ThrowController> logger)
public GetRandomEmojiController(ILogger<GetRandomEmojiController> logger)
{
_logger = logger;
_logger.Log(LogLevel.Critical, "hello");
Expand All @@ -33,7 +33,7 @@ public ThrowController(ILogger<ThrowController> logger)
Consumes("application/x-www-form-urlencoded"),
Produces("application/json")
]
public async Task<JsonResult> Post([FromForm] SlashCommand data)
public async Task<JsonResult> Post([FromForm] SlashCommandPayload data)
{
var emoji = data.Text?.ToLower();

Expand Down Expand Up @@ -67,17 +67,17 @@ public async Task<JsonResult> Post([FromForm] SlashCommand data)

var index = _random.Next(emojies.Count);
var emojiInfo = emojies[index];
var imageUrl = $"https://cdn.betterttv.net/emote/{emojiInfo.Id}/3x";

return new JsonResult(new
{
response_type = "ephemeral", //"in_channel"
delete_original = true,
attachments = new[]
{
new
{
text = emojiInfo.Code,
image_url = $"https://cdn.betterttv.net/emote/{emojiInfo.Id}/3x"
image_url = imageUrl
}
},
blocks = new []
Expand All @@ -96,8 +96,8 @@ public async Task<JsonResult> Post([FromForm] SlashCommand data)
type = "plain_text",
text = "Send"
},
value = "click_me_123",
action_id = "actionId-0"
value = imageUrl,
action_id = ActionType.Send.ToString()
},
new
{
Expand All @@ -108,8 +108,8 @@ public async Task<JsonResult> Post([FromForm] SlashCommand data)
type = "plain_text",
text = "Shuffle"
},
value = "click_me_123",
action_id = "actionId-1"
value = emoji,
action_id = ActionType.Shuffle.ToString()
},
new
{
Expand All @@ -120,8 +120,8 @@ public async Task<JsonResult> Post([FromForm] SlashCommand data)
type = "plain_text",
text = "Cancel"
},
value = "click_me_123",
action_id = "actionId-2"
value = imageUrl,
action_id = ActionType.Remove.ToString()
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions Model/ActionType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace SlackThrowReaction.Model
{
public enum ActionType
{
Send,
Shuffle,
Remove
}
}
2 changes: 1 addition & 1 deletion Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"SlackThrowReaction": {
"commandName": "Project",
"dotnetRunMessages": "true",
"launchBrowser": true,
"launchBrowser": false,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
Expand Down

0 comments on commit 39a0a48

Please sign in to comment.