如何發送LINE訊息(Push Message)

Overview

這個lab介紹如何透過 C# 以程式碼發送各種LINE基本訊息,包含貼圖、文字、圖片、聲音、影片、GPS 座標位置(Location)

Prerequisites

  1. 建立好LINE Bot帳號,並取得Channel Access Token與UserId 參考這裡arrow-up-right

  2. 下載安裝 .net core sdk 3.1 以上版本 herearrow-up-right

  3. 安裝 Visual Studio Code 開發工具 herearrow-up-right

  4. 建立 .net core console 專案,在專案中引用 nuget 上的 LineBotSDK 套件。

Steps

  1. 建立 .net core console專案 在命令列模式建立資料夾,接著透過 dotnet new 指令建立專案

PS D:\> md linetest
PS D:\> cd linetest
PS D:\linetest> dotnet new console

系統會出現類似底下畫面...

The template "Console Application" was created successfully.

Processing post-creation actions...
Running 'dotnet restore' on D:\linetest\linetest.csproj...
  正在判斷要還原的專案...
  已還原 D:\linetest\linetest.csproj (251 ms )。
Restore succeeded.

2.接著執行底下指令, 安裝 LineBotSDK 套件...

系統會出現類似底下畫面...

安裝完成後我們順便建置(Build)一下,看結果如何:

如果可以順利建置,你應該會收到類似上面這樣的訊息。

  1. 透過底下指令,開啟 VS Code進入開發環境 請在命令列輸入 code (空格) .

enter image description here 完成後會看到類似上面的畫面。

  1. 鍵入訊息發送程式碼 將上圖中的第9行程式註銷,換成底下程式碼 (請注意ChannelAccessToken和UserId 須換為你自己申請的LINE Bot相關資訊):

  1. 在VS Code以hot key(C+S+`)切換至terminal: enter image description here

  2. 鍵入 dotnet run 執行程式

  1. 檢視結果

enter image description here
enter image description here

相關參考資料

電子書:https://www.pubu.com.tw/ebook/103305arrow-up-right 實體書:https://www.tenlong.com.tw/products/9789865022662arrow-up-right 線上課程:https://www.udemy.com/line-bot/arrow-up-right LineBotSDK:https://www.nuget.org/packages/LineBotSDKarrow-up-right

如果需要即時取得更多相關訊息,可按這裡arrow-up-right加入FB專頁。若這篇文章對您有所幫助,請幫我們分享出去,謝謝您的支持。

Last updated