Skip to content

Commit

Permalink
Merge pull request #24 from YDKK/develop
Browse files Browse the repository at this point in the history
ユーザアイコンとユーザ名の書き換えに対応
  • Loading branch information
YDKK authored Jun 6, 2023
2 parents 234a19e + 4ba23d5 commit 256b9cc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 45 deletions.
61 changes: 19 additions & 42 deletions SlackLineBridge/Controllers/WebhookController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public async Task<IActionResult> Slack2()

string text = data.@event.text;
string userId = data.@event.user;
string userName = await GetSlackUserName(userId);
var user = await GetSlackUserNameAndIcon(userId);

JsonDynamicArray files = data.@event.files;
SlackFile[] slackFiles = null;
Expand All @@ -124,7 +124,7 @@ public async Task<IActionResult> Slack2()
}).ToArray();
}

return await PushToLine(Request.Host.ToString(), slackChannel, userName, text, slackFiles);
return await PushToLine(Request.Host.ToString(), slackChannel, user.icon, user.userName, text, slackFiles);
}
break;
}
Expand All @@ -138,15 +138,15 @@ public async Task<IActionResult> Slack2()
return BadRequest();
}

private async Task<string> GetSlackUserName(string userId)
private async Task<(string userName, string icon)> GetSlackUserNameAndIcon(string userId)
{
var client = _clientFactory.CreateClient("Slack");
var result = await client.GetAsync($"https://slack.com/api/users.profile.get?user={userId}");
var json = await result.Content.ReadAsStringAsync();
dynamic data = JsonSerializer.Deserialize<dynamic>(json, _jsonOptions);
string name = data.profile.display_name;
(string name, string icon) = (data.profile.display_name, data.profile.image_512);

return name;
return (name, icon);
}

private record SlackFile
Expand All @@ -156,7 +156,7 @@ private record SlackFile
public string mimeType { get; set; }
}

private async Task<IActionResult> PushToLine(string host, SlackChannel slackChannel, string userName, string text, SlackFile[] files = null)
private async Task<IActionResult> PushToLine(string host, SlackChannel slackChannel, string userIconUrl, string userName, string text, SlackFile[] files = null)
{
var bridges = GetBridges(slackChannel);
if (!bridges.Any())
Expand All @@ -180,41 +180,13 @@ private async Task<IActionResult> PushToLine(string host, SlackChannel slackChan
{
var message = new
{
type = "flex",
altText = $"{userName}\r\n{text}",
contents = new
{
type = "bubble",
size = "giga",
body = new
{
type = "box",
layout = "vertical",
contents = new dynamic[]
{
new
{
type = "text",
text = userName,
weight = "bold",
wrap = true,
size = "xs"
},
new
{
type = "separator",
margin = "sm"
},
new
{
type = "text",
text = text,
wrap = true,
margin = "sm"
}
}
}
}
type = "text",
altText = text,
text = text,
sender = new {
name = userName,
iconUrl = $"https://{host}/proxy/slack/{Crypt.GetHMACHex(userIconUrl, _slackSigningSecret)}/{HttpUtility.UrlEncode(userIconUrl)}"
},
};
var urlMessages = urls.Select(x => x.Groups["url"].Value).Select(x => new
{
Expand Down Expand Up @@ -243,7 +215,12 @@ private async Task<IActionResult> PushToLine(string host, SlackChannel slackChan
{
type = "image",
originalContentUrl = $"https://{host}/proxy/slack/{Crypt.GetHMACHex(urlPrivate, _slackSigningSecret)}/{HttpUtility.UrlEncode(urlPrivate)}",
previewImageUrl = $"https://{host}/proxy/slack/{Crypt.GetHMACHex(urlThumb360, _slackSigningSecret)}/{HttpUtility.UrlEncode(urlThumb360)}"
previewImageUrl = $"https://{host}/proxy/slack/{Crypt.GetHMACHex(urlThumb360, _slackSigningSecret)}/{HttpUtility.UrlEncode(urlThumb360)}",
sender = new
{
name = userName,
iconUrl = $"https://{host}/proxy/slack/{Crypt.GetHMACHex(userIconUrl, _slackSigningSecret)}/{HttpUtility.UrlEncode(userIconUrl)}"
},
};
});
var json = new
Expand Down
4 changes: 2 additions & 2 deletions SlackLineBridge/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80

FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["SlackLineBridge/SlackLineBridge.csproj", "SlackLineBridge/"]
RUN dotnet restore "SlackLineBridge/SlackLineBridge.csproj"
Expand Down
2 changes: 1 addition & 1 deletion SlackLineBridge/SlackLineBridge.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>

Expand Down

0 comments on commit 256b9cc

Please sign in to comment.