您的位置:首页 > 移动开发 > 微信开发

微信多客服

2015-12-18 17:15 549 查看
如何在没有接触过微信开发时在第三方添加多客服

看文档

动手



看文档

以前,只看过文档里面的一个接入指南。这次要加多客服看到是XML格式数据,不懂数据代表什么以及要怎么处理。先看文档的接收消息:

<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>1348831860</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[this is a test]]></Content>
<MsgId>1234567890123456</MsgId>
</xml>


看了两篇前辈的文章:微信公众平台代码详解-php语言 &《微信公众平台入门到精通》Vol.9 以及 官方开发文档的DEMO_PHP,对微信的消息处理方式有了初步了解。然后看了多客服的文档以及大概扫了下微信公众平台开发(92) 多客服

动手

第三方平台不好找啊,但找找肯定可以找到,先找消息处理。

private function reply($data)
{
//file_put_contents("log2.txt",'咦,where is here?',FILE_APPEND);
//语音功能
if (isset($data['MsgType'])) {
if ('voice' == $data['MsgType']) {
$data['Content'] = $data['Recognition'];
$this->data['Content'] = $data['Recognition'];
....


找到个文件有个私有方法reply,再看到下面注释有语音功能以及多图文什么的注释,没错就是它了。

第一条注释是我从代码里别处找来的,写文件,对于微信开发,不能直接输出,调试方法就只有在执行后查看日志文件了。第一个参数是写文件的路径,第二个是要写入文件的String(数组的话可以用serialize方法写),第三个是FILE_APPEND追加,其余用法不赘述。调试一直都用的它的=-=

这个reply里面处理了很多东西,所以直接来暴力的,直接到此函数底部,看到最后一句

return $this->keyword($data['Content']);


返回了个什么鬼,先不去管。在返回前面加

if(!empty( $data )){
$xmlTpl = "
<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
</xml>";
$result = sprintf($xmlTpl, $this->data['ToUserName'], $this->data['FromUserName'], time(),$this->data['MsgType']);
return $result;
}


这是第一次打出来的,上面有两三处错误。

1. MsgType不应该被赋值,而是应直接定为多客服的transfer_customer_service消息,sprintf里面也需要去掉最后一个参数

2.常规是应该直接返回$result xml,但是第三方不一样,查看了下,别的代码是如此

return array($result,'msgType');


进行返回的。

所以正确写法应该是:

if(!empty( $data )){
$xmlTpl = "
<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[transfer_customer_service]]></MsgType>
</xml>";
$result = sprintf($xmlTpl, $this->data['ToUserName'], $this->data['FromUserName'], time());
return array($result,'event');
}


然后经过几次测试发送消息,多客服端终于有反应了。:)

但是有功能不能用,比如非链接的菜单,比如有个生成名片的应用到第三方程序的都不能用。肯定是我阻挡的哪个返回值。经测试,改成如下:

$gaisidexx=$data['Content'];
if($gaisidexx == '[xx]'){
return $this->keyword($data['Content']);
}
else{
//这里是上面的代码段
}


然后代码坚强的坚持了一天之后,我也不知怎么它就失效了,无论怎么发送消息再也无法触发到多客服。

所以继续用文件法debug,目前为止只改了这一个函数,对三方平台的架构还不了解,往上查询看什么地方调用了reply方法(=-=暴力的全局搜索)。最终锁定响应文件,主函数通过reply()函数返回值,然后list()之后再去调用response()函数:

list($content, $type) = $this->reply($data);
$weixin->response($content, $type);


public function response($content, $type = 'text', $flag = 0){
$this -> data = array('ToUserName' => $this -> data['FromUserName'], 'FromUserName' => $this -> data['ToUserName'], 'CreateTime' => NOW_TIME, 'MsgType' => $type);
$this -> $type($content);
$this -> data['FuncFlag'] = $flag;
$xml = new SimpleXMLElement('<xml></xml>');
....


在给公众号发送了几百条垃圾消息后><,两函数分别改成如下:

//reply()底部代码段:在发送消息中包含'客服'关键词时候,就触发到多客服,不会影响到别的功能(除了关键词一样)
if(strstr($data['Content'],'客服')){
$xmlTpl = "
<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[transfer_customer_service]]></MsgType>
</xml>";
$result = sprintf($xmlTpl, $this->data['ToUserName'], $this->data['FromUserName'], time());
return array($result,'xxxx');//经过别的函数会传送到response里面
}else {
return $this->keyword($data['Content']);
}


//response()顶部代码段
public function response($content, $type = 'text', $flag = 0){
if($type=='xxxx'){
$this -> data = array('ToUserName' => $this -> data['FromUserName'], 'FromUserName' => $this -> data['ToUserName'], 'CreateTime' => NOW_TIME, 'MsgType' =>'transfer_customer_service');//懒得弄变量,就直接把复制上来改MsgType
}else {
$this -> data = array('ToUserName' => $this -> data['FromUserName'], 'FromUserName' => $this -> data['ToUserName'], 'CreateTime' => NOW_TIME, 'MsgType' => $type);
$this -> $type($content);//会调用别的函数,比如 function text($content); function music($content); function new($content),因为没有属于多客服的函数transfer_customer_service($content),所以在数组构造好了后就略过这步,继续构造完整xml
}
$this -> data['FuncFlag'] = $flag;
$xml = new SimpleXMLElement('<xml></xml>');


测试,还好。接着添加菜单[我要咨询]触发多客服这种事就很简单了。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: