-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcallback.php
147 lines (130 loc) · 4.45 KB
/
callback.php
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
$resMessage="はげ";
$channel="0000000000";
$secret="f0000000000000000000000000000000";
$mid="u90377c66be898f239f5ae3aadac9240f";
$api_key = "AIzaSyDHXq-9WgT9wUHdwoFAf69D9xxaFhgOrxE";
$r = recev_line();
$checked_content = check_content($r["type"] , $r["id"] , $secret , $channel ,$mid);
post_line( $r["from"] , $checked_content , $secret , $channel ,$mid );
function recev_line(){
$r=json_decode(file_get_contents('php://input') , true);
try{
return array( "id"=>$r["result"][0]["content"]["id"] , "from"=>$r["result"][0]["content"]["from"] , "type"=>$r["result"][0]["content"]["contentType"] , "body"=>$r["result"][0]["content"]["text"]);
}catch (Exception $e) {
return false;
}
}
function check_content($type, $id, $secret, $channel ,$mid){
if ($type == "2"){ // 画像だったときの処理
$gazou = get_image($id, $secret, $channel ,$mid);
return $gazou;
}else{ // 画像以外
return "写真を送ってちょ。\r\n送ってもらった写真を識別するよ";
}
}
function get_image($id, $secret, $channel ,$mid){
$url = 'https://trialbot-api.line.me/v1/bot/message/'.$id.'/content';
$options = array(
'http' => array(
'method' => 'GET',
'ignore_errors' => true,
'header'=>
"Content-type: application/json; charset=UTF-8\r\n"
."X-Line-ChannelID: ".$channel."\r\n"
."X-Line-ChannelSecret: ".$secret."\r\n"
."X-Line-Trusted-User-With-ACL: ".$mid
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$img = base64_encode($result);
//$result = json_decode(file_get_contents('php://input') , true);
//$result_base64 = imagecreatefromstring($result);
//return $result;
$scheme = 'data:application/octet-stream;base64,';
$size = getimagesize($scheme . $img);
$img_label = imgrecgnize($result);
return $img_label;
}
function imgrecgnize($img){
// リファラー (許可するリファラーを設定した場合)
$referer = "https://office-eguchi.co.jp" ;
//$api_key = "AIzaSyDHXq-9WgT9wUHdwoFAf69D9xxaFhgOrxE";
// 画像へのパス
//$image_path = "./image.jpg" ;
// リクエスト用のJSONを作成
$json = json_encode( array(
"requests" => array(
array(
"image" => array(
"content" => base64_encode($img) ,
) ,
"features" => array(
array(
"type" => "LABEL_DETECTION" ,
"maxResults" => 5 ,
) ,
) ,
) ,
) ,
) ) ;
// リクエストを実行
$curl = curl_init() ;
curl_setopt( $curl, CURLOPT_URL, "https://vision.googleapis.com/v1/images:annotate?key=AIzaSyDHXq-9WgT9wUHdwoFAf69D9xxaFhgOrxE" );//. $api_key ) ;
curl_setopt( $curl, CURLOPT_HEADER, true ) ;
curl_setopt( $curl, CURLOPT_CUSTOMREQUEST, "POST" ) ;
curl_setopt( $curl, CURLOPT_HTTPHEADER, array( "Content-Type: application/json" ) ) ;
curl_setopt( $curl, CURLOPT_SSL_VERIFYPEER, false ) ;
curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ) ;
if( isset($referer) && !empty($referer) ) curl_setopt( $curl, CURLOPT_REFERER, $referer ) ;
curl_setopt( $curl, CURLOPT_TIMEOUT, 15 ) ;
curl_setopt( $curl, CURLOPT_POSTFIELDS, $json ) ;
$res1 = curl_exec( $curl ) ;
$res2 = curl_getinfo( $curl ) ;
curl_close( $curl ) ;
// 取得したデータ
$json = substr( $res1, $res2["header_size"] ) ;
$array = json_decode($json, true);
//$r["result"][0]["content"]["from"]
$labelAnnotations = $array["responses"][0]["labelAnnotations"];
$answer = "おそらく・・・\r\n";
for ($i = 0 ; $i < count($labelAnnotations); $i++ ){
$answer .= (string)$array["responses"][0]["labelAnnotations"][$i]["description"];
$answer .= "とか\r\n";
}
$answer .= "じゃないかな";
//return $answer;
return $answer;
//return $labelAnnotations[2]["description"];
}
function post_line( $to , $body , $secret , $channel ,$mid ){
$url = 'https://trialbot-api.line.me/v1/events/';
$data = array(
"to"=>array( $to ),
"toChannel"=>"1383378250",
"eventType"=>"138311608800106203",
"content"=>array(
"to"=>$to,
"toType"=>1,
"contentType"=>1,
"text"=>$body
)
);
$options = array(
'http' => array(
'method' => 'POST',
'content' => json_encode( $data ),
'ignore_errors' => true,
'header'=>
"Content-type: application/json; charset=UTF-8\r\n"
."X-Line-ChannelID: ".$channel."\r\n"
."X-Line-ChannelSecret: ".$secret."\r\n"
."X-Line-Trusted-User-With-ACL: ".$mid
)
);
$context = stream_context_create( $options );
$result = file_get_contents( $url, false, $context );
$result_json = json_decode($result,true);
return $result_json;
}