Micro message public platform (three) subscription event (subscribe) treatment

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

One, introduction

New user attention micro message public platform, will produce a subscription event, subscribe event , the default is not code of this event was the corresponding reply processing.

In a public platform for the new user attention, may want to know this platform provides what function, and how to use the platform, the popular point stresses that the platform “ manual”.

This paper will describe in detail the process of subscribe events, reply to the corresponding information for improving the interaction.

Two, analysis

Micro message currently offers five message types, respectively.:

Upon receiving a message, first of all need to judge the message type, and then according to the different types of information to deal with. In the event of a push, the event type is divided into three kinds, subscribe (subscription), unsubscribe (cancel), CLICK (custom menu click event), also need to add a judge; judge for the subscribe event after the welcome message, according to preset, reply to the users.

Three, determine the message type

$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);

switch($RX_TYPE)
{
    case "text":
        $resultStr = $this->handleText($postObj);
        break;
    case "event":
        $resultStr = $this->handleEvent($postObj);
        break;
    default:
        $resultStr = "Unknow msg type: ".$RX_TYPE;
        break;
}

Explain:

$RX_TYPE = trim($postObj->MsgType);   Get the message type,

case "text":
  $resultStr = $this->handleText($postObj);   Using handleText () function to handle text message,

case "event":
  $resultStr = $this->handleEvent($postObj);   Using handleEvent () function to handle events push,

Four, determine the type of event

switch ($object->Event)
{
    case "subscribe":
        $contentStr = "Thank you for your concern about the [Suzhou] Zhuo Jin "." \n "." the micro signal: zhuojinsz "." \n "excellence." beautiful, City of Suzhou, We provide the Suzhou local life guide for you, Suzhou information query, Do the best Suzhou micro message platform. "." \n "." the current platform functions as follows: "." \n "." [1] check weather, If the input: the weather of Suzhou "." \n "." [2] check bus, If the input: Suzhou bus 178 "." \n "." [3] translation, If the input: love you translation I "." \n "." [4] Suzhou information query, If the input: "Suzhou Guanqian Street." \n "more.", Please look...";
        break;
    default :
        $contentStr = "Unknow Event: ".$object->Event;
        break;
}

Explain:

If the subscribe event, set the reply content is “ thank you for your concern about the [Zhuo Jin Suzhou]...”,

Five, the complete code

<?php
/**
  * wechat php test
  */

//define your token
define("TOKEN", "zhuojin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->responseMsg();
//$wechatObj->valid();

class wechatCallbackapiTest
{
    /*public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }*/

    public function responseMsg()
    {
        //get post data, May be due to the different environments
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

        //extract post data
        if (!empty($postStr)){
                
                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $RX_TYPE = trim($postObj->MsgType);

                switch($RX_TYPE)
                {
                    case "text":
                        $resultStr = $this->handleText($postObj);
                        break;
                    case "event":
                        $resultStr = $this->handleEvent($postObj);
                        break;
                    default:
                        $resultStr = "Unknow msg type: ".$RX_TYPE;
                        break;
                }
                echo $resultStr;
        }else {
            echo "";
            exit;
        }
    }

    public function handleText($postObj)
    {
        $fromUsername = $postObj->FromUserName;
        $toUsername = $postObj->ToUserName;
        $keyword = trim($postObj->Content);
        $time = time();
        $textTpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[%s]]></MsgType>
                    <Content><![CDATA[%s]]></Content>
                    <FuncFlag>0</FuncFlag>
                    </xml>";             
        if(!empty( $keyword ))
        {
            $msgType = "text";
            $contentStr = "Welcome to wechat world!";
            $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
            echo $resultStr;
        }else{
            echo "Input something...";
        }
    }

    public function handleEvent($object)
    {
        $contentStr = "";
        switch ($object->Event)
        {
            case "subscribe":
                $contentStr = "Thank you for your concern about the [Suzhou] Zhuo Jin "." \n "." the micro signal: zhuojinsz "." \n "excellence." beautiful, City of Suzhou, We provide the Suzhou local life guide for you, Suzhou information query, Do the best Suzhou micro message platform. "." \n "." the current platform functions as follows: "." \n "." [1] check weather, If the input: the weather of Suzhou "." \n "." [2] check bus, If the input: Suzhou bus 178 "." \n "." [3] translation, If the input: love you translation I "." \n "." [4] Suzhou information query, If the input: "Suzhou Guanqian Street." \n "more.", Please look...";
                break;
            default :
                $contentStr = "Unknow Event: ".$object->Event;
                break;
        }
        $resultStr = $this->responseText($object, $contentStr);
        return $resultStr;
    }
    
    public function responseText($object, $content, $flag=0)
    {
        $textTpl = "<xml>
                    <ToUserName><![CDATA[%s]]></ToUserName>
                    <FromUserName><![CDATA[%s]]></FromUserName>
                    <CreateTime>%s</CreateTime>
                    <MsgType><![CDATA[text]]></MsgType>
                    <Content><![CDATA[%s]]></Content>
                    <FuncFlag>%d</FuncFlag>
                    </xml>";
        $resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
        return $resultStr;
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];    
                
        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );
        
        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
}

?>

Six, pay attention to

Please pay attention to   Zhuo Jin Suzhou   micro message public account, Zhuo Jin Suzhou   based on the SAE platform, the main micro message function development test.

You can look at     in Suzhou; Zhuo Jin; public account function test, and the acquisition of new application development.

1 micro message client login, add friends friends -> -> search number -> zhuojinsz, search and attention.

The 2 scan two-dimensional code:

Zhuo Jin Suzhou   function list.


David Camp

    Micro message development group I share: 168236814

    Micro message development share group II: 213260412

We always believe, sharing is a virtue| We Believe, Great People Share Knowledge...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by James at October 23, 2013 - 7:54 PM