国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

微信公眾平臺(tái)開(kāi)發(fā)(十二) 發(fā)送客服消息

2019-11-15 01:34:49
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
微信公眾平臺(tái)開(kāi)發(fā)(十二) 發(fā)送客服消息

一、簡(jiǎn)介

當(dāng)用戶主動(dòng)發(fā)消息給公眾號(hào)的時(shí)候(包括發(fā)送信息、點(diǎn)擊自定義菜單、訂閱事件、掃描二維碼事件、支付成功事件、用戶維權(quán)),微信將會(huì)把消息數(shù)據(jù)推送給開(kāi)發(fā)者,開(kāi)發(fā)者在一段時(shí)間內(nèi)(目前修改為48小時(shí))可以調(diào)用客服消息接口,通過(guò)POST一個(gè)JSON數(shù)據(jù)包來(lái)發(fā)送消息給普通用戶,在48小時(shí)內(nèi)不限制發(fā)送次數(shù)。此接口主要用于客服等有人工消息處理環(huán)節(jié)的功能,方便開(kāi)發(fā)者為用戶提供更加優(yōu)質(zhì)的服務(wù)。

二、思路分析

官方文檔中只提供了一個(gè)發(fā)送客服消息的接口,開(kāi)發(fā)者只要POST一個(gè)特定的JSON數(shù)據(jù)包即可實(shí)現(xiàn)消息回復(fù)。在這里,我們打算做成一個(gè)簡(jiǎn)單的平臺(tái),可以記錄用戶消息,并且用網(wǎng)頁(yè)表格的形式顯示出來(lái),然后可以對(duì)消息進(jìn)行回復(fù)操作。

首先,我們使用數(shù)據(jù)庫(kù)記錄用戶主動(dòng)發(fā)送過(guò)來(lái)的消息,然后再提取出來(lái)展示到頁(yè)面,針對(duì)該消息,進(jìn)行回復(fù)。這里我們只討論文本消息,關(guān)于其他類型的消息,大家自行研究。

三、記錄用戶消息

3.1 創(chuàng)建數(shù)據(jù)表

創(chuàng)建一張數(shù)據(jù)表tbl_customer 來(lái)記錄用戶消息。

---- 表的結(jié)構(gòu) `tbl_customer`--CREATE TABLE `tbl_customer` (  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '//消息ID',  `from_user` char(50) NOT NULL COMMENT '//消息發(fā)送者',  `message` varchar(200) NOT NULL COMMENT '//消息體',  `time_stamp` datetime NOT NULL COMMENT '//消息發(fā)送時(shí)間',  PRIMARY KEY (`id`),  KEY `from_user` (`from_user`)) ENGINE=MyISAM  DEFAULT CHARSET=utf8 ;

3.2 創(chuàng)建sql.func.php 文件

創(chuàng)建 _query($_sql) {} 函數(shù),來(lái)執(zhí)行INSERT 操作。

function _query($_sql){    if(!$_result = MySQL_query($_sql)){        exit('SQL執(zhí)行失敗'.mysql_error());    }    return $_result;}

3.3 創(chuàng)建記錄消息的函數(shù)文件record_message.func.inc.php

//引入數(shù)據(jù)庫(kù)處理函數(shù)require_once 'sql.func.php';function _record_message($fromusername,$keyWord,$date_stamp){    //調(diào)用_query()函數(shù)    _query("INSERT INTO tbl_customer(from_user,message,time_stamp) VALUES('$fromusername','$keyword','$date_stamp')");}

3.4 處理并記錄文本消息

A. 引入回復(fù)文本的函數(shù)文件,引入記錄消息的函數(shù)文件

//引入回復(fù)文本的函數(shù)文件require_once 'responseText.func.inc.php';//引入記錄消息的函數(shù)文件require_once 'record_message.func.inc.php';

B. 記錄消息入數(shù)據(jù)庫(kù),并返回給用戶剛才發(fā)送的消息,在這里,你可以修改成其他的文本,比如:“你好,消息已收到,我們會(huì)盡快回復(fù)您!” 等等。

    //處理文本消息函數(shù)    public function handleText($postObj)    {        //獲取消息發(fā)送者,消息體,時(shí)間戳        $fromusername = $postObj->FromUserName;        $keyword = trim($postObj->Content);        $date_stamp = date('Y-m-d H:i:s');        if(!empty( $keyword ))        {            //調(diào)用_record_message()函數(shù),記錄消息入數(shù)據(jù)庫(kù)            _record_message($fromusername,$keyword,$date_stamp);                        $contentStr = $keyword;            //調(diào)用_response_text()函數(shù),回復(fù)發(fā)送者消息            $resultStr = _response_text($postObj,$contentStr);            echo $resultStr;        }else{            echo "Input something...";        }    }

四、網(wǎng)頁(yè)展示用戶消息

我們的最終效果大概如下所示,主要的工作在“信息管理中心”這塊,其他的頁(yè)面布局等等,不在這里贅述了,只講解消息展示這塊。

4.1 具體實(shí)施

引入數(shù)據(jù)庫(kù)操作文件,執(zhí)行分頁(yè)模塊,執(zhí)行數(shù)據(jù)庫(kù)查詢,將查詢出來(lái)的結(jié)果賦給$_result 供下面使用。

//引入數(shù)據(jù)庫(kù)操作文件require_once 'includes/sql.func.php';//分頁(yè)模塊global $_pagesize,$_pagenum;_page("SELECT id FROM tbl_customer",15);        //第一個(gè)參數(shù)獲取總條數(shù),第二個(gè)參數(shù),指定每頁(yè)多少條$_result = _query("SELECT * FROM tbl_customer ORDER BY id DESC LIMIT $_pagenum,$_pagesize");

將$_result 遍歷出來(lái),依次插入表格中。

<form>    <table cellspacing="1">        <tr><th>消息ID</th><th>發(fā)送者</th><th>消息體</th><th>消息時(shí)間</th><th>操作</th></tr>        <?php             while(!!$_rows = _fetch_array_list($_result)){                $_html = array();                $_html['id'] = $_rows['id'];                $_html['from_user'] = $_rows['from_user'];                $_html['message'] = $_rows['message'];                $_html['time_stamp'] = $_rows['time_stamp'];        ?>        <tr><td><?php echo $_html['id']?></td><td><?php echo $_html['from_user']?></td><td><?php echo $_html['message']?></td><td><?php echo $_html['time_stamp']?></td><td><a href="reply.php?fromusername=<?php echo $_html['from_user']?>&message=<?php echo $_html['message']?>"><input type="button" value="回復(fù)" /></a></td></tr>        <?php             }            _free_result($_result);        ?>    </table></form>

說(shuō)明:在每條消息后,都有一個(gè)“回復(fù)”操作,點(diǎn)擊該按鈕,向reply.php文件中傳入fromusername和用戶發(fā)送的消息,為回復(fù)用戶消息做準(zhǔn)備。

五、消息回復(fù)

5.1 創(chuàng)建客服消息回復(fù)函數(shù)文件customer.php

微信發(fā)送客服消息的接口URL如下:

https://api.weixin.QQ.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN

需要POST的JSON數(shù)據(jù)包格式如下:

{    "touser":"OPENID",    "msgtype":"text",    "text":    {         "content":"Hello World"    }}

所以,根據(jù)上面的提示,我們編寫(xiě)處理函數(shù) _reply_customer($touser,$content),調(diào)用的時(shí)候,傳入touser和需要回復(fù)的content,即可發(fā)送客服消息。

function _reply_customer($touser,$content){        //更換成自己的APPID和APPSECRET    $APPID="wxef78f22f877db4c2";    $APPSECRET="3f3aa6ea961b6284057b8170d50e2048";        $TOKEN_URL="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$APPID."&secret=".$APPSECRET;        $json=file_get_contents($TOKEN_URL);    $result=json_decode($json);        $ACC_TOKEN=$result->access_token;        $data = '{        "touser":"'.$touser.'",        "msgtype":"text",        "text":        {             "content":"'.$content.'"        }    }';        $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$ACC_TOKEN;        $result = https_post($url,$data);    $final = json_decode($result);    return $final;}function https_post($url,$data){    $curl = curl_init();    curl_setopt($curl, CURLOPT_URL, $url);     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);    curl_setopt($curl, CURLOPT_POST, 1);    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);    $result = curl_exec($curl);    if (curl_errno($curl)) {       return 'Errno'.curl_error($curl);    }    curl_close($curl);    return $result;}

下面,我們就將上面寫(xiě)好的函數(shù)引入到消息回復(fù)頁(yè)面,實(shí)現(xiàn)發(fā)送客服消息的功能。

5.2 點(diǎn)擊“回復(fù)”按鈕,帶上fromusername和message參數(shù)跳轉(zhuǎn)到reply.php。

5.3 reply.php 頁(yè)面顯示

5.4 reply.php文件分析

//引入回復(fù)消息的函數(shù)文件require_once '../customer.php';

form表單提交到relpy.php本身,帶有action=relpy.

<form method="post" action="reply.php?action=reply">    <dl>        <dd><strong>收件人:</strong><input type="text" name="tousername" class="text" value="<?php echo $from_username?>" /></dd>        <dd><strong>原消息:</strong><input type="text" name="message" class="text" value="<?php echo $message?>" /></dd>        <dd><span><strong>內(nèi) 容:</strong></span><textarea rows="5" cols="34" name="content"></textarea></dd>        <dd><input type="submit" class="submit" value="回復(fù)消息" /></dd>    </dl></form>

action=reply 動(dòng)作處理。

if($_GET['action'] == "reply"){    $touser = $_POST['tousername'];    $content = $_POST['content'];    $result = _reply_customer($touser, $content);        if($result->errcode == "0"){        _location('消息回復(fù)成功!', 'index.php');    }}

說(shuō)明:POST方式獲取touser, content,然后調(diào)用_reply_customer($touser, $content)方法處理,處理成功,則彈出“消息回復(fù)成功!”,然后跳轉(zhuǎn)到index.php頁(yè)面,完成發(fā)送客服消息過(guò)程。

六、測(cè)試

6.1 微信用戶發(fā)送消息

6.2 平臺(tái)消息管理

6.3 發(fā)送客服消息

再次發(fā)送客服消息

發(fā)送客服消息測(cè)試成功!

七、代碼獲取

http://files.VEVb.com/mchina/customer.rar

八、總

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 农安县| 勃利县| 南陵县| 郁南县| 平罗县| 浏阳市| 焉耆| 天水市| 弋阳县| 织金县| 买车| 秭归县| 万盛区| 木兰县| 漳州市| 广德县| 浑源县| 瓮安县| 江安县| 柳林县| 茂名市| 麦盖提县| 东山县| 阳信县| 汶川县| 双流县| 大渡口区| 江达县| 东乡县| 宜昌市| 贡山| 翁牛特旗| 乐陵市| 德庆县| 广西| 泰和县| 富顺县| 简阳市| 昂仁县| 鹤山市| 永嘉县|