Skip to content

Commit 4e58faa

Browse files
authored
URL対応 (#15)
* 🎉 .NET 5.0 🎉 * URLを普通のメッセージとして別に送信するように変更 fix #14
1 parent 0b07f92 commit 4e58faa

4 files changed

Lines changed: 34 additions & 21 deletions

File tree

.github/workflows/pull_request.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ jobs:
1212
- name: Setup .NET Core
1313
uses: actions/setup-dotnet@v1
1414
with:
15-
dotnet-version: 3.0.100
15+
dotnet-version: 5.0.101
1616
- name: Build with dotnet
1717
run: dotnet build --configuration Release

SlackLineBridge/Controllers/WebhookController.cs

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Net.Http;
88
using System.Text;
99
using System.Text.Json;
10+
using System.Text.RegularExpressions;
1011
using System.Threading.Tasks;
1112
using Microsoft.AspNetCore.Mvc;
1213
using Microsoft.Extensions.Configuration;
@@ -27,6 +28,7 @@ public class WebhookController : ControllerBase
2728
private readonly SlackLineBridges _bridges;
2829
private readonly IHttpClientFactory _clientFactory;
2930
private readonly ConcurrentQueue<(string signature, string body, string host)> _lineRequestQueue;
31+
private static readonly Regex _urlRegex = new Regex(@"(\<(?<url>http[^\|\>]+)\|?.*?\>)");
3032

3133
public WebhookController(
3234
ILogger<WebhookController> logger,
@@ -63,6 +65,10 @@ public async Task<OkResult> Slack([FromForm] SlackData data)
6365
{
6466
return Ok();
6567
}
68+
69+
//URLタグを抽出
70+
var urls = _urlRegex.Matches(data.text);
71+
6672
foreach (var bridge in bridges)
6773
{
6874
var lineChannel = _lineChannels.Channels.FirstOrDefault(x => x.Name == bridge.Line);
@@ -72,24 +78,19 @@ public async Task<OkResult> Slack([FromForm] SlackData data)
7278
return Ok();
7379
}
7480

75-
var json = new
81+
var message = new
7682
{
77-
to = lineChannel.Id,
78-
messages = new[]
83+
type = "flex",
84+
altText = $"{data.user_name}\r\n{data.text}」",
85+
contents = new
7986
{
80-
new
87+
type = "bubble",
88+
size = "kilo",
89+
body = new
8190
{
82-
type = "flex",
83-
altText = $"{data.user_name}\r\n{data.text}」",
84-
contents = new
85-
{
86-
type = "bubble",
87-
size = "kilo",
88-
body = new
89-
{
90-
type = "box",
91-
layout = "vertical",
92-
contents = new dynamic[]
91+
type = "box",
92+
layout = "vertical",
93+
contents = new dynamic[]
9394
{
9495
new
9596
{
@@ -112,11 +113,23 @@ public async Task<OkResult> Slack([FromForm] SlackData data)
112113
margin = "sm"
113114
}
114115
}
115-
}
116-
}
117116
}
118117
}
119118
};
119+
var urlMessages = urls.Select(x => x.Groups["url"].Value).Select(x => new
120+
{
121+
type = "text",
122+
text = x
123+
});
124+
125+
var json = new
126+
{
127+
to = lineChannel.Id,
128+
messages = new dynamic[]
129+
{
130+
message
131+
}.Concat(urlMessages).ToArray()
132+
};
120133
await _clientFactory.CreateClient("Line").PostAsync($"message/push", new StringContent(JsonSerializer.Serialize(json), Encoding.UTF8, "application/json"));
121134
}
122135
return Ok();

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/core/aspnet:3.0-buster-slim AS base
1+
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
22
WORKDIR /app
33
EXPOSE 80
44

5-
FROM mcr.microsoft.com/dotnet/core/sdk:3.0-buster AS build
5+
FROM mcr.microsoft.com/dotnet/sdk:5.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>netcoreapp3.0</TargetFramework>
4+
<TargetFramework>net5.0</TargetFramework>
55
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
66
</PropertyGroup>
77

0 commit comments

Comments
 (0)