7
8

More than 1 year has passed since last update.

PowerShell から トーク Bot API で LINE WORKS にメッセージ送信 (LINE WORKS API 1.0)

Last updated at Posted at 2019-04-21

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

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

この記事は LINE WORKS API 1.0 に沿った記載です。API 1.0 は 2023 年 4 月 30 日にリタイア予定です。API 2.0 に準じた記事を以下で掲載しています。
https://qiita.com/iwaohig/items/85a40c6c01d6486a53eb

事前作業

LINE WORKS でトークボット API を利用するため、LINE WORKS の Developer Console で、以下の事前作業を行います。

◆ API 認証の準備

  • API ID 発行
  • サーバー API のコンシューマーキー発行
  • サーバー API の Server Token

手順はこちらのページの情報を参照します。
https://developers.worksmobile.com/jp/document/1002002?lang=ja

◆ Bot 登録
Bot メニューで [登録] ボタンをクリックし、以下の項目を入力し、保存をクリック

  • Bot名 -> 任意の名前
  • 「説明」-> 任意の説明文
  • Callback URL -> 今回は Off
  • Botポリシー -> 用途に応じて設定
  • 管理者 -> 任意のメンバーを指定

登録すると Bot No. が生成されるので、確認しておきます。

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

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

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

$header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$header.Add("Content-Type","application/json; charset=UTF-8")
$header.Add("consumerKey","<Server API Consumer Key>")
$header.Add("Authorization","Bearer <Server Token>")
$header.Add("Cache-Control","no-cache")

$request = @"
{
    "accountId": "<user id>",
    "content": {
        "type": "text",
        "text": "<メッセージ本文>"
    }
}
"@


$Body = [System.Text.Encoding]::UTF8.GetBytes($request)

$Uri = "https://apis.worksmobile.com/r/{API ID}/message/v1/bot/{botNo}/message/push"
Invoke-RestMethod -Uri $Uri -ContentType 'application/json' -Method Post -Header $header -Body $Body

上記により、LINE WORKS へトーク メッセージの送信が可能です。

7
8
0

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
  3. You can use dark theme
What you can do with signing up
7
8