BLOG
LINE Messaging APIを簡単に試してみました
2016/10/11
先日LINEの新APIが公開されていろいろ検証しています。
簡単にcurlを使おうと思いますがいくつか注意点があります。
サンプルcurl
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
curl -X POST \ -H 'Content-Type:application/json' \ -H 'Authorization: Bearer {ENTER_ACCESS_TOKEN}' \ -d '{ "to": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "messages":[ { "type": "template", "altText": "this is a buttons template", "template": { "type": "buttons", "thumbnailImageUrl": "https://example.com/bot/images/image.jpg", "title": "Menu", "text": "Please select", "actions": [ { "type": "postback", "label": "Buy", "data": "action=buy&itemid=123" }, { "type": "postback", "label": "Add to cart", "data": "action=add&itemid=123" }, { "type": "uri", "label": "View detail", "uri": "http://example.com/page/123" } ] } } ] }' https://api.line.me/v2/bot/message/push |
・ACCESS TOKENはLINE Developersの管理画面より取得できます。
・LINE Developersの管理画面に設定するWebhook URLはhttps環境を用意しなくてはなりません。
・toは送信先識別子です。
toの調べ方
Webhook URLにアクセス(メッセージを送る)しlogを出力するようにします。
下記はphp出力する例です。
1 2 3 4 5 6 |
<?php ob_start(); $raw = file_get_contents('php://input'); var_dump(json_decode($raw,1)); $raw = ob_get_clean(); file_put_contents(‘log.txt', $raw.”\n————\n”, FILE_APPEND); |
logに出力されているuserIdを使います。
実際これらを使い、curlを叩いて成功すると{}が返ってきます。
そしてメッセージが送信されてきます。