UIの評判が高いLINE。クラウドと組み合わせることで様々なアプリが期待できます。
と、いうわけで、今回はベース部分を作ってみました。
AWSが人気ですが、今回はあえてBluemixでチャレンジします。理由はありません、私が天邪鬼なだけです。ちなみにいずれも無料枠の範囲内で作れます。
今回はベース部分だけを作るので、話しかけると定型文を返すだけのBotです。
開発用LINEアカウント取得
LINE Bot 制作前に、コチラの記事を参考にアカウントを取得してください。
つまったところ1
Access to this API denied due to the following reason: Your ip address [***.***.***.***] is not allowed to access this API. Please add your IP to the IP whitelist in the developer center.
対策:
LINEアカウント側でWhiteListの設定をしてください。
つまったところ2
“message”:”The request body has 1 error(s)”,
“details”:[{“message”:”May not be empty”,”property”:”messages”}]}”
対策
POSTするときのpayloadでMessagesを配列形式で定義してください
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
var messages = []; var msg1 = { "type" : "text", "text" : "ここにBotからの返信メッセージ" }; messages.push(msg1); var to = "送信先のUserIdなどを指定" var payload = { "to": to, "type": "text", "messages": messages }; |
NodeRED用のソース。
クリップボードに以下を張り付けて、トークンだけご自身のアカウントのものに差し替えればOKです。
1 |
[{"id":"17e65a56.2638e6","type":"http in","z":"bcd219ec.93e288","name":"roadroller[POST]","url":"/roadroller","method":"post","upload":false,"swaggerDoc":"","x":100,"y":260,"wires":[["a346b7cf.87eb58"]]},{"id":"99869b7b.ba0038","type":"http request","z":"bcd219ec.93e288","name":"send_result","method":"POST","ret":"txt","url":"https://api.line.me/v2/bot/message/push","tls":"","x":590,"y":260,"wires":[["58b515c4.8fb8fc","eb614b2f.e83a58"]]},{"id":"a346b7cf.87eb58","type":"function","z":"bcd219ec.93e288","name":"process_result","func":"\nmsg.headers = {\n \"Authorization\":\"Bearer 'アカウントのtoken'\",\n \"Content-type\": \"application/json\",\n};\n\nvar to = msg.payload.events[0].source.userId\n\nvar messages = [];\nvar msg1 = {\n \"type\" : \"text\",\n \"text\" : \"今日の君は素敵だよ\"\n};\n\nmessages.push(msg1);\n\n\nvar payload = {\n \"to\": to,\n \"type\": \"text\",\n \"messages\": messages\n};\n\nmsg.payload = payload\n\n\n\nreturn msg\n\n","outputs":1,"noerr":0,"x":360,"y":260,"wires":[["d07c5f7.fd6a4a","99869b7b.ba0038"]]},{"id":"eb614b2f.e83a58","type":"http response","z":"bcd219ec.93e288","name":"response","statusCode":"","headers":{},"x":860,"y":260,"wires":[]},{"id":"58b515c4.8fb8fc","type":"debug","z":"bcd219ec.93e288","name":"sent_message","active":true,"console":"false","complete":"true","x":800,"y":380,"wires":[]},{"id":"d07c5f7.fd6a4a","type":"json","z":"bcd219ec.93e288","name":"","pretty":false,"x":290,"y":380,"wires":[["58b515c4.8fb8fc"]]},{"id":"4987feeb.af4ea","type":"comment","z":"bcd219ec.93e288","name":"リンク集","info":"https://devdocs.line.me/ja/#messaging-api\n\nhttps://admin-official.line.me/\n\nhttps://business.line.me/ja/\n\nhttps://developers.line.me/\n\nhttps://www.niandc.co.jp/sol/tech/date20170525_674.php","x":80,"y":380,"wires":[]}] |
以上