2017年2月8日 星期三

認證微信機械人伺服器


要開發 WeChat Bot,需要有微信公眾帳號。就像建立 Facebook Messenger Bot 一樣,在平台內建立應用程式,得到 App ID 及 App Secret;拿著這兩串數值換來兩小時期限的 Access Token;往後的 API 都是用 Access Token 來執行。

每個應用程式都需要一個伺服器用來接收由客戶發出的訊息;就像 Webhook 的方法差不多。所以,第一步得對伺服器做認證工作。根據上圖的要求去做認證。如果運算結果符合時,傳回 echostr 代表。以下是我的認證程式:
<?php
//----------------------------------------------------------------------------------------
//  WeChat Open Platform Test
//  API Token Checking Program
//----------------------------------------------------------------------------------------
//  Platform: Linux + Apache + PHP
//  Written by Pacess HO
//  Copyright 2017 Pacess Studio.  All rights reserved.
//----------------------------------------------------------------------------------------

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);

//----------------------------------------------------------------------------------------
//  Defines
define("TOKEN", "sita-chan");

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

//========================================================================================
//  Main Program
$_interfaceType = php_sapi_name();

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

//----------------------------------------------------------------------------------------
$wechatAPI = new wechatAPI();
$wechatAPI->valid();
echo("Sorry, validation failed...");

?>
index.php

<?php
//----------------------------------------------------------------------------------------
//  WeChat Open Platform Test
//  WeChat API Test Object
//----------------------------------------------------------------------------------------
//  Platform: Linux + Apache + PHP
//  Written by Pacess HO
//  Copyright 2017 Pacess Studio.  All rights reserved.
//----------------------------------------------------------------------------------------

//========================================================================================
//  Main Program
class wechatAPI  {
      
   //----------------------------------------------------------------------------------------
   private function checkSignature()  {
      if (!defined("TOKEN"))  {throw new Exception('TOKEN is not defined!');}
      
      $signature = $_GET["signature"];
      $timestamp = $_GET["timestamp"];
      $nonce = $_GET["nonce"];
      $token = TOKEN;

      $parameterArray = array($token, $timestamp, $nonce);

      //  Use SORT_STRING rule
      sort($parameterArray, SORT_STRING);
      $string = implode($parameterArray);
      $sha1 = sha1($string);
      
      return ($sha1 == $signature);
   }

   //----------------------------------------------------------------------------------------
   public function valid()  {
      $echoStr = $_GET["echostr"];
      if ($this->checkSignature())  {echo($echoStr);  exit;}
   }
}
?>
wechatAPI.php

認證成功後,便可以正式編寫 WeChat 機械人的程式。

沒有留言: