交互响应控制器

如果在创建插件的时候勾选了是否需要响应规则,则插件创建成功后会自动生成一个RespondController,在RespondController.class.php中会有一个自动生成的wechat方法用于接收微信端消息并进行响应。

例如聊天机器人插件,在创建的时候勾选了是否需要响应规则,然后在后台响应规则处设置了一个触发关键词聊天,那么当用户在微信端发送消息“聊天”的时候,用户的此条微信消息会被分发到聊天机器人插件的RespondController.class.php中的wechat方法进行响应。我们可以这样来进行响应:

  1. <?php
  2. namespace Addons\IdouChat\Controller;
  3. use Mp\Controller\ApiController;
  4. /**
  5. * 聊天机器人响应控制器
  6. * @author 艾逗笔
  7. */
  8. class RespondController extends ApiController {
  9. /**
  10. * 微信交互
  11. * @param $message array 微信消息数组
  12. * @author 艾逗笔<765532665@qq.com>
  13. */
  14. public function wechat($message = array()) {
  15. $settings = get_addon_settings('IdouChat');
  16. $settings['enter_tip'] || $settings['enter_tip'] = '你想聊点什么呢';
  17. $settings['keep_time'] || $settings['keep_time'] = 300;
  18. $settings['exit_keyword'] || $settings['exit_keyword'] = '退出';
  19. $settings['exit_tip'] || $settings['exit_tip'] = '下次无聊的时候可以再找我聊天哦';
  20. if (!$settings['api_url'] || !$settings['api_key']) {
  21. reply_text('机器人聊天接口未填写,暂时不能使用此功能');
  22. exit();
  23. }
  24. if ($this->message['MsgType'] == 'voice') {
  25. if ($settings['can_voice'] == '1') {
  26. $content = $this->message['Recognition']; // 语音识别,直接开启机器人聊天模式
  27. $reply = $this->turingAPI($content);
  28. if (is_array($reply)) {
  29. return reply_news($reply);
  30. } else {
  31. return reply_text($reply);
  32. }
  33. }
  34. } else {
  35. $content = $this->message['Content']; // 通过消息上下文机制与机器人展开聊天
  36. if (!$this->in_context) {
  37. $reply = $settings['enter_tip'];
  38. $this->begin_context($settings['keep_time']); // 开启上下文模式
  39. } else {
  40. if ($content == $settings['exit_keyword']) {
  41. $reply = $settings['exit_tip'];
  42. $this->end_context();
  43. } else {
  44. $reply = $this->turingAPI($content);
  45. $this->keep_context($settings['keep_time']); // 保持消息上下文
  46. }
  47. }
  48. if (is_array($reply)) {
  49. return reply_news($reply);
  50. } else {
  51. return reply_text($reply);
  52. }
  53. }
  54. }
  55. // 图灵机器人
  56. private function turingAPI($keyword) {
  57. $settings = get_addon_settings('IdouChat');
  58. $settings['api_url'] || $settings['api_url'] = '';
  59. $settings['api_key'] || $settings['api_key'] = '';
  60. $api_url = $settings['api_url'] . "?key=" . $settings['api_key'] . "&info=" . $keyword;
  61. $result = file_get_contents ( $api_url );
  62. $result = json_decode ( $result, true );
  63. if ($_GET ['format'] == 'test') {
  64. dump ( '图灵机器人结果:' );
  65. dump ( $result );
  66. }
  67. if ($result ['code'] > 40000 && $result['code'] < 40008) {
  68. if ($result ['code'] < 40008 && ! empty ( $result ['text'] )) {
  69. return '图灵机器人请你注意:' . $result ['text'];
  70. } else {
  71. return false;
  72. }
  73. }
  74. switch ($result ['code']) {
  75. case '100000' :
  76. return $result['text'];
  77. break;
  78. case '200000' :
  79. $text = $result ['text'] . ',<a href="' . $result ['url'] . '">点击进入</a>';
  80. return $text;
  81. break;
  82. case '301000' :
  83. foreach ( $result ['list'] as $info ) {
  84. $articles [] = array (
  85. 'Title' => $info ['name'],
  86. 'Description' => $info ['author'],
  87. 'PicUrl' => $info ['icon'],
  88. 'Url' => $info ['detailurl']
  89. );
  90. }
  91. return $articles;
  92. break;
  93. case '302000' :
  94. foreach ( $result ['list'] as $info ) {
  95. $articles [] = array (
  96. 'Title' => $info ['article'],
  97. 'Description' => $info ['source'],
  98. 'PicUrl' => $info ['icon'],
  99. 'Url' => $info ['detailurl']
  100. );
  101. }
  102. return $articles;
  103. break;
  104. case '304000' :
  105. foreach ( $result ['list'] as $info ) {
  106. $articles [] = array (
  107. 'Title' => $info ['name'],
  108. 'Description' => $info ['count'],
  109. 'PicUrl' => $info ['icon'],
  110. 'Url' => $info ['detailurl']
  111. );
  112. }
  113. return $articles;
  114. break;
  115. case '305000' :
  116. foreach ( $result ['list'] as $info ) {
  117. $articles [] = array (
  118. 'Title' => $info ['start'] . '--' . $info ['terminal'],
  119. 'Description' => $info ['starttime'] . '--' . $info ['endtime'],
  120. 'PicUrl' => $info ['icon'],
  121. 'Url' => $info ['detailurl']
  122. );
  123. }
  124. return $articles;
  125. break;
  126. case '306000' :
  127. foreach ( $result ['list'] as $info ) {
  128. $articles [] = array (
  129. 'Title' => $info ['flight'] . '--' . $info ['route'],
  130. 'Description' => $info ['starttime'] . '--' . $info ['endtime'],
  131. 'PicUrl' => $info ['icon'],
  132. 'Url' => $info ['detailurl']
  133. );
  134. }
  135. return $articles;
  136. break;
  137. case '307000' :
  138. foreach ( $result ['list'] as $info ) {
  139. $articles [] = array (
  140. 'Title' => $info ['name'],
  141. 'Description' => $info ['info'],
  142. 'PicUrl' => $info ['icon'],
  143. 'Url' => $info ['detailurl']
  144. );
  145. }
  146. return $articles;
  147. break;
  148. case '308000' :
  149. foreach ( $result ['list'] as $info ) {
  150. $articles [] = array (
  151. 'Title' => $info ['name'],
  152. 'Description' => $info ['info'],
  153. 'PicUrl' => $info ['icon'],
  154. 'Url' => $info ['detailurl']
  155. );
  156. }
  157. return $articles;
  158. break;
  159. case '309000' :
  160. foreach ( $result ['list'] as $info ) {
  161. $articles [] = array (
  162. 'Title' => $info ['name'],
  163. 'Description' => '价格 : ' . $info ['price'] . ' 满意度 : ' . $info ['satisfaction'],
  164. 'PicUrl' => $info ['icon'],
  165. 'Url' => $info ['detailurl']
  166. );
  167. }
  168. return $articles;
  169. break;
  170. case '310000' :
  171. foreach ( $result ['list'] as $info ) {
  172. $articles [] = array (
  173. 'Title' => $info ['number'],
  174. 'Description' => $info ['info'],
  175. 'PicUrl' => $info ['icon'],
  176. 'Url' => $info ['detailurl']
  177. );
  178. }
  179. return $articles;
  180. break;
  181. case '311000' :
  182. foreach ( $result ['list'] as $info ) {
  183. $articles [] = array (
  184. 'Title' => $info ['name'],
  185. 'Description' => '价格 : ' . $info ['price'],
  186. 'PicUrl' => $info ['icon'],
  187. 'Url' => $info ['detailurl']
  188. );
  189. }
  190. return $articles;
  191. break;
  192. case '312000' :
  193. foreach ( $result ['list'] as $info ) {
  194. $articles [] = array (
  195. 'Title' => $info ['name'],
  196. 'Description' => '价格 : ' . $info ['price'],
  197. 'PicUrl' => $info ['icon'],
  198. 'Url' => $info ['detailurl']
  199. );
  200. }
  201. return $articles;
  202. break;
  203. default :
  204. if (empty ( $result ['text'] )) {
  205. return false;
  206. } else {
  207. return $result ['text'];
  208. }
  209. }
  210. return true;
  211. }
  212. }

其中,$this->message或者$message数组里面包含了分发到wechat方法的用户消息,包含ToUserName、FromUserName、Content等信息。通过get_addon_settings可以获取插件的配置参数,通过$this->begin_context可以设置消息上下文、通过reply_text可以回复文本消息等。

总而言之。RespondController是用于接收用户通过微信发送来的消息并进行响应的控制器,如果插件的功能需要在微信对话框通过消息的收发来进行交互的话,那么在创建插件的时候就需要选择响应规则,并在微信响应控制器中设计对应的业务逻辑来对用户消息进行响应。