在线客服
在线客服是一种以网络为媒介,向互联网客户与内部员工提供及时沟通的通信技术。
在线客服可以增加销售机会,降低运营成本,巩固客户关系。
微信公众号高级接口中提供了在线客服接口,本章介绍如何利用这一接口开发基于微信的在线客服。
index.php整体代码如下:
<?php
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET['echostr'])) {
$wechatObj->responseMsg();
}else{
$wechatObj->valid();
}
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
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;
}
}
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);
switch ($RX_TYPE)
{
case "text":
$result = $this->receiveText($postObj);
break;
case "event":
$result = $this->receiveEvent($postObj);
break;
default:
$result = "";
break;
}
echo $result;
}else {
echo "";
exit;
}
}
private function receiveText($object)
{
$keyword = trim($object->Content);
$mmc = memcache_init();
$service = memcache_get($mmc, "service");
//当前有人工客服对话
if ($service){
$relation = json_decode($service, true);
//用户处在人工客服交互中
if (in_array($object->FromUserName, $relation)){
require_once('weixin.class.php');
$to = ($relation['from'] == strval($object->FromUserName))?$relation['to']:$relation['from'];
$weixin = new class_weixin();
$weixin->send_custom_msg($to, "text", $keyword);
return;
}
}
$content = "22";
if (is_array($content)){
$result = $this->transmitNews($object, $content);
}else{
$result = $this->transmitText($object, $content);
}
return $result;
}
private function receiveEvent($object)
{
$content = "";
switch ($object->Event)
{
case "subscribe":
$content = "欢迎关注 德强1012";
case "unsubscribe":
break;
case "CLICK":
switch ($object->EventKey)
{
case "在线客服":
$staffs = array("oXpyI03cX4bpCDW5aaqBKWYrRm_o");
//ooD-ow-C8ZigAihscJN54pAWpu2g
//oXpyI03cX4bpCDW5aaqBKWYrRm_o
$mmc=memcache_init();
require_once('weixin.class.php');
$weixin = new class_weixin();
//客服点击,结束会话
if (in_array($object->FromUserName, $staffs)) {
$relation = json_decode(memcache_get($mmc, "service"), true);
$from = strval($object->FromUserName);
$to = $relation['from'];
$weixin->send_custom_msg($from, "text", "你已结束本次对话!");
$weixin->send_custom_msg($to, "text", "感谢您的咨询。期待下次再会!");
memcache_delete($mmc,"service");
}
//用户点击,开始会话
else {
$from = strval($object->FromUserName);
$to = $staffs[0];
memcache_set($mmc, "service", '{"from":"'.$from.'","to":"'.$to.'"}');
$weixin->send_custom_msg($from, "text", "正在为您接入客服,请稍候...");
$weixin->send_custom_msg($to, "text", "有用户请求接入,请响应!");
}
return;
default:
$content = "菜单默认回复";
break;
}
break;
default:
break;
}
if (is_array($content)){
$result = $this->transmitNews($object, $content);
}else{
$result = $this->transmitText($object, $content);
}
return $result;
}
private function transmitText($object, $content, $funcFlag = 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>";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $funcFlag);
return $result;
}
private function transmitNews($object, $arr_item, $funcFlag = 0)
{
//首条标题28字,其他标题39字
if(!is_array($arr_item))
return;
$itemTpl = " <item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
";
$item_str = "";
foreach ($arr_item as $item)
$item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
$newsTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[]]></Content>
<ArticleCount>%s</ArticleCount>
<Articles>
$item_str</Articles>
<FuncFlag>%s</FuncFlag>
</xml>";
$result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item), $funcFlag);
return $result;
}
}
?>
weixin.class.php 构造函数代码如下:
<?php
// $weixin = new class_weixin();
// $openid = "oQW8FuFKL0530Qhp3rh0HciHkTL0";
// $weixin->send_custom_msg($openid, "text", "ttttt");
class class_weixin
{
var $appid = "";
var $appsecret = "";
//构造函数
public function __construct($appid = NULL, $appsecret = NULL)
{
if($appid){
$this->appid = $appid;
}
if($appsecret){
$this->appsecret = $appsecret;
}
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
$res = $this->https_request($url);
$result = json_decode($res, true);
$this->access_token = $result["access_token"];
}
//发送客服消息,文本形式
public function send_custom_msg($touser, $type, $data)
{
$msg = array('touser' =>$touser);
switch($type)
{
case 'text':
$msg['msgtype'] = 'text';
$msg['text'] = array('content'=> urlencode($data));
break;
}
$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$this->access_token;
return $this->https_request($url, urldecode(json_encode($msg)));
}
protected function https_request($url, $data = null)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}
}