現在LINE messaging APIから来た入力内容phpでオウム返しするようにしています。
次はphpからpythonに入力内容をPOSTして、pythonで処理をしてphpを通して返すように変えたいです。
なにか実装のヒントをいただけたらと思います。
よろしくお願いいたします。
php
1<?php 2 3$accessToken = 'アクセストークンの記述'; 4 5$jsonString = file_get_contents('php://input'); 6error_log($jsonString); 7$jsonObj = json_decode($jsonString); 8 9$message = $jsonObj->{"events"}[0]->{"message"}; 10$replyToken = $jsonObj->{"events"}[0]->{"replyToken"}; 11 12 送られてきたメッセージの中身からレスポンスのタイプを選択 13if ($message->{"text"} == '確認') { 14 // 確認ダイアログタイプ 15 $messageData = [ 16 'type' => 'template', 17 'altText' => '確認ダイアログ', 18 'template' => [ 19 'type' => 'confirm', 20 'text' => '元気ですかー?', 21 'actions' => [ 22 [ 23 'type' => 'message', 24 'label' => '元気です', 25 'text' => '元気です' 26 ], 27 [ 28 'type' => 'message', 29 'label' => 'まあまあです', 30 'text' => 'まあまあです' 31 ], 32 ] 33 ] 34 ]; 35} elseif ($message->{"text"} == 'ボタン') { 36 // ボタンタイプ 37 $messageData = [ 38 'type' => 'template', 39 'altText' => 'ボタン', 40 'template' => [ 41 'type' => 'buttons', 42 'title' => 'タイトルです', 43 'text' => '選択してね', 44 'actions' => [ 45 [ 46 'type' => 'postback', 47 'label' => 'webhookにpost送信', 48 'data' => 'value' 49 ], 50 [ 51 'type' => 'uri', 52 'label' => 'googleへ移動', 53 'uri' => 'https://google.com' 54 ] 55 ] 56 ] 57 ]; 58} elseif ($message->{"text"} == 'カルーセル') { 59 // カルーセルタイプ 60 $messageData = [ 61 'type' => 'template', 62 'altText' => 'カルーセル', 63 'template' => [ 64 'type' => 'carousel', 65 'columns' => [ 66 [ 67 'title' => 'カルーセル1', 68 'text' => 'カルーセル1です', 69 'actions' => [ 70 [ 71 'type' => 'postback', 72 'label' => 'webhookにpost送信', 73 'data' => 'value' 74 ], 75 [ 76 'type' => 'uri', 77 'label' => '美容の口コミ広場を見る', 78 'uri' => 'http://clinic.e-kuchikomi.info/' 79 ] 80 ] 81 ], 82 [ 83 'title' => 'カルーセル2', 84 'text' => 'カルーセル2です', 85 'actions' => [ 86 [ 87 'type' => 'postback', 88 'label' => 'webhookにpost送信', 89 'data' => 'value' 90 ], 91 [ 92 'type' => 'uri', 93 'label' => '女美会を見る', 94 'uri' => 'https://jobikai.com/' 95 ] 96 ] 97 ], 98 ] 99 ] 100 ]; 101} else { 102 // それ以外は送られてきたテキストをオウム返し 103 $messageData = [ 104 'type' => 'text', 105 'text' => $message->{"text"} 106 ]; 107} 108 109$response = [ 110 'replyToken' => $replyToken, 111 'messages' => [$messageData] 112]; 113error_log(json_encode($response)); 114 115$ch = curl_init('https://api.line.me/v2/bot/message/reply'); 116curl_setopt($ch, CURLOPT_POST, true); 117curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST'); 118curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 119curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($response)); 120curl_setopt($ch, CURLOPT_HTTPHEADER, array( 121 'Content-Type: application/json; charser=UTF-8', 122 'Authorization: Bearer ' . $accessToken 123)); 124$result = curl_exec($ch); 125error_log($result); 126curl_close($ch);
回答1件
あなたの回答
tips
プレビュー