起因
開發過程中,使用 LineBotSDK 相當不順手以及建立方式,後面我另外把LineBotSDK 另外用 NET Core 版本擴充,方便後續引用、覆寫。請參考使用方式。
所需套件
備註: 初版暫時使用兩種套件合併使用,後續看看有沒有空去修正現行狀況。
使用方式
本範例 .NET6.0 展示,若有其他使用操作問題,可以在下方留言。
一、新創一個類別
創建一個新的 Class 改寫LineBots 內容,後續會比較好使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
   |  using StartFMS.Extensions.Line; namespace StartFMS.Partner.API.Helper {     public class LineBot:LineBots     {         public override void MessageText()         {             var @event = ReceivedMessage.events.FirstOrDefault();             string message = @event!=null ? @event.message.text:"";             ReplyMessage(message);                      }     } }
 
  | 
 
二、設定 Program.cs 加入以下設定
加入 StartFMS.Partner.API.Helper.LineBot 類別。 (並不是 StartFMS.Extensions.Line.LineBots)
1 2 3 4 5 6
   |  var lineBots = new LineBot() {     ChannelToken = config.GetValue<string>("Line:Bots:channelToken"),     AdminUserID = config.GetValue<string>("Line:Bots:adminUserID") }; builder.Services.AddSingleton<LineBot>(lineBots);
 
  | 
 
三、加入Controllers
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
   | [HttpPost("", Name = "Message Reply")] public async Task<string> Post() {
      try     {                  using (var linebot = await _lineBots.LoadAsync(Request.Body))         {                          linebot.ExecuteReader();          }
          return JsonConvert.SerializeObject(new         {             Success = true,             Message = "",         });     }     catch(Exception ex) {         return JsonConvert.SerializeObject(new         {             Success = false,             Message = ex.Message,         });     }
  }
  | 
 
注意事項
設定上面只需要以上動作即可,目前簡化到直接透過 Helper 方式進行修改執行內容。為了達到簡化效果本擴充有提供Function ,只需要針對Function 進行修改即可。
override method
Behavior method
function 分為兩段 
- Join : 加入機器人
 
- Message : 留言
 
a. Join
加入機器人之後Bot動作行為。
1
   | public override void Join() { }
  | 
 
b.Message
如果是第一次使用以下 Function 不建議直接修改,本擴充有把 Message 回傳類別特別回傳到指定位置。
1
   | public override void Message() { }
  | 
 
Message method
以下是使用者透過下面 Function 選擇、讀取,也是本擴充工具的精華。overrid 建議由這邊function 進行修改,目前實測下來相當好用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
   |  public override void MessageText() { }
 
  public override void MessageImage() { }
 
  public override void MessageVideo() { }
 
  public override void MessageAudio() { }
 
  public override void MessageSticker() { }
 
  public override void MessageLocation() { }
 
  | 
 
完成畫面如下,希望大家可以順利執行Line Bot 嚕。
