Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26031
  • 博文数量: 4
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 60
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-14 19:22
个人简介

live a easy life.

文章分类

全部博文(4)

文章存档

2014年(4)

我的朋友

分类: PHP

2014-03-31 15:05:17

根据第3节中的描述,当易信平台给我们推送的消息类型(MsgType)为文本(text)类型时,我们会自动载入一个handle_text.class.php文件,类名称都为 handleMessage ,调用的都是handle()方法来处理消息,然后将文字或图文消息按规定格式返回给 yixin.class.php 中的调用方法: handleMessage(),然后发送给易信平台。
首先,我们新建一个 handle_text.class.php文件:

  1. <?php
  2. class handleMessage
  3. {
  4.      // to do here.
  5. }
其次,在类 handleMessage 中实现 handle()方法,其接受的参数为易信平台发动的消息(XML)字符串转化的对象:
注意:我以根据输入检索通讯录为例,请根据不同的需要做扩展或改动。
  1.     /*
  2.      * 处理接收的文本消息;
  3.      * 可以预先定义一些文本keyword,根据不同的keyword返回给用户不同的消息
  4.      * 可以用文本方式或图文方式自动回复给订阅者;
  5.      */
  6.     public function handle($obj)
  7.     {
  8.         $content = trim($obj->Content);
  9.         $content = str_replace("'",'',$content);
  10.         $method = ''; // action
  11.         $aryKey = array(); // 查询关键字数组;array('查','姓名',10);
  12.         $sql = '';
  13.         if($content == '?' || $content == '?')
  14.         {
  15.             // 获取帮助菜单
  16.             $method = 'help';
  17.         }
  18.         elseif($content == '1')
  19.         {
  20.             // 通讯录的使用帮助;
  21.             $method = 'help_txl';
  22.         }
  23.         else
  24.         {
  25.             // 按姓名查找
  26.             if(strpos($content, '找') === 0)
  27.             {
  28.                 $method = 'txl_name';
  29.                 $aryKey = $this->keyToArray($content);
  30.                 if(count($aryKey) == 2)
  31.                 {
  32.                     // 数组元素为2表示没有起始索引,则只返回0-10条记录;
  33.                     $sql = "select * from txl where username like '%{$aryKey[1]}%' limit 10";
  34.                 }
  35.                 if(count($aryKey) == 3)
  36.                 {
  37.                     // 有第三个元素,即查询起始索引,例如10,则表示从第10条开始,检索10条记录;
  38.                     $start = (int)$aryKey[2]; //转换为int
  39.                     $sql = "select * from txl where username like '%{$aryKey[1]}%' limit {$start},10";
  40.                 }
  41.             }
  42.             // 按部门查找
  43.             if(strpos($content, '查') === 0)
  44.             {
  45.                 $method = 'txl_dep';
  46.                 $aryKey = $this->keyToArray($content); // 查询数组;
  47.                 $depid = 1; // 默认省公司
  48.                 if(count($aryKey) > 1)
  49.                 {
  50.                     $strDep = $aryKey[1]; // 部门关键字
  51.                     if(strpos($strDep, '太原') === 0) {$depid = 2;$strDep = str_replace('太原', '', $strDep);}
  52.                     elseif(strpos($strDep, '大同') === 0) {$depid = 3;$strDep = str_replace('大同', '', $strDep);}
  53.                     elseif(strpos($strDep, '朔州') === 0) {$depid = 4;$strDep = str_replace('朔州', '', $strDep);}
  54.                     elseif(strpos($strDep, '忻州') === 0) {$depid = 5;$strDep = str_replace('忻州', '', $strDep);}
  55.                     elseif(strpos($strDep, '吕梁') === 0) {$depid = 6;$strDep = str_replace('吕梁', '', $strDep);}
  56.                     elseif(strpos($strDep, '阳泉') === 0) {$depid = 7;$strDep = str_replace('阳泉', '', $strDep);}
  57.                     elseif(strpos($strDep, '晋中') === 0) {$depid = 8;$strDep = str_replace('晋中', '', $strDep);}
  58.                     elseif(strpos($strDep, '长治') === 0) {$depid = 9;$strDep = str_replace('长治', '', $strDep);}
  59.                     elseif(strpos($strDep, '临汾') === 0) {$depid = 10;$strDep = str_replace('临汾', '', $strDep);}
  60.                     elseif(strpos($strDep, '运城') === 0) {$depid = 11;$strDep = str_replace('运城', '', $strDep);}
  61.                     elseif(strpos($strDep, '晋城') === 0) {$depid = 12;$strDep = str_replace('晋城', '', $strDep);}
  62.                     $sql = "select * from txl where depid = '{$depid}' and depname like '%{$strDep}%'";
  63.                     if(count($aryKey > 2))
  64.                     {
  65.                         $start = (int)$aryKey[2];
  66.                         $sql .= " limit {$start},10";
  67.                     }
  68.                     else $sql .= ' limit 10';
  69.                 }
  70.             }
  71.         }
  72.         // 根据不同的输入返回不同的结果
  73.         switch($method)
  74.         {
  75.             case 'txl_name':
  76.                 // 查询姓名;
  77.                 if($sql)
  78.                 {
  79.                     $rows = $this->queryResult($sql);
  80.                     $re = $this->fmtTxl($rows);
  81.                 }
  82.                 else $re = '请输入要查询的姓名关键字';
  83.                 return $this->echoText($obj, $re); // 返回格式化文本消息;
  84.                 break;
  85.             case 'txl_dep':
  86.                 // 按部门查询;
  87.                 if($sql)
  88.                 {
  89.                     $rows = $this->queryResult($sql);
  90.                     $re = $this->fmtTxl($rows);
  91.                 }
  92.                 else $re = '请输入要查询的部门关键字';
  93.                 return $this->echoText($obj, $re);
  94.                 break;
  95.             case 'help':
  96.                 $re = "通讯录查询使用帮助,请输入1";
  97.                 return $this->echoText($obj, $re);
  98.                 break;
  99.             case 'help_txl':
  100.                 $re = "限于微信接口文字限制,每次查询只返回10条数据。\n";
  101.                 $re = "查询通讯录可以通过下面两种方式:\n";
  102.                 $re .= "输入:\"找 关键字\" 按姓名查询。\n";
  103.                 $re .= "输入:\"查 关键字\" 按部门查询。\n";
  104.                 $re .= "例:\"找 宋\"可以得到名字中有\"宋\"的结果。\n";
  105.                 $re .= "例:\"找 宋 10\" 可以得到名字中有\"宋\",且返回从第10条开始的结果。\n";
  106.                 $re .= "例:\"查 网络发展 10\" 可以查找\"省公司网络发展部\"从第10条开始的结果。\n";
  107.                 $re .= "例:\"查 太原网络发展 10\" 可以查找\"太原公司网络发展部\"从第10条开始的结果。\n";
  108.                 $re .= "注意1:如果不输入起始索引,则默认返回前10条结果。\n";
  109.                 $re .= "注意2:关键字虽可支持模糊查询,但\"网发\"不能查询\"网络发展\"\n";
  110.                 return $this->echoText($obj, $re);
  111.                 break;
  112.             case 'Test':
  113.                 $aryNews = array(
  114.                     array(
  115.                         'title'=>'【标题标题标题标题标题标题】',
  116.                         'description' => '摘要摘要摘要摘要摘要摘要摘要摘要摘要摘要。',
  117.                         'picurl' => '',
  118.                         'url' => ''
  119.                     )
  120.                 );
  121.                 return $this->echoNews($obj, $aryNews);
  122.                 break;        
  123.             default :
  124.                 return $this->echoText($obj, '指令错误,请输入"?"获取帮助信息。');
  125.                 break;
  126.         }
  127.     }
通讯录查询这块不必细看,对各位也未必有什么用,关键则是分析用户的输入,根据不同的输入返回文本消息或图文消息或其他。
1、回复文本消息 echoText();
  1.     /*
  2.      * 发送文本消息;
  3.      */
  4.     private function echoText($obj,$content)
  5.     {
  6.         // 发送文本模板;
  7.         $textTpl = "
  8.                     
  9.                     
  10.                     %s
  11.                     
  12.                     
  13.                     ";
  14.         $response = sprintf($textTpl, $obj->FromUserName, $obj->ToUserName, time(), $content);
  15.         return $response;
  16.     }
2、回复图文消息echoNews():
  1.     /*
  2.      * 发送图文消息
  3.      * 注意:图文消息一次不能超过10条;
  4.      */
  5.     private function echoNews($obj,$aryNews)
  6.     {
  7.         if(!empty($aryNews) && (count($aryNews) < 11))
  8.         {
  9.             // 图文消息模板;
  10.             $re = '
  11.                     . $obj->FromUserName .']]>
  12.                     . $obj->ToUserName .']]>
  13.                     '. time() .'
  14.                     
  15.                     '. count($aryNews) .'
  16.                     ';
  17.             for($i=0;$i<count($aryNews);$i++)
  18.             {
  19.                 $re .= '
  20.                             <![CDATA['<span style="color:#0000CC;">.</span> <span style="color:#0000FF;">$</span><span style="color:#008080;">aryNews</span><span style="color:#0000CC;">[</span><span style="color:#0000FF;">$</span><span style="color:#008080;">i</span><span style="color:#0000CC;">]</span><span style="color:#0000CC;">[</span><span style="color:#FF00FF;">'title'</span><span style="color:#0000CC;">]</span> <span style="color:#0000CC;">.</span><span style="color:#FF00FF;">']]>
  21.                             . $aryNews[$i]['description'] .']]>
  22.                             . $aryNews[$i]['picurl'] .']]>
  23.                             . $aryNews[$i]['url'] .']]>
  24.                         ';
  25.             }
  26.             $re .= '';
  27.             return $re;
  28.         }
  29.         else return '';
  30.     }
这样我们就能根据不同的情景和要求自动回复文本或图文消息给订阅者了。
阅读(1719) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~