使用NUGET
StartFMS.Extensions.Line
LineBotSDK
StartFMS.Extensions.Configuration
StartFMS.Extensions.Line 本章節使用 v1.1.1 版本,版本已經釋出 Line Login 、Line Notify 正常使用,設定格是可以參考下方json設定檔。下方範例如果有保留值表示官方提供傳值設定,無須更改。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 "Line" : { "Bots" : { "channelToken" : "" , "adminUserID" : "" } , "Login" : { "channelToken" : "" , "adminUserID" : "" , "openIdConnect" : { "url" : "https://access.line.me/oauth2/v2.1/authorize" , "response_type" : "code" , "client_id" : "" , "redirect_uri" : "" , "state" : "12345abcde" , "scope" : "profile openid" , "nonce" : "09876xyz" } } , "Notify" : { "channelToken" : "" }
Line Notify Notify 只需要設定一個 Token 無需要取得其他資料
StartFMS.Extensions.Configuration 本章節雖然非必要存在,後續章節會介紹到這個使用方式。如果要使用【管理使用者密碼】可以使用下方指令,最新版本為v1.0.1 可以使用,並且還支援 Azure 取得值的功能。
1 2 3 4 5 var config = Config.GetConfiguration<Program>(); var config = Config.GetConfiguration<Startup>();
(正篇) 使用Line Notify
設定參數
1 2 3 4 5 var lineNotify = new LineNotify(){ ChannelToken = config.GetValue<string >("Line:Notify:channelToken" ), }; builder.Services.AddSingleton<LineNotify>(lineNotify);
加入 Controller
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 using Microsoft.AspNetCore.Mvc;using Newtonsoft.Json;using StartFMS.Extensions.Line;using StartFMS.Models.Backend;using StartFMS.Partner.API.Helper;namespace StartFMS.Partner.API.Controllers ;[ApiController ] [Route("/api/Line/Notify/v1.0/" ) ] public class LineNotifyV1Controller : ControllerBase { private LineNotify _LineNotify; public LineNotifyV1Controller (LineNotify LineNotify ) { _LineNotify = LineNotify; } [HttpGet ] public string SendMessage () { _LineNotify.Send($"發送訊息時間 : {DateTime.Now} " ); return JsonConvert.SerializeObject(new { Success = true , Message = "" }); } }