demo.bot.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. class demoBot extends xuanBot
  3. {
  4. /**
  5. * 机器人的显示名称
  6. * Display name of this bot.
  7. *
  8. * @var string
  9. * @access public
  10. */
  11. public $name = '示例';
  12. /**
  13. * 机器人的代号
  14. * Bot code.
  15. *
  16. * @var string
  17. * @access public
  18. */
  19. public $code = 'demo';
  20. /**
  21. * 机器人的命令列表
  22. * Command list of this bot.
  23. *
  24. * @var array
  25. * @access public
  26. */
  27. public $commands = array();
  28. /**
  29. * 机器人的帮助信息
  30. * Help message of this bot.
  31. *
  32. * @var string
  33. * @access public
  34. */
  35. public $help = 'Hello from demo bot!';
  36. /**
  37. * 构造函数
  38. * Constructor.
  39. *
  40. * @access public
  41. * @return void
  42. */
  43. public function __construct()
  44. {
  45. $this->commands[] = 'whoami';
  46. $this->commands[] = array('command' => 'ping', 'alias' => array('乒'), 'description' => 'Ping the bot.');
  47. $this->commands[] = array('command' => 'getUser', 'description' => 'Get current user.');
  48. $this->commands[] = array('command' => 'star', 'alias' => array('pin', '置顶', '收藏'), 'description' => 'Pin this chat.');
  49. $this->commands[] = array('command' => 'unstar', 'alias' => array('unpin', '取消置顶', '取消收藏'), 'description' => 'Unpin this chat.');
  50. $this->commands[] = array('command' => 'add', 'alias' => array('sum', '加'), 'description' => 'Calculate sum of given numbers.', 'validator' => 'numValidator', 'argsRequired' => true);
  51. }
  52. /**
  53. * 机器人初始化方法,仅会在机器人初次被调用时执行
  54. * Initialization method, will be called when this bot is called.
  55. *
  56. * @access public
  57. * @return void
  58. */
  59. public function init()
  60. {
  61. $this->setHelp();
  62. }
  63. /**
  64. * 设置帮助信息
  65. * Set help message.
  66. *
  67. * @access private
  68. * @return void
  69. */
  70. private function setHelp()
  71. {
  72. $this->help = <<<EOF
  73. This is the help message of {$this->name} bot.
  74. > This help message is updated with `init()` method.
  75. | Command | Description | Bot Code |
  76. | :--- | :--- | :--- |
  77. | [ping](xxc://sendContentToServerBySendbox/ping@demo) | Ping bot | demo |
  78. | [getUser](xxc://sendContentToServerBySendbox/getUser@demo) | Get current user | demo |
  79. | [star](xxc://sendContentToServerBySendbox/star@demo) | Pin bot chat | demo |
  80. | [unstar](xxc://sendContentToServerBySendbox/unstar@demo) | Unpin bot chat | demo |
  81. | [whoami](xxc://sendContentToServerBySendbox/whoami@demo) | Who am I | demo |
  82. | [add](xxc://sendContentToChat/add@demo) | Calculate sum of given numbers | demo |
  83. EOF;
  84. }
  85. // /**
  86. // * 收到普通消息(非命令消息)时的回调函数
  87. // * Callback function when receive a normal message.
  88. // *
  89. // * @param string $message message content
  90. // * @param int $userID sender user id
  91. // * @access public
  92. // * @return string|object
  93. // */
  94. // public function onMessage($message, $userID)
  95. // {
  96. // return "示例机器人收到了消息:{$message}";
  97. // }
  98. /**
  99. * Who am I 示例命令,返回当前用户名称
  100. * Who am I, returns current user name.
  101. *
  102. * @param array $args
  103. * @param int $userID
  104. * @param object $user
  105. * @access public
  106. * @return string
  107. */
  108. public function whoami($args = array(), $userID = 0, $user = null)
  109. {
  110. return empty($user) ? 'unknown' : (empty($user->realname) ? $user->account : $user->realname);
  111. }
  112. /**
  113. * Ping 示例命令,返回 pong
  114. * Ping, returns pong.
  115. *
  116. * @param array $args
  117. * @param int $userID
  118. * @access public
  119. * @return string
  120. */
  121. public function ping($args = array())
  122. {
  123. return empty($args) ? 'pong' : ('pong ' . implode(' ', $args));
  124. }
  125. /**
  126. * 获取当前用户信息示例命令
  127. * Get current user.
  128. *
  129. * @param array $args
  130. * @param int $userID
  131. * @access public
  132. * @return string
  133. */
  134. public function getUser($args = array(), $userID = 0)
  135. {
  136. return json_encode($this->im->userGetByID($userID));
  137. }
  138. /**
  139. * 置顶机器人会话示例命令
  140. * Pin bot chat.
  141. *
  142. * @param array $args
  143. * @param int $userID
  144. * @access public
  145. * @return void
  146. */
  147. public function star($args = array(), $userID = 0)
  148. {
  149. $cgid = "$userID&xuanbot";
  150. $this->im->chatStar('1', $cgid, $userID);
  151. $starResponse = array('method' => 'chatstar', 'result' => 'success', 'data' => array('gid' => $cgid, 'star' => 1), 'users' => array($userID));
  152. return array((object)$starResponse, 'Pinned.');
  153. }
  154. /**
  155. * 取消置顶机器人会话示例命令
  156. * Unpin bot chat.
  157. *
  158. * @param array $args
  159. * @param int $userID
  160. * @access public
  161. * @return void
  162. */
  163. public function unstar($args = array(), $userID = 0)
  164. {
  165. $cgid = "$userID&xuanbot";
  166. $this->im->chatStar('1', $cgid, $userID);
  167. $starResponse = array('method' => 'chatstar', 'result' => 'success', 'data' => array('gid' => $cgid, 'star' => 0), 'users' => array($userID));
  168. return array((object)$starResponse, 'Unpinned.');
  169. }
  170. /**
  171. * Add 示例命令,返回数字相加的结果
  172. *
  173. * @param array $args
  174. * @param int $userID
  175. * @access public
  176. * @return string
  177. */
  178. public function add($args = array(), $userID = 0)
  179. {
  180. $result = 0;
  181. foreach($args as $arg) $result += $arg;
  182. return "$result";
  183. }
  184. /**
  185. * Add 示例命令对应的参数验证函数
  186. * Add command args validator.
  187. *
  188. * @param array $args
  189. * @access public
  190. * @return bool
  191. */
  192. public function numValidator($args = array())
  193. {
  194. foreach($args as $arg)
  195. {
  196. if(!is_numeric($arg)) return false;
  197. }
  198. return true;
  199. }
  200. }