Skip to content

Commit

Permalink
URL対応 (#15)
Browse files Browse the repository at this point in the history
* 🎉 .NET 5.0 🎉

* URLを普通のメッセージとして別に送信するように変更
fix #14
  • Loading branch information
YDKK authored Dec 20, 2020
1 parent 0b07f92 commit 4e58faa
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ jobs:
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.0.100
dotnet-version: 5.0.101
- name: Build with dotnet
run: dotnet build --configuration Release
47 changes: 30 additions & 17 deletions SlackLineBridge/Controllers/WebhookController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
Expand All @@ -27,6 +28,7 @@ public class WebhookController : ControllerBase
private readonly SlackLineBridges _bridges;
private readonly IHttpClientFactory _clientFactory;
private readonly ConcurrentQueue<(string signature, string body, string host)> _lineRequestQueue;
private static readonly Regex _urlRegex = new Regex(@"(\<(?<url>http[^\|\>]+)\|?.*?\>)");

public WebhookController(
ILogger<WebhookController> logger,
Expand Down Expand Up @@ -63,6 +65,10 @@ public async Task<OkResult> Slack([FromForm] SlackData data)
{
return Ok();
}

//URLタグを抽出
var urls = _urlRegex.Matches(data.text);

foreach (var bridge in bridges)
{
var lineChannel = _lineChannels.Channels.FirstOrDefault(x => x.Name == bridge.Line);
Expand All @@ -72,24 +78,19 @@ public async Task<OkResult> Slack([FromForm] SlackData data)
return Ok();
}

var json = new
var message = new
{
to = lineChannel.Id,
messages = new[]
type = "flex",
altText = $"{data.user_name}\r\n{data.text}",
contents = new
{
new
type = "bubble",
size = "kilo",
body = new
{
type = "flex",
altText = $"{data.user_name}\r\n{data.text}",
contents = new
{
type = "bubble",
size = "kilo",
body = new
{
type = "box",
layout = "vertical",
contents = new dynamic[]
type = "box",
layout = "vertical",
contents = new dynamic[]
{
new
{
Expand All @@ -112,11 +113,23 @@ public async Task<OkResult> Slack([FromForm] SlackData data)
margin = "sm"
}
}
}
}
}
}
};
var urlMessages = urls.Select(x => x.Groups["url"].Value).Select(x => new
{
type = "text",
text = x
});

var json = new
{
to = lineChannel.Id,
messages = new dynamic[]
{
message
}.Concat(urlMessages).ToArray()
};
await _clientFactory.CreateClient("Line").PostAsync($"message/push", new StringContent(JsonSerializer.Serialize(json), Encoding.UTF8, "application/json"));
}
return Ok();
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/core/aspnet:3.0-buster-slim AS base
FROM mcr.microsoft.com/dotnet/aspnet:5.0 AS base
WORKDIR /app
EXPOSE 80

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

Expand Down

0 comments on commit 4e58faa

Please sign in to comment.