微信公眾號之自動回復


微信公眾號,智能回復!
1.首先,你需要一個域名!
2.然后需要進行配置!


若想配通,必須要TOKEN驗證通過!

<?php
/**
 * Created by PhpStorm.
 * User: jiqing
 * Date: 18-7-19
 * Time: 下午6:30
 * 處理消息自動回復
 */

class AutoAction extends CommonAction
{
    public function index()
    {
        $timestamp = $_GET['timestamp'];//timestamp其實就是一個時間戳
        $nonce = $_GET['nonce'];//nonce是一個隨機參數
        $token = "vTA2zJ3969BKJGSlJKYJhy22j96GKKlA";//這個token填寫你在微信公眾平台上寫的那個值
        $signature = $_GET['signature'];//這個signature其實就是在微信公眾平台已經加密好的字符串
        $echostr = $_GET['echostr'];
        $array = array($timestamp, $nonce, $token);
        sort($array);
        $tmpstr = implode('', $array);
        $tmpstr = sha1($tmpstr);
        if ($tmpstr == $signature && $echostr) {
            echo $echostr;
            exit;
        } else {
            $this->responseMsg();
        }
    }

    public function responseMsg()
    {
        $postArr = $GLOBALS['HTTP_RAW_POST_DATA'];
        $postObj = simplexml_load_string($postArr);
        if (strtolower($postObj->MsgType) == 'event') {
            //如果是關注事件(subscribe)
            if (strtolower($postObj->Event == 'subscribe')) {
                //回復用戶消息
                $toUser = $postObj->FromUserName;
                $fromUser = $postObj->ToUserName;
                $time = time();
                $msgType = 'text';
                $content = '歡迎關注 宿遷市面包新語 微信公眾號^_^';
                $template = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[%s]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							</xml>";
                $info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);
                echo $info;
            }
        }
        //回復純文本或單圖文消息
        if (($postObj->MsgType) == 'text' && trim($postObj->Content) == '閨蜜') {
            $toUser = $postObj->FromUserName;
            $fromUser = $postObj->ToUserName;

            $arr = array(
                array(
                    'title' => '閨蜜挑戰',
                    'description' => "參與真假塑料閨蜜活動,免費領取面包",
                    'picUrl' => 'http://xinyu.squmo.com/static/images/lulu.png',
                    'url' => 'http://xinyu.squmo.com/wx.php/Index/index',
                ),
            );
            $template = "<xml>
						<ToUserName><![CDATA[%s]]></ToUserName>
						<FromUserName><![CDATA[%s]]></FromUserName>
						<CreateTime>%s</CreateTime>
						<MsgType><![CDATA[%s]]></MsgType>
						<ArticleCount>" . count($arr) . "</ArticleCount>
						<Articles>";
            foreach ($arr as $k => $v) {
                $template .= "<item>
							<Title><![CDATA[" . $v['title'] . "]]></Title> 
							<Description><![CDATA[" . $v['description'] . "]]></Description>
							<PicUrl><![CDATA[" . $v['picUrl'] . "]]></PicUrl>
							<Url><![CDATA[" . $v['url'] . "]]></Url>
							</item>";
            }
            $template .= "</Articles>
						</xml> ";
            echo sprintf($template, $toUser, $fromUser, time(), 'news');
        } else {
            $fromUser = $postObj->ToUserName;//消息從哪里來
            $toUser = $postObj->FromUserName;//發送給誰

            switch (trim($postObj->Content)) {
                case '兌換碼':
                    $question_cdkey_model = M('question_cdkey');
                    $openid = $toUser->__toString(); // 關鍵將 simplexmlelement object to string
                    $question_info = $question_cdkey_model->where(['openid'=>$openid])->find();
                    if ($question_info) {
                        $content = $question_info['cdkey'];
                    } else {
                        $content = "抱歉,您還沒有參加活動,回復【閨蜜】參與活動";
                    }

                    break;
                default:
                    $content = "歡迎品嘗面包新語 回復【閨蜜】參與活動";
            }
            $template1 = "<xml>
							<ToUserName><![CDATA[%s]]></ToUserName>
							<FromUserName><![CDATA[%s]]></FromUserName>
							<CreateTime>%s</CreateTime>
							<MsgType><![CDATA[%s]]></MsgType>
							<Content><![CDATA[%s]]></Content>
							</xml>";
            $time = time();
            $msgType = 'text';
            echo sprintf($template1, $toUser, $fromUser, $time, $msgType, $content);
        }
    }
}

很有意思!

他會根據事件,文本做出不同的處理!



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM