LoginSignup
8
3

NoodlからLINEにメッセージを送信する(LINE Notify)

Last updated at Posted at 2020-12-19

やること

ビジュアルプログラミングツールNoodlから、LINEにメッセージを送信したかったので調べてみました。
結果として、RESTノードを使えば手間なく実装できることがわかりました。
LINE Notifyについてはアクセストークンの取得以外は説明しません。LINE Notifyを初めて使われる方は下記の記事などを参考として確認してください。

今回の成果物

下記のNoodlのサンプルでは送信ボタンを押すとTextInputの内容をLINEに送信します。

img0022.JPG

事前準備(LINE Notify)

下記手順でLINE Notifyのアクセストークンを取得しておきます。

  1. LINE Notifyにアクセスし、自身のLINE IDでログインをします。
  2. 右上のLINE ID名をクリックしてマイページに移動します。
  3. アクセストークンの発行(開発者向け)でトークンを発行するを選択します。
  4. トークン名:任意(通知時に発信元として表示されます)
  5. 送信するトークルームを選択:1:1でLINE Notifyから通知を受け取るを選択します。
  6. 発行するを選択します。
  7. これでサービスが連携し、アクセストークンが発行される。

発行されたアクセストークンをメモしておいてください。

img0028.JPG

Noodlの設定

RESTノードの設定

Resource: /api/notify
Method: POST
Endpoint: https://notify-api.line.me

RESTノードのScriptを記述

REST-Script
define({
	// The input ports of the REST node, name of input and type
	inputs:{
	    //ExampleInput:'number',
	    apikey:'string',
	    msg:'string'
	},
	
	outputs:{
	},
	
	request:function(inputs,request) {
	    request.headers={"Authorization":'Bearer '+inputs.apikey};
	    request.parameters.message=inputs.msg;
	},
	
	response:function(outputs,response) {
	}
})

### Nodeの接続とAPIキーの設定
RESTノードに送信ボタンのシグナルと、テキストインプットのテキストを接続します

  • 送信ボタンGrouup:ClickをREST:Fetchに接続します
  • TextInput:TextをREST:msgに接続します

アクセストークンの設定

LINE Notifyで発行したアクセストークンをREST:Inputsにあるapikeyに記入します。
img0024.JPG

完成!

NoodlのRESTノードを使うことで、LINEへのメッセージ送信を簡単に実装することができました。
ちなみにステッカーや画像も送付することができました。

ステッカーや画像を送信する例

define({
	// The input ports of the REST node, name of input and type
	inputs:{
	    //ExampleInput:'number',
	    apikey:'string',
	    msg:'string',
	    img:'string'
	},
	
	outputs:{
	},
	
	request:function(inputs,request) {
	    request.headers={"Authorization":'Bearer '+inputs.apikey};
	    request.parameters.message=inputs.msg;
	    //ステッカーを送信する場合
	    request.parameters.stickerPackageId=1;
	    request.parameters.stickerId=403;
	    //画像を送信する場合
	    request.parameters.imageThumbnail=inputs.img;//サムネール画像は最大240x240
	    request.parameters.imageFullsize=inputs.img;//メイン画像は最大1024x1024
	}, 
	
	response:function(outputs,response) {
	}
})

使用できるステッカーの一覧はこちらを確認してください。
画像の場合はimgのところに表示したい画像のURLを記載してください。
例えばhttps://s3-ap-northeast-1.amazonaws.com/qiita-tag-image/41b89d5d2067df05ce0e71d60deb4fe6ffdd1add/large.jpg?1565416440
とした場合、下記が送信されます。

img

8
3
3

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