根据第3节中的描述,当易信平台给我们推送的消息类型(MsgType)为文本(text)类型时,我们会自动载入一个
handle_text.class.php文件,类名称都为
handleMessage ,调用的都是handle()方法来处理消息,然后将文字或图文消息按规定格式返回给 yixin.class.php 中的调用方法: handleMessage(),然后发送给易信平台。
首先,我们新建一个 handle_text.class.php文件:
-
<?php
-
class handleMessage
-
{
-
// to do here.
-
}
其次,在类 handleMessage 中实现 handle()方法,其接受的参数为易信平台发动的消息(XML)字符串转化的对象:
注意:我以根据输入检索通讯录为例,请根据不同的需要做扩展或改动。
-
/*
-
* 处理接收的文本消息;
-
* 可以预先定义一些文本keyword,根据不同的keyword返回给用户不同的消息
-
* 可以用文本方式或图文方式自动回复给订阅者;
-
*/
-
public function handle($obj)
-
{
-
$content = trim($obj->Content);
-
$content = str_replace("'",'',$content);
-
$method = ''; // action
-
$aryKey = array(); // 查询关键字数组;array('查','姓名',10);
-
$sql = '';
-
if($content == '?' || $content == '?')
-
{
-
// 获取帮助菜单
-
$method = 'help';
-
}
-
elseif($content == '1')
-
{
-
// 通讯录的使用帮助;
-
$method = 'help_txl';
-
}
-
else
-
{
-
// 按姓名查找
-
if(strpos($content, '找') === 0)
-
{
-
$method = 'txl_name';
-
$aryKey = $this->keyToArray($content);
-
if(count($aryKey) == 2)
-
{
-
// 数组元素为2表示没有起始索引,则只返回0-10条记录;
-
$sql = "select * from txl where username like '%{$aryKey[1]}%' limit 10";
-
}
-
if(count($aryKey) == 3)
-
{
-
// 有第三个元素,即查询起始索引,例如10,则表示从第10条开始,检索10条记录;
-
$start = (int)$aryKey[2]; //转换为int
-
$sql = "select * from txl where username like '%{$aryKey[1]}%' limit {$start},10";
-
}
-
}
-
// 按部门查找
-
if(strpos($content, '查') === 0)
-
{
-
$method = 'txl_dep';
-
$aryKey = $this->keyToArray($content); // 查询数组;
-
$depid = 1; // 默认省公司
-
if(count($aryKey) > 1)
-
{
-
$strDep = $aryKey[1]; // 部门关键字
-
if(strpos($strDep, '太原') === 0) {$depid = 2;$strDep = str_replace('太原', '', $strDep);}
-
elseif(strpos($strDep, '大同') === 0) {$depid = 3;$strDep = str_replace('大同', '', $strDep);}
-
elseif(strpos($strDep, '朔州') === 0) {$depid = 4;$strDep = str_replace('朔州', '', $strDep);}
-
elseif(strpos($strDep, '忻州') === 0) {$depid = 5;$strDep = str_replace('忻州', '', $strDep);}
-
elseif(strpos($strDep, '吕梁') === 0) {$depid = 6;$strDep = str_replace('吕梁', '', $strDep);}
-
elseif(strpos($strDep, '阳泉') === 0) {$depid = 7;$strDep = str_replace('阳泉', '', $strDep);}
-
elseif(strpos($strDep, '晋中') === 0) {$depid = 8;$strDep = str_replace('晋中', '', $strDep);}
-
elseif(strpos($strDep, '长治') === 0) {$depid = 9;$strDep = str_replace('长治', '', $strDep);}
-
elseif(strpos($strDep, '临汾') === 0) {$depid = 10;$strDep = str_replace('临汾', '', $strDep);}
-
elseif(strpos($strDep, '运城') === 0) {$depid = 11;$strDep = str_replace('运城', '', $strDep);}
-
elseif(strpos($strDep, '晋城') === 0) {$depid = 12;$strDep = str_replace('晋城', '', $strDep);}
-
$sql = "select * from txl where depid = '{$depid}' and depname like '%{$strDep}%'";
-
if(count($aryKey > 2))
-
{
-
$start = (int)$aryKey[2];
-
$sql .= " limit {$start},10";
-
}
-
else $sql .= ' limit 10';
-
}
-
}
-
}
-
// 根据不同的输入返回不同的结果
-
switch($method)
-
{
-
case 'txl_name':
-
// 查询姓名;
-
if($sql)
-
{
-
$rows = $this->queryResult($sql);
-
$re = $this->fmtTxl($rows);
-
}
-
else $re = '请输入要查询的姓名关键字';
-
return $this->echoText($obj, $re); // 返回格式化文本消息;
-
break;
-
case 'txl_dep':
-
// 按部门查询;
-
if($sql)
-
{
-
$rows = $this->queryResult($sql);
-
$re = $this->fmtTxl($rows);
-
}
-
else $re = '请输入要查询的部门关键字';
-
return $this->echoText($obj, $re);
-
break;
-
case 'help':
-
$re = "通讯录查询使用帮助,请输入1";
-
return $this->echoText($obj, $re);
-
break;
-
case 'help_txl':
-
$re = "限于微信接口文字限制,每次查询只返回10条数据。\n";
-
$re = "查询通讯录可以通过下面两种方式:\n";
-
$re .= "输入:\"找 关键字\" 按姓名查询。\n";
-
$re .= "输入:\"查 关键字\" 按部门查询。\n";
-
$re .= "例:\"找 宋\"可以得到名字中有\"宋\"的结果。\n";
-
$re .= "例:\"找 宋 10\" 可以得到名字中有\"宋\",且返回从第10条开始的结果。\n";
-
$re .= "例:\"查 网络发展 10\" 可以查找\"省公司网络发展部\"从第10条开始的结果。\n";
-
$re .= "例:\"查 太原网络发展 10\" 可以查找\"太原公司网络发展部\"从第10条开始的结果。\n";
-
$re .= "注意1:如果不输入起始索引,则默认返回前10条结果。\n";
-
$re .= "注意2:关键字虽可支持模糊查询,但\"网发\"不能查询\"网络发展\"\n";
-
return $this->echoText($obj, $re);
-
break;
-
case 'Test':
-
$aryNews = array(
-
array(
-
'title'=>'【标题标题标题标题标题标题】',
-
'description' => '摘要摘要摘要摘要摘要摘要摘要摘要摘要摘要。',
-
'picurl' => '',
-
'url' => ''
-
)
-
);
-
return $this->echoNews($obj, $aryNews);
-
break;
-
default :
-
return $this->echoText($obj, '指令错误,请输入"?"获取帮助信息。');
-
break;
-
}
-
}
通讯录查询这块不必细看,对各位也未必有什么用,关键则是分析用户的输入,根据不同的输入返回文本消息或图文消息或其他。
1、回复文本消息 echoText();
-
/*
-
* 发送文本消息;
-
*/
-
private function echoText($obj,$content)
-
{
-
// 发送文本模板;
-
$textTpl = "
-
-
-
%s
-
-
-
";
-
$response = sprintf($textTpl, $obj->FromUserName, $obj->ToUserName, time(), $content);
-
return $response;
-
}
2、回复图文消息echoNews():
-
/*
-
* 发送图文消息
-
* 注意:图文消息一次不能超过10条;
-
*/
-
private function echoNews($obj,$aryNews)
-
{
-
if(!empty($aryNews) && (count($aryNews) < 11))
-
{
-
// 图文消息模板;
-
$re = '
-
. $obj->FromUserName .']]>
-
. $obj->ToUserName .']]>
-
'. time() .'
-
-
'. count($aryNews) .'
-
';
-
for($i=0;$i<count($aryNews);$i++)
-
{
-
$re .= '
-
. $aryNews[$i]['title'] .']]>
-
. $aryNews[$i]['description'] .']]>
-
. $aryNews[$i]['picurl'] .']]>
-
. $aryNews[$i]['url'] .']]>
-
';
-
}
-
$re .= '';
-
return $re;
-
}
-
else return '';
-
}
这样我们就能根据不同的情景和要求自动回复文本或图文消息给订阅者了。
阅读(1719) | 评论(0) | 转发(0) |