2017年3月3日 星期五

為微信機械人加入選單


客人的微信公眾號本來有一個選單,但當啟用了微信機械人後,選單便不見了。主要原因是微信只支援其中一種模式,兩者不能兼備。

由於微信機械人有它的任務,於是我只好為機械人加入選單。做法很簡單,只需要按格式準備好 JSON 內容,發到 https://api.weixin.qq.com/cgi-bin/menu/create 就能加入選單。這樣,用戶點選項目後,當中的 Key(如:M01_A)便會傳到微信機械人作進一步處理。以下是設定選單的程序:
<?php
//----------------------------------------------------------------------------------------
//  WeChat Bot Menu Creator
//----------------------------------------------------------------------------------------
//  Platform: Linux + Apache + PHP
//  Written by Pacess HO
//  Copyright 2017 Pacess Studio.  All rights reserved.
//----------------------------------------------------------------------------------------

session_start();

header("Access-Control-Allow-Origin: https://home.pacess.com");
header("Access-Control-Allow-Methods: POST");

header("Content-type: text/plain");
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Tue, 10 Mar 1987 00:00:00 GMT");

date_default_timezone_set("Asia/Hong_Kong");
mb_internal_encoding("UTF-8");
ini_set("memory_limit", "-1");
set_time_limit(0);

//----------------------------------------------------------------------------------------
//  * Order is important
require("./libraries/constants.php");
require("./libraries/common.php");

require("./libraries/wechatAPI.php");

//----------------------------------------------------------------------------------------
//  Global variables
$_apiName = baseName(__FILE__);
$_remark = $_SERVER["REMOTE_ADDR"]."/".$_SERVER["HTTP_USER_AGENT"];

//========================================================================================
//  Main Program
$_interfaceType = php_sapi_name();
$_logPath = "./__logs__/".date("Ymd").".txt";

//  apache2handler = Browser
//  cli = Command Line (Terminal)
if ($_interfaceType == "cli")  {
   echo("Please run this program with browser...");
   exit(0);
}

//----------------------------------------------------------------------------------------
$wechatAPI = new wechatAPI();

$resultDictionary = $wechatAPI->getAccessToken($_appID, $_appSecret);
if ($resultDictionary == null)  {
   echo("### Error 1");
   exit(-1);
}

$accessToken = $resultDictionary["accessToken"];
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token=".$accessToken;

//  Don't put text other than English into JSON!
$menuArray = array(
   "button"=>array(
      array(
         "type"=>"click",
         "name"=>"##_TEXT01_##",
         "sub_button"=>array(
            array(
               "type"=>"click",
               "key"=>"M01_A",
               "name"=>"##_TEXT01A_##"
            ),
            array(
               "type"=>"click",
               "key"=>"M01_B",
               "name"=>"##_TEXT01B_##"
            )
         )
      ),
      array(
         "type"=>"click",
         "name"=>"##_TEXT02_##",
         "sub_button"=>array(
            array(
               "type"=>"click",
               "key"=>"M02_A",
               "name"=>"##_TEXT02A_##"
            ),
            array(
               "type"=>"click",
               "key"=>"M02_B",
               "name"=>"##_TEXT02B_##"
            )
         )
      ),
      array(
         "type"=>"view",
         "key"=>"M03",
         "name"=>"##_TEXT03_##",
         "url"=>"http://www.pacess.com/"
      )
   )
);

$searchArray = array("##_TEXT01_##", "##_TEXT01A_##", "##_TEXT01B_##", "##_TEXT02_##", "##_TEXT02A_##", "##_TEXT02B_##", "##_TEXT03_##");
$replaceArray = array("最新资讯", "新品速递", "视频欣赏", "关于品牌", "风格穿搭", "品牌历史", "店铺位置");
$data = str_replace($searchArray, $replaceArray, json_encode($menuArray));
echo("data: $data\n\n");

$curl = curl_init($url) ;
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Content-Type: application/json"));
$result = curl_exec($curl) ;
curl_close($curl) ;

?>

沒有留言: