1. 后端处理服务器API
1.1  数据库结构
1.2  后端处理服务器HTTP 接口
1.3. 数据包
1.3.1  数据包的基本信息
1.3.2  所有数据包
2. 后端处理服务器应用集成API
2.1  设置应用集成
2.2  API 格式和签名机制
2.3  API 定义
3. 客户端扩展机制
3.1. 扩展定义
3.1.1  扩展类型
3.1.2  扩展包目录结构
3.1.3  扩展描述文件
3.2. 扩展模块
3.2.1  主入口模块
3.2.2. 扩展模块API
3.2.2.1  扩展 API 概览
3.2.2.2  扩展 API 权限
3.2.2.3  扩展 API 定义
3.2.3  扩展实例对象
3.2.4  数据存储机制
3.2.5  自定义上下文菜单
3.2.6  自定义命令
3.2.7  自定义网址解析
3.3. 开发扩展
3.3.1  载入开发中的扩展
3.3.2  开发应用扩展
3.3.3  开发插件扩展
3.3.4  开发主题扩展
3.4  内置扩展
3.5  分发扩展
4. 会话机器人开发指南
4.1  机器人应用开发

机器人应用开发

2022-12-16 10:59:13
苏萌
5722
最后编辑:苏萌 于 2022-12-21 10:52:51
分享链接

会话机器人功能在 喧喧 7.0 及更高版本中可用,是一种允许用户通过与“小喧喧”发送命令或消息来查询数据、触发自定义操作的功能。机器人具有很高的可定制性,用户可以用 PHP 来编写自己的机器人应用。

本手册简单介绍会话机器人的开发方式。

 

相关代码位置

机器人相关代码位于喧喧后端业务服务器(XXB)中,具体位置为:xxb/module/im/ext/bot/ 下。

该目录用于存放机器人相关代码以及机器人父类,以及默认机器人和示例机器人两个机器人:

xxb/module/im/ext/bot
├── default.bot.php    默认机器人
├── demo.bot.php       示例机器人,如不需要可以删除
└── xuanbot.php        机器人父类 
                  

新建的机器人应放置在该目录中。并且机器人 PHP 文件的命名应符合 [机器人代号].bot.php ,例如代号为 weather 的机器人文件名应为 weather.bot.php ,该 PHP 文件中包含一个继承了机器人父类 xuanBot 的类 [机器人代号]Bot 例如 weatherBot ,机器人的命令函数和其他属性都在该类中声明。

 

机器人代码结构

机器人的具体代码结构,可以参考 demo.bot.php 中的示例机器人,该机器人展示了机器人的基本属性、初始化方式、命令机制。xuanbot.php 文件中也有详细的注释,可以参考进行机器人开发。

以下是一段简单的示例代码:


<?php
class myBot extends xuanBot
{
    /**
     * 机器人的显示名称
     * Display name of this bot.
     *
     * @var    string
     * @access public
     */
    public $name = 'My Bot';
    /**
     * 机器人的代号,与类名保持一致
     * Bot code. It should be the same as the class name.
     *
     * @var    string
     * @access public
     */
    public $code = 'my';
    /**
     * 机器人的头像图片 URL
     * Avatar image URL of this bot.
     *
     * @var    string
     * @access public
     */
    public $avatar = '';
    /**
     * 机器人的别名,可以有多个
     * Aliases of this bot.
     *
     * @var    array
     * @access public
     */
    public $alias = array('mine');
    /**
     * 机器人的命令列表,需要与实际命令函数名称一一对应
     * Command list of this bot, should be the same as the function names.
     *
     * 可以使用函数名称或命令对象作为元素,如果使用命令对象,可以设置命令的别名、描述、参数验证器、参数是否必须等属性。
     * 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.
     *
     * 以下是一个命令对象示例:
     * Command object example:
     *   array(
     *       'command'      => 'sample',                // 命令名称
     *                                                  // Command name.
     *       'description'  => 'A sample command.',     // 命令描述
     *                                                  // Command description.
     *       'alias'        => array('example', 'cmd'), // 别名
     *                                                  // Alias.
     *       'validator'    => 'sampleValidator',       // 参数验证方法(在对应 bot 中定义)的名称
     *                                                  // Validator function name (defined in the bot).
     *       'argsRequired' => true,                    // 是否需要参数,如果为真,点击帮助信息中返回的链接不会发送命令,而是填写到输入框
     *                                                  // 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.
     *       'adminOnly'    => true,                    // 是否只有管理员才能使用
     *                                                  // Whether the command can only be used by admin.
     *       'internal'     => true,                    // 是否切换到该应用才能使用
     *                                                  // Whether the command can only be used in this app.
     *   )
     *
     * @var    array
     * @access public
     */
    public $commands = array();
    /**
     * 机器人的帮助信息,是 Markdown 格式的文本
     * Help message of this bot.
     *
     * @var    string
     * @access public
     */
    public $help = '';
    /**
     * 构造函数,会随 im 模块初始化,可以在这里做一些简单的初始化,复杂的初始化请使用 init() 函数
     * Constructor, will be called when im module is initialized.
     *
     * @access public
     * @return void
     */
    public function __construct()
    {
      
    }
    // /**
    //  * 收到普通消息(非命令消息)时的回调函数,可在机器人中实现该函数,会在收到普通消息时调用
    //  * Callback function when receive a normal message. If this method is implemented in the bot, it will be called on normal message.
    //  *
    //  * @param  string $message  message content
    //  * @param  int    $userID   sender user id
    //  * @access public
    //  * @return string|object
    //  */
    // public function onMessage() {}
    // /**
    //  * 命令函数,可在机器人中实现该函数并注册到命令中,会在收到对应命令时调用
    //  * Command function, if this method is implemented and registered in the bot, it will be called on command.
    //  *
    //  * @param  array  $args     arguments of the command
    //  * @param  int    $userID   sender user id
    //  * @param  object $user     sender user object
    //  * @access public
    //  * @return string|object
    //  */
    // public function command($args = array(), $userID = 0, $user = null) {}
} 
                  

以上代码中,实现了一个简单的机器人的类 myBot ,被注释掉的方法可以选择性地实现。

完成机器人后,放置在对应目录中,就可以向“阿道”发送命令来调用新的机器人了。

发表评论
陆 减 陆 =
评论通过审核后显示。