xuanbot.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. abstract class xuanBot
  3. {
  4. /**
  5. * 类名 (PHP 5.5 以下不能用 ::class 获取,所以需要提供)
  6. * Class name of this class, since ::class is introduced in PHP 5.5.
  7. */
  8. const className = __CLASS__;
  9. /**
  10. * 机器人的显示名称
  11. * Display name of this bot.
  12. *
  13. * @var string
  14. * @access public
  15. */
  16. public $name = 'XuanBot';
  17. /**
  18. * 机器人的代号,与类名保持一致
  19. * Bot code. It should be the same as the class name.
  20. *
  21. * @var string
  22. * @access public
  23. */
  24. public $code;
  25. /**
  26. * 机器人实体类
  27. * IM object.
  28. *
  29. * @var object
  30. * @access public
  31. */
  32. public $im;
  33. /**
  34. * 机器人的头像图片 URL
  35. * Avatar image URL of this bot.
  36. *
  37. * @var string
  38. * @access public
  39. */
  40. public $avatar;
  41. /**
  42. * 机器人的别名,可以有多个
  43. * Aliases of this bot.
  44. *
  45. * @var array
  46. * @access public
  47. */
  48. public $alias = array();
  49. /**
  50. * 机器人的命令列表,需要与实际命令函数名称一一对应
  51. * Command list of this bot, should be the same as the function names.
  52. *
  53. * 可以使用函数名称或命令对象作为元素,如果使用命令对象,可以设置命令的别名、描述、参数验证器、参数是否必须等属性。
  54. * You can use function name or command object as the element. If you use command object, you can set alias, description, validator, require args and some other properties.
  55. *
  56. * 以下是一个命令对象示例:
  57. * Command object example:
  58. * array(
  59. * 'command' => 'sample', // 命令名称
  60. * // Command name.
  61. * 'description' => 'A sample command.', // 命令描述
  62. * // Command description.
  63. * 'alias' => array('example', 'cmd'), // 别名
  64. * // Alias.
  65. * 'validator' => 'sampleValidator', // 参数验证方法(在对应 bot 中定义)的名称
  66. * // Validator function name (defined in the bot).
  67. * 'argsRequired' => true, // 是否需要参数,如果为真,点击帮助信息中返回的链接不会发送命令,而是填写到输入框
  68. * // Whether the command requires arguments. If true, the command link in the help message will not send the command, but send command into input box.
  69. * 'adminOnly' => true, // 是否只有管理员才能使用
  70. * // Whether the command can only be used by admin.
  71. * 'internal' => true, // 是否切换到该应用才能使用
  72. * // Whether the command can only be used in this app.
  73. * )
  74. *
  75. * @var array
  76. * @access public
  77. */
  78. public $commands = array();
  79. /**
  80. * 机器人的帮助信息,是 Markdown 格式的文本
  81. * Help message of this bot.
  82. *
  83. * @var string
  84. * @access public
  85. */
  86. public $help = '';
  87. /**
  88. * 构造函数,会随 im 模块初始化,可以在这里做一些简单的初始化,复杂的初始化请使用 init() 函数
  89. * Constructor, will be called when im module is initialized.
  90. *
  91. * @access public
  92. * @return void
  93. */
  94. abstract public function __construct();
  95. /**
  96. * 机器人初始化方法,可以在这里进行一些初始化,仅会在机器人初次被调用时执行,适合复杂的初始化步骤
  97. * Initialization method, can be used to initialize some variables, will be called when this bot is called.
  98. *
  99. * @access public
  100. * @return void
  101. */
  102. public function init()
  103. {
  104. // init.
  105. }
  106. /**
  107. * 获取机器人的帮助信息
  108. * Get help message of this bot.
  109. *
  110. * @access public
  111. * @return string
  112. */
  113. public function getHelp()
  114. {
  115. return $this->help;
  116. }
  117. /**
  118. * 打印 Markdown 表格的帮助方法
  119. * Convert items to markdown.
  120. *
  121. * @param array $header
  122. * @param array $items
  123. * @param string $align
  124. * @access public
  125. * @return string
  126. */
  127. public static function printMarkdownTable($header, $items, $align = 'left')
  128. {
  129. $alignIdentifier = array('left' => ':---', 'center' => ':---:', 'right' => '---:');
  130. if(!isset($alignIdentifier[$align])) $align = 'left';
  131. $align = "| " . str_repeat($alignIdentifier[$align] . ' | ', count($header)) . "\n";
  132. $header = "| " . join(" | ", $header) . " |\n";
  133. $content = '';
  134. foreach($items as $key => $item)
  135. {
  136. if(!is_array($item))
  137. {
  138. $content .= "| $key | $item |\n";
  139. continue;
  140. }
  141. $content .= "| " . join(" | ", $item) . " |\n";
  142. }
  143. return $header . $align . $content;
  144. }
  145. // /**
  146. // * 收到普通消息(非命令消息)时的回调函数,可在机器人中实现该函数,会在收到普通消息时调用
  147. // * Callback function when receive a normal message. If this method is implemented in the bot, it will be called on normal message.
  148. // *
  149. // * @param string $message message content
  150. // * @param int $userID sender user id
  151. // * @access public
  152. // * @return string|object
  153. // */
  154. // public function onMessage() {}
  155. // /**
  156. // * 命令函数,可在机器人中实现该函数并注册到命令中,会在收到对应命令时调用
  157. // * Command function, if this method is implemented and registered in the bot, it will be called on command.
  158. // *
  159. // * @param array $args arguments of the command
  160. // * @param int $userID sender user id
  161. // * @param object $user sender user object
  162. // * @access public
  163. // * @return string|object
  164. // */
  165. // public function command($args = array(), $userID = 0, $user = null) {}
  166. }