Skip to content

Commit 256b9cc

Browse files
authored
Merge pull request #24 from YDKK/develop
ユーザアイコンとユーザ名の書き換えに対応
2 parents 234a19e + 4ba23d5 commit 256b9cc

3 files changed

Lines changed: 22 additions & 45 deletions

File tree

SlackLineBridge/Controllers/WebhookController.cs

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public async Task<IActionResult> Slack2()
109109

110110
string text = data.@event.text;
111111
string userId = data.@event.user;
112-
string userName = await GetSlackUserName(userId);
112+
var user = await GetSlackUserNameAndIcon(userId);
113113

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

127-
return await PushToLine(Request.Host.ToString(), slackChannel, userName, text, slackFiles);
127+
return await PushToLine(Request.Host.ToString(), slackChannel, user.icon, user.userName, text, slackFiles);
128128
}
129129
break;
130130
}
@@ -138,15 +138,15 @@ public async Task<IActionResult> Slack2()
138138
return BadRequest();
139139
}
140140

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

149-
return name;
149+
return (name, icon);
150150
}
151151

152152
private record SlackFile
@@ -156,7 +156,7 @@ private record SlackFile
156156
public string mimeType { get; set; }
157157
}
158158

159-
private async Task<IActionResult> PushToLine(string host, SlackChannel slackChannel, string userName, string text, SlackFile[] files = null)
159+
private async Task<IActionResult> PushToLine(string host, SlackChannel slackChannel, string userIconUrl, string userName, string text, SlackFile[] files = null)
160160
{
161161
var bridges = GetBridges(slackChannel);
162162
if (!bridges.Any())
@@ -180,41 +180,13 @@ private async Task<IActionResult> PushToLine(string host, SlackChannel slackChan
180180
{
181181
var message = new
182182
{
183-
type = "flex",
184-
altText = $"{userName}\r\n{text}」",
185-
contents = new
186-
{
187-
type = "bubble",
188-
size = "giga",
189-
body = new
190-
{
191-
type = "box",
192-
layout = "vertical",
193-
contents = new dynamic[]
194-
{
195-
new
196-
{
197-
type = "text",
198-
text = userName,
199-
weight = "bold",
200-
wrap = true,
201-
size = "xs"
202-
},
203-
new
204-
{
205-
type = "separator",
206-
margin = "sm"
207-
},
208-
new
209-
{
210-
type = "text",
211-
text = text,
212-
wrap = true,
213-
margin = "sm"
214-
}
215-
}
216-
}
217-
}
183+
type = "text",
184+
altText = text,
185+
text = text,
186+
sender = new {
187+
name = userName,
188+
iconUrl = $"https://{host}/proxy/slack/{Crypt.GetHMACHex(userIconUrl, _slackSigningSecret)}/{HttpUtility.UrlEncode(userIconUrl)}"
189+
},
218190
};
219191
var urlMessages = urls.Select(x => x.Groups["url"].Value).Select(x => new
220192
{
@@ -243,7 +215,12 @@ private async Task<IActionResult> PushToLine(string host, SlackChannel slackChan
243215
{
244216
type = "image",
245217
originalContentUrl = $"https://{host}/proxy/slack/{Crypt.GetHMACHex(urlPrivate, _slackSigningSecret)}/{HttpUtility.UrlEncode(urlPrivate)}",
246-
previewImageUrl = $"https://{host}/proxy/slack/{Crypt.GetHMACHex(urlThumb360, _slackSigningSecret)}/{HttpUtility.UrlEncode(urlThumb360)}"
218+
previewImageUrl = $"https://{host}/proxy/slack/{Crypt.GetHMACHex(urlThumb360, _slackSigningSecret)}/{HttpUtility.UrlEncode(urlThumb360)}",
219+
sender = new
220+
{
221+
name = userName,
222+
iconUrl = $"https://{host}/proxy/slack/{Crypt.GetHMACHex(userIconUrl, _slackSigningSecret)}/{HttpUtility.UrlEncode(userIconUrl)}"
223+
},
247224
};
248225
});
249226
var json = new

SlackLineBridge/Dockerfile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
1+
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
22
WORKDIR /app
33
EXPOSE 80
44

5-
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
5+
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
66
WORKDIR /src
77
COPY ["SlackLineBridge/SlackLineBridge.csproj", "SlackLineBridge/"]
88
RUN dotnet restore "SlackLineBridge/SlackLineBridge.csproj"

SlackLineBridge/SlackLineBridge.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>net5.0</TargetFramework>
4+
<TargetFramework>net6.0</TargetFramework>
55
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
66
</PropertyGroup>
77

0 commit comments

Comments
 (0)