Libro.LineMessageAPI.Extensions 2.1.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package Libro.LineMessageAPI.Extensions --version 2.1.5
                    
NuGet\Install-Package Libro.LineMessageAPI.Extensions -Version 2.1.5
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Libro.LineMessageAPI.Extensions" Version="2.1.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Libro.LineMessageAPI.Extensions" Version="2.1.5" />
                    
Directory.Packages.props
<PackageReference Include="Libro.LineMessageAPI.Extensions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Libro.LineMessageAPI.Extensions --version 2.1.5
                    
#r "nuget: Libro.LineMessageAPI.Extensions, 2.1.5"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Libro.LineMessageAPI.Extensions@2.1.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Libro.LineMessageAPI.Extensions&version=2.1.5
                    
Install as a Cake Addin
#tool nuget:?package=Libro.LineMessageAPI.Extensions&version=2.1.5
                    
Install as a Cake Tool

Libro.LineMessageAPI

GitHub release CI CodeQL NuGet NuGet downloads License Issues PRs .NET

此專案為 LINE Messaging API 的 C# SDK,並以開源專案結構進行整理,方便維護與擴充。

NuGet

dotnet add package Libro.LineMessageAPI

若要使用 DI/Options 方便註冊,請加裝擴充套件:

dotnet add package Libro.LineMessageAPI.Extensions

快速使用範例

1) 直接建立實例

using Libro.LineMessageApi;

var channelAccessToken = Environment.GetEnvironmentVariable("LINE_CHANNEL_ACCESS_TOKEN");
if (string.IsNullOrWhiteSpace(channelAccessToken))
{
    throw new InvalidOperationException("缺少 LINE_CHANNEL_ACCESS_TOKEN");
}

var sdk = new LineSdkBuilder(channelAccessToken)
    .UseBot()
    .UseMessages()
    .Build();

2) 透過 DI 注入

using Libro.LineMessageApi;
using Libro.LineMessageApi.Extensions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

var services = new ServiceCollection();
var configuration = new ConfigurationBuilder()
    .AddEnvironmentVariables()
    .Build();

services.AddLineSdk(
    configuration,
    sdkBuilder => sdkBuilder
        .UseBot()
        .UseMessages());

var serviceProvider = services.BuildServiceProvider();
var sdk = serviceProvider.GetRequiredService<LineSdk>();

3) Webhook 驗證與回覆訊息(最短流程)

using Libro.LineMessageApi;
using Libro.LineMessageApi.LineMessageObject;
using Libro.LineMessageApi.LineReceivedObject;
using System.Text.Json;

public async Task<IActionResult> Webhook(HttpRequestMessage request, string channelSecret, string channelAccessToken)
{
    if (!LineChannel.VaridateSignature(request, channelSecret))
    {
        return new UnauthorizedResult();
    }

    var body = await request.Content.ReadAsStringAsync();
    var payload = JsonSerializer.Deserialize<LineReceivedMsg>(body);
    var replyToken = payload?.events?[0]?.replyToken;

    if (!string.IsNullOrWhiteSpace(replyToken))
    {
        var sdk = new LineSdkBuilder(channelAccessToken)
            .UseMessages()
            .Build();

        await sdk.Messages!.SendReplyMessageAsync(replyToken, new TextMessage("收到!"));
    }

    return new OkResult();
}

LINE Messaging API 2.0 快速使用

請先閱讀「LINE Messaging API 2.0 規格速覽與快速上手」,內含支援端點與最短上手流程。
docs/line-message-api-2.0.md

Wiki 同步

本 repo 以 docs/wiki/ 作為文件來源,可用腳本同步到 GitHub Wiki repo:

pwsh scripts/sync-wiki.ps1
pwsh scripts/sync-wiki.ps1 -Commit
pwsh scripts/sync-wiki.ps1 -Commit -Push

授權

本專案採用 MIT License,詳見 LICENSE

範例:API 與 Dashboard 兩種流程

此範例提供兩條路徑:

  1. API 範例:透過設定或環境變數注入 Channel Access Token / Secret
  2. Dashboard 範例:由頁面輸入並存於記憶體,提供快速驗證與即時事件流

API 範例(注入或環境變數)

設定 LineChannel

{
  "LineChannel": {
    "ChannelAccessToken": "YOUR_CHANNEL_ACCESS_TOKEN",
    "ChannelSecret": "YOUR_CHANNEL_SECRET"
  }
}

或使用環境變數:

  • LineChannel__ChannelAccessToken
  • LineChannel__ChannelSecret

Webhook 入口:

  • POST /line/hook

Dashboard 範例(頁面設定)

新增 Bootstrap 5 + Vue 的 Web UI 範例,支援輸入 Token/Secret、設定 Webhook Endpoint,並即時顯示 webhook 事件。

詳細說明請見: docs/example-line-webhook-dashboard.md

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.1.7 118 2/5/2026
2.1.5 114 2/5/2026