在这篇微信公众平台开发教程中,我们将介绍如何使用开发模式实现多客服系统。
本文分为以下三个部分:
回复多客服消息
触发多客服会话
其他说明
一、回复多客服消息在新的微信协议中,开发模式也可以接入客服系统。 开发者如果需要让用户使用客服系统,需要在接收到用户发送的消息时,返回一个msgtype为transfer_customer_service的消息,微信服务器在收到这条消息时,会把用户这次发送的和以后一段时间内发送的消息转发客服系统。
返回的消息举例如下
<xml>
<tousername><![cdata[touser]]></tousername>
<fromusername><![cdata[fromuser]]></fromusername>
<createtime>1399197672</createtime>
<msgtype><![cdata[transfer_customer_service]]></msgtype>
</xml>
该消息的实现如下
//回复多客服消息
private function transmitservice($object)
{
$xmltpl = <xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[transfer_customer_service]]></msgtype>
</xml>;
$result = sprintf($xmltpl, $object->fromusername, $object->tousername, time());
return $result;
}
二、触发多客服会话一般情况下,用户想要咨询问题是,经常会问“你好”,“在吗”,这样的问题。
我们以这些词为触发关键词,当用户发送的文本消息内容中包含这些词的时候,就返回多客服消息给用户(用户在微信端感觉不到任何内容,但微信公众账号会将用户本次及以后一段时间的消息都转发到客服)。
实现代码如下:
//接收文本消息
private function receivetext($object)
{
$keyword = trim($object->content);
if (strstr($keyword, 投诉) || strstr($keyword, 你好) || strstr($keyword, 在吗)){
$result = $this->transmitservice($object);
}
return $result;
}
三、完整代码<?php
/*
方倍工作室
copyright 2014 all rights reserved
*/
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, sort_string);
$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)){
$this->logger(r .$poststr);
$postobj = simplexml_load_string($poststr, 'simplexmlelement', libxml_nocdata);
$rx_type = trim($postobj->msgtype);
switch ($rx_type)
{
case event:
$result = $this->receiveevent($postobj);
break;
case text:
$result = $this->receivetext($postobj);
break;
}
$this->logger(t .$result);
echo $result;
}else {
echo ;
exit;
}
}
//接收事件消息
private function receiveevent($object)
{
switch ($object->event)
{
case subscribe:
$content[] = array(title =>欢迎关注方倍工作室, description =>使用方法:\n1.发送快递单号,例如6367532560,可查询快递详情, picurl =>http://www.3856.cc/weixin/weixin/logo.jpg, url =>);
break;
default:
$content = receive a new event: .$object->event;
break;
}
if(is_array($content)){
if (isset($content[0])){
$result = $this->transmitnews($object, $content);
}else if (isset($content['musicurl'])){
$result = $this->transmitmusic($object, $content);
}
}else{
$result = $this->transmittext($object, $content);
}
return $result;
}
//接收文本消息
private function receivetext($object)
{
$keyword = trim($object->content);
if($keyword == 时间 || $keyword == 测试){
$content = date(y-m-d h:i:s,time());
$result = $this->transmittext($object, $content);
}
//触发多客服模式
else if (strstr($keyword, 您好) || strstr($keyword, 你好) || strstr($keyword, 在吗) || strstr($keyword, 有人吗)){
$result = $this->transmitservice($object);
return $result;
}
return $result;
}
private function transmittext($object, $content)
{
$texttpl = <xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[text]]></msgtype>
<content><![cdata[%s]]></content>
</xml>;
$result = sprintf($texttpl, $object->fromusername, $object->tousername, time(), $content);
return $result;
}
private function transmitnews($object, $newsarray)
{
if(!is_array($newsarray)){
return;
}
$itemtpl = <item>
<title><![cdata[%s]]></title>
<description><![cdata[%s]]></description>
<picurl><![cdata[%s]]></picurl>
<url><![cdata[%s]]></url>
</item>
;
$item_str = ;
foreach ($newsarray 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>
</xml>;
$result = sprintf($newstpl, $object->fromusername, $object->tousername, time(), count($newsarray));
return $result;
}
private function transmitmusic($object, $musicarray)
{
$itemtpl = <music>
<title><![cdata[%s]]></title>
<description><![cdata[%s]]></description>
<musicurl><![cdata[%s]]></musicurl>
<hqmusicurl><![cdata[%s]]></hqmusicurl>
</music>;
$item_str = sprintf($itemtpl, $musicarray['title'], $musicarray['description'], $musicarray['musicurl'], $musicarray['hqmusicurl']);
$texttpl = <xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[music]]></msgtype>
$item_str
</xml>;
$result = sprintf($texttpl, $object->fromusername, $object->tousername, time());
return $result;
}
//回复多客服消息
private function transmitservice($object)
{
$xmltpl = <xml>
<tousername><![cdata[%s]]></tousername>
<fromusername><![cdata[%s]]></fromusername>
<createtime>%s</createtime>
<msgtype><![cdata[transfer_customer_service]]></msgtype>
</xml>;
$result = sprintf($xmltpl, $object->fromusername, $object->tousername, time());
return $result;
}
private function logger($log_content)
{
if(isset($_server['http_appname'])){ //sae
sae_set_display_errors(false);
sae_debug($log_content);
sae_set_display_errors(true);
}else if($_server['remote_addr'] != 127.0.0.1){ //local
$max_size = 10000;
$log_filename = log.xml;
if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}
file_put_contents($log_filename, date('h:i:s'). .$log_content.\r\n, file_append);
}
}
}
?>
四、其他注意事项1. 经测试,在自定义菜单中返回多客服消息,无法让用户进入多客服状态。
2. 使用多客服消息后,后续所有消息在一段时间内都将作为客服消息转发,原来的开发模式下的自动回复都将失效!!!
以上就是微信公众平台开发多客服的详细内容。