LoginSignup
3
2

More than 3 years have passed since last update.

Node.js から Bot API で LINE WORKS にメッセージ送信

Last updated at Posted at 2019-07-02

Node.js で LINE WORKS にメッセージ送信

Node.js の request で LINE WORKS のトーク BOT API を実行し、任意のメンバーやトークルームにメッセージを送る方法です。

API のバージョンアップに伴い、記事を修正しました。
https://qiita.com/iwaohig/items/4b0717f2c66b74b74d0c

事前作業

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

var request = require('request');
var options = {
  uri: 'https://apis.worksmobile.com/r/<API ID>/message/v1/bot/<bot No.>/message/push',
  headers: {
    'Content-Type': 'application/json; charset=UTF-8',
    'consumerKey': '<Server API Consumer Key>',
    'Authorization': 'Bearer <Server Token>'
  },
  json: {
    'accountId': '<user id>',
    'content': {
        'type': 'text',
        'text': '<メッセージ本文>'
  }
 }
};
request.post(options, function(error, response, body){});

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

3
2
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
3
2