LoginSignup
1

posted at

updated at

Organization

PowerShell で LINE WORKS にメッセージ送信 (LINE WORKS API 2.0)

PowerShell で LINE WORKS にメッセージ送信

PowerShell のコマンドレット操作で LINE WORKS のトーク BOT API を用いて、任意のメンバーやトークルームにメッセージを送る方法です。

以下の旧 API 対応記事の API 2.0 対応版です。
https://qiita.com/iwaohig/items/92a3b5b4af288c22a96a

事前作業

LINE WORKS でトークボット API を利用するため、LINE WORKS の Developer Console と管理画面で API 2.0 や Bot の設定を行ってください。

トーク Bot API によるメッセージ送信

LINE WORKS の API 2.0 の トーク Bot API の解説はこちらのページです。
https://developers.worksmobile.com/jp/reference/bot?lang=ja

PowerShell の Invoke-RestMethod コマンドレットで、トーク Bot API を実行します。

{Server Token} → トークンを入力
{botId} → botId を入力
{userId} → 送信先の userId を入力

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json")
$headers.Add("Authorization", "Bearer {Server Token}")

$payload = @"
{
    "content": {
        "type": "text",
        "text": "$args"
    }
}
"@
$Body = [System.Text.Encoding]::UTF8.GetBytes($payload)
Invoke-RestMethod 'https://www.worksapis.com/v1.0/bots/{botId}/users/{userId}/messages' -Method 'POST' -Headers $headers -Body $body

PowerShell で実行。
l_1517737_1983_1a9b726e4b0641cb930b568180f41c08.png

指定した Bot からメッセージが届く。
l_1517737_1984_1c48813f403b385983f8b86a32b5a293.png

以上です。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
1