77using System . Net . Http ;
88using System . Text ;
99using System . Text . Json ;
10+ using System . Text . RegularExpressions ;
1011using System . Threading . Tasks ;
1112using Microsoft . AspNetCore . Mvc ;
1213using 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 ( ) ;
0 commit comments