-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
133 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace SlackThrowReaction.Model | ||
{ | ||
public enum ActionType | ||
{ | ||
Send, | ||
Shuffle, | ||
Remove | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters