LINE Messaging APIを触ってみました。ということで備忘録。
設定とAPIに必要なパラメータの取得
設定画面へはトップ画面でログインして、事業者を選択します。あとは対象のアカウントを選択して各設定画面に移動します。
LINE@MANAGERのBot設定でWebhook設定を有効化する
LINE DevelopersのBasic InformationでWebhook URLを設定し、Channel SecretとChannel Access Tokenを取得。
Webhookのリクエスト例
フォロー時Accept: */*
Connection: close
Content-Length: 174
Content-Type: application/json;charset=UTF-8
Host:
User-Agent: LineBotWebhook/1.0
X-Line-Signature: {Signature}
{
"events": [
{
"type": "follow",
"replyToken": "{ReplyToken}",
"source": {
"userId": "{UserId}",
"type": "user"
},
"timestamp": 1478677328858
}
]
}
メッセージ受信時
Accept: */*
Connection: close
Content-Length: 241
Content-Type: application/json;charset=UTF-8
Host:
User-Agent: LineBotWebhook/1.0
X-Line-Signature: {Signature}
{
"events": [
{
"type": "message",
"replyToken": "{ReplyToken}",
"source": {
"userId": "{UserId}",
"type": "user"
},
"timestamp": 1478678945668,
"message": {
"type": "text",
"id": "5179620358183",
"text": "ほげー"
}
}
]
}
Signatureを手元で確認したい場合は、以下のコマンドで確認可能です。
$ echo -n '{MessageBody}' |
openssl dgst -sha256 -hmac "{ChannelSecret}" -binary | base64
APIを叩く
Reply Message$ curl -X POST \
-H 'Content-Type:application/json' \
-H 'Authorization: Bearer {AccessToken}' \
-d '{
"replyToken":"{ReplyToken}",
"messages":[
{
"type":"text",
"text":"Hoge"
},
{
"type":"text",
"text":"Fuga?"
}
]
}' https://api.line.me/v2/bot/message/reply
Push Message
$ curl -X POST \
-H 'Content-Type:application/json' \
-H 'Authorization: Bearer {AccessToken}' \
-d '{
"to": "{UserId}",
"messages":[
{
"type":"text",
"text":"Hello, world1"
},
{
"type":"text",
"text":"Hello, world2"
}
]
}' https://api.line.me/v2/bot/message/push
Get Profile
$ curl -X GET \
-H 'Authorization: Bearer {AccessToken}' \
https://api.line.me/v2/bot/profile/{UserId}