Skip to content

Commit eae8b91

Browse files
committed
Add Rich
1 parent 6838cf7 commit eae8b91

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed

api.php

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,196 @@ public function downloadMessageObject($msgid, $path = "./") {
466466
}
467467
}
468468

469+
public function getRichMenuList() {
470+
$header = array(
471+
'Authorization: Bearer ' . $this->channelAccessToken,
472+
);
473+
474+
$context = stream_context_create(array(
475+
"http" => array(
476+
"method" => "GET",
477+
"header" => implode("\r\n", $header),
478+
),
479+
));
480+
481+
$response = file_get_contents($this->host.'/v2/bot/richmenu/list', false, $context);
482+
if (strpos($http_response_header[0], '200') === false) {
483+
http_response_code(500);
484+
error_log("Request failed: " . $response);
485+
}else {
486+
return json_decode($response);
487+
}
488+
}
489+
490+
public function getRichMenu($richMenuId) {
491+
$header = array(
492+
'Authorization: Bearer ' . $this->channelAccessToken,
493+
);
494+
495+
$context = stream_context_create(array(
496+
"http" => array(
497+
"method" => "GET",
498+
"header" => implode("\r\n", $header),
499+
),
500+
));
501+
502+
$response = file_get_contents($this->host.'/v2/bot/richmenu/'.urlencode($richMenuId), false, $context);
503+
if (strpos($http_response_header[0], '200') === false) {
504+
http_response_code(500);
505+
error_log("Request failed: " . $response);
506+
}else {
507+
return json_decode($response);
508+
}
509+
}
510+
511+
public function createRichMenu($content) {
512+
$header = array(
513+
"Content-Type: application/json",
514+
'Authorization: Bearer ' . $this->channelAccessToken,
515+
);
516+
517+
$context = stream_context_create(array(
518+
"http" => array(
519+
"method" => "POST",
520+
"header" => implode("\r\n", $header),
521+
"content" => json_encode($content),
522+
),
523+
));
524+
525+
$response = file_get_contents($this->host.'/v2/bot/richmenu', false, $context);
526+
if (strpos($http_response_header[0], '200') === false) {
527+
http_response_code(500);
528+
error_log("Request failed: " . $response);
529+
}else {
530+
return json_decode($response);
531+
}
532+
}
533+
534+
public function deleteRichMenu($richMenuId) {
535+
$header = array(
536+
'Authorization: Bearer ' . $this->channelAccessToken,
537+
);
538+
539+
$context = stream_context_create(array(
540+
"http" => array(
541+
"method" => "DELETE",
542+
"header" => implode("\r\n", $header),
543+
),
544+
));
545+
546+
$response = file_get_contents($this->host.'/v2/bot/richmenu/'.urlencode($richMenuId), false, $context);
547+
if (strpos($http_response_header[0], '200') === false) {
548+
http_response_code(500);
549+
error_log("Request failed: " . $response);
550+
}
551+
}
552+
553+
public function getRichMenuIdOfUser($userId) {
554+
$header = array(
555+
'Authorization: Bearer ' . $this->channelAccessToken,
556+
);
557+
558+
$context = stream_context_create(array(
559+
"http" => array(
560+
"method" => "GET",
561+
"header" => implode("\r\n", $header),
562+
),
563+
));
564+
565+
$response = file_get_contents($this->host.'/v2/bot/user/'.urlencode($userId).'/richmenu', false, $context);
566+
if (strpos($http_response_header[0], '200') === false) {
567+
http_response_code(500);
568+
error_log("Request failed: " . $response);
569+
}else {
570+
return json_decode($response);
571+
}
572+
}
573+
574+
public function linkRichMenuToUser($userId, $richMenuId) {
575+
$header = array(
576+
"Content-Type: application/json",
577+
'Authorization: Bearer ' . $this->channelAccessToken,
578+
);
579+
580+
$context = stream_context_create(array(
581+
"http" => array(
582+
"method" => "POST",
583+
"header" => implode("\r\n", $header),
584+
"content" => "[]",
585+
),
586+
));
587+
588+
$response = file_get_contents($this->host.'/v2/bot/user/'.urlencode($userId).'/richmenu/'.urlencode($richMenuId), false, $context);
589+
if (strpos($http_response_header[0], '200') === false) {
590+
http_response_code(500);
591+
error_log("Request failed: " . $response);
592+
}
593+
}
594+
595+
public function unlinkRichMenuFromUser($userId, $richMenuId) {
596+
$header = array(
597+
'Authorization: Bearer ' . $this->channelAccessToken,
598+
);
599+
600+
$context = stream_context_create(array(
601+
"http" => array(
602+
"method" => "DELETE",
603+
"header" => implode("\r\n", $header),
604+
),
605+
));
606+
607+
$response = file_get_contents($this->host.'/v2/bot/user/'.urlencode($userId).'/richmenu/'.urlencode($richMenuId), false, $context);
608+
if (strpos($http_response_header[0], '200') === false) {
609+
http_response_code(500);
610+
error_log("Request failed: " . $response);
611+
}
612+
}
613+
614+
//public function uploadRichMenuImage($path)
615+
# I think it is not a good way to upload file with "file_get_contents"
616+
/* #Uncommit This to use the function for upload File with cURL
617+
public function uploadRichMenuImage($path) {
618+
$ch = curl_init($this->host.'/v2/bot/richmenu/'.$richMenuId.'/content');
619+
curl_setopt($ch, CURLOPT_POST, true);
620+
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
621+
'file_input' => $path,
622+
));
623+
curl_exec($ch);
624+
}
625+
*/
626+
627+
public function getRichMenuImage($richMenuId) {
628+
$header = array(
629+
'Authorization: Bearer ' . $this->channelAccessToken,
630+
);
631+
632+
$context = stream_context_create(array(
633+
"http" => array(
634+
"method" => "GET",
635+
"header" => implode("\r\n", $header),
636+
),
637+
));
638+
639+
$response = file_get_contents($this->host.'/v2/bot/richmenu/' .urlencode($richMenuId).'/content', false, $context);
640+
if (strpos($http_response_header[0], '200') === false) {
641+
http_response_code(500);
642+
error_log("Request failed: " . $response);
643+
}else {
644+
return $response;
645+
}
646+
}
647+
648+
public function downloadRichMenuImage($richMenuId, $path = "./") {
649+
$response = $this->getRichMenuImage($richMenuId);
650+
if ($response != null) {
651+
$file = fopen($path . $richMenuId, "wb");
652+
fwrite($file, $response);
653+
fclose($file);
654+
}else{
655+
return false;
656+
}
657+
}
658+
469659
private function sign($body) {
470660
$hash = hash_hmac('sha256', $body, $this->channelSecret, true);
471661
$signature = base64_encode($hash);

0 commit comments

Comments
 (0)