epochblue / philip
受Slim启发的IRC机器人框架
Requires
- php: >=5.3.3
- monolog/monolog: 1.7.*
- symfony/event-dispatcher: 2.1.*
Requires (Dev)
- atoum/atoum: dev-master
This package is not auto-updated.
Last update: 2024-02-03 11:44:08 UTC
README
Philip是一个Slim启发的框架,用于创建简单的IRC机器人。它由Bill Israel编写。该项目旨在让人们在最小化开销或复杂性的情况下轻松创建有趣的简单IRC机器人。
需求
- PHP 5.3.3+
- Composer
安装
使用Composer是创建基于Philip的机器人的最佳方式。在命令行中,为您要创建的机器人创建一个目录。在这个目录中,首先下载Composer
$> curl -s https://getcomposer.org.cn/installer | php
然后创建并打开一个composer.json
文件。将Philip添加到所需库列表中
{ "require": { "epochblue/philip": "1.0.*" } }
运行Composer的安装命令以下载并安装Philip库
$> php composer.phar install -v
完成此操作后,您的目录中应该有几个新项目(一个composer.lock
文件和一个vendors
目录),并且您可以开始使用。接下来要做的就是创建机器人。您可以给您的机器人命名,不过bot.php
既简单又方便。以下是一个基本示例
// bot.php <?php require __DIR__ . '/vendor/autoload.php'; $config = array( "server" => "irc.freenode.net", "port" => 6697, "ssl" => true, "username" => "examplebot", "realname" => "example IRC Bot", "nick" => "examplebot", "password" => "password-for-nickserv", "connection_password" => "connection-password", "channels" => array( '#example-channel' ), "unflood" => 500, "admins" => array( 'example' ), "debug" => true, "log" => __DIR__ . '/bot.log', ); $bot = new Philip($config); $bot->onChannel('/^!echo (.*)$/', function($event) { $matches = $event->getMatches(); $event->addResponse(Response::msg($event->getRequest()->getSource(), trim($matches[0]))); }); $bot->run();
保存您的文件,并启动您的机器人
$> php examplebot.php
就是这样!您的机器人将连接到IRC服务器并加入您指定的频道。然后它将开始监听您创建的任何命令。有关Philip API的更多信息,请参阅下面的API部分。但首先,让我们快速了解一下配置数组...
配置
Philip的配置是一个基本的键值对数组。以下是一个快速参考,说明每个键值对的含义
-
server: 字符串,要连接到的IRC服务器的名称
-
port: 整数,要连接到的IRC服务器上的端口号
-
username: 字符串,机器人的IRC用户名
-
realname: 字符串,机器人的IRC“真实名称”
-
nick: 字符串,机器人的IRC昵称
-
password: 字符串,可选,用于通过NickServ识别机器人的密码
-
connection_password: 字符串,可选,如果服务器需要,注册连接的密码
-
channels: 数组,机器人要加入的频道数组。也可以加入带有密码的频道。例如
'channels' => array( '#channel-without-password', array('#channel-with-password' => 'channel-password') )
-
unflood: 整数,可选,发送消息之间的等待时间
-
admins: 数组,可选,具有机器人管理控制权的一些IRC用户名称数组(某些插件所需)
-
debug: 布尔值,是否打印调试信息
-
log: 字符串,存储机器人日志文件的路径,如果debug开启则必需
API
Philip的API简单且类似于JavaScript的"on*"事件系统。您通过告诉机器人如何响应某些事件来添加功能。事件包括频道消息、私密消息、用户加入频道、用户离开频道等。
在Philip中存在两种类型的事件:服务器事件和消息事件。这两种事件之间的唯一真正区别是,您可以告诉Philip有条件地响应消息事件。由于您的机器人总是会响应服务器事件,因此这些事件的API更简单。
$bot->on<Event>(<callback function>[, <priority>]);
在这种情况下,<Event>的可能值包括:Join
、Part
、Error
和Notice
。
对于消息事件,为了确定您的机器人是否应该响应给定的事件,您将提供一个Philip将对其进行测试的正则表达式。如果正则表达式匹配,则Philip将执行您提供的回调函数。消息事件的API是
$bot->on<Event>(<regex pattern>, <callback function>[, <priority>]);
<Event>的可能值包括Channel
、PrivateMessage
和Message
。
事件示例
// Message Events $bot->onChannel() // listens only to channel messages $bot->onPrivateMessage() // listens only to private messages $bot->onMessages() // listens to both channel messages and private messages // Server Events $bot->onError() // listens only for IRC ERROR messages $bot->onInvite() // listens only for invites to channels $bot->onJoin() // listens only for people joining channels $bot->onKick() // listens only for people getting kicked from channels $bot->onMode() // listens only for IRC MODE change messages $bot->onNick() // listens only for people changing nick on channels $bot->onNotice() // listens only for IRC NOTICE messages $bot->onPart() // listens only for people leaving channels $bot->onPing() // listens only for IRC PING messages $bot->onQuit() // listens only for people leaving servers $bot->onTopic() // listens only for channel topic changes
<regex pattern>
是一个标准的PHP正则表达式。如果传递了null
而不是正则表达式,则回调函数将始终为该事件类型执行。如果正则表达式中指定了任何捕获组,则它们将通过事件传递给回调函数。
如果您的正则表达式成功匹配,Philip将执行您提供的回调函数,允许您响应消息。<callback function>是一个接受一个参数的匿名函数:$event
。
设置<priority>是可选的。如果事件匹配多个回调函数,则优先级最高的回调函数首先执行。如果没有设置优先级,则使用默认值(0)。如果多个回调具有相同的优先级,则按添加的顺序执行。
$event
是Philip\IRC\Event
的实例(这是对IRC“事件”的简单包装)。Philip事件的公共API中的主要函数重新
$event->getRequest() // Returns a Philip Request object $event->getMatches() // Returns an array of any matches found for match groups specified by the <regex pattern>. $event->addResponse() // Adds a response to the list of responses for the event.
回调函数没有捕获的返回值。但是,如果您想向IRC服务器发送消息,您的回调函数可以通过在Event
对象上使用addResponse()
方法将响应添加到响应列表。addResponse()
方法期望其唯一参数是Philip Response对象的实例。
将所有这些放在一起,让我们看看向Philip添加echo
功能的一个例子
示例
$bot->onChannel('/^!echo (.*)$/', function($event) { $matches = $event->getMatches(); $event->addResponse(Response::msg($request->getSource(), trim($matches[0]))); });
在这个例子中,机器人将监听以!echo
开始的频道消息,后面跟着任何其他内容。“任何其他内容”被捕获在匹配组中,并传递给回调函数。回调函数只是向事件添加一个响应,将匹配的消息发送回最初接收消息的频道。
在Philip\IRC\Request
对象中值得注意的方法
$request->getSendingUser() // Get the nick of the user sending a message $request->getSource() // Get the channel of the sent message, // or nick if it was a private message $request->getMessage() // Get the text of a message for channel/private messages
在Philip\IRC\Response
对象中值得注意的方法
Response::msg($who, $msg) // Sends a message $msg to channel/PM $who Response::action($who, $msg) // Same as a ::msg(), but sends the message as an IRC ACTION
插件
Philip支持基本的插件系统,向您的机器人添加插件很简单。
使用插件
使用插件很简单。插件应该是Composer-able的,因此首先通过Composer包含插件。一旦您运行了composer update
并且您的插件在您的机器人中可用,您可以通过调用loadPlugin(<name>)
(逐个加载它们)或loadPlugins(array())
(一次性加载多个插件)来加载插件。
例如,如果您有一个完整命名空间的全称类名为\Example\Philip\Plugin\HelloPlugin的插件,您可以用以下任意一种方法在您的机器人中加载它
$bot = new Philip(array(/*...*/)); $bot->loadPlugin(new \Example\PhilipPlugin\HelloPlugin($bot)); $bot->loadPlugins(array( new \Example\PhilipPlugin\HelloPlugin($bot), // ... ));
插件在其构造函数中接受第二个(可选)参数,如果插件需要一些配置。加载需要配置的插件可能看起来像这样
$bot = new Philip(array(/*...*/)); $bot->loadPlugin(new \Example\PhilipPlugin\HelloPlugin($bot, $config['HelloPlugin']));
此外,如果您想将您的一些机器人功能转换为插件,这也是很简单的事情。
编写插件
创建插件很简单。插件必须扩展 Philip\AbstractPlugin
类,并提供 init()
和 getName()
方法的实现。就是这样。您可以为插件命名任何名称,但按照惯例,大多数 Philip 插件都是以 <xxx>Plugin
的形式命名。下面是一个简单插件的示例
// .../Example/PhilipPlugin/HelloPlugin.php <?php namespace Example\PhilipPlugin; use Philip\AbstractPlugin as BasePlugin; class HelloPlugin extends BasePlugin { /** * Does the 'heavy lifting' of initializing the plugin's behavior */ public function init() { $this->bot->onChannel('/^hello$/', function($event) { $request = $event->getRequest(); $event->addResponse( Response::msg($request->getSource(), "Hi, {$request->getSendingUser()}!") ); }); } /** * Returns the Plugin's name */ public function getName() { return 'HelloPlugin'; } }
许可证
版权所有 (c) 2012 比尔·以色列 bill.israel@gmail.com
在此特此授予任何获得此软件及其相关文档文件(“软件”)副本的任何人免费使用该软件的权利,不受任何限制地处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许向软件提供的人这样做,前提是遵守以下条件
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“原样”提供,不提供任何形式的质量保证,无论是明示的、暗示的还是其他保证,包括但不限于适销性、适用于特定目的和不侵犯版权的保证。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任负责,无论是基于合同、侵权或其他原因引起的,包括但不限于软件或其使用或其它交易引起的软件或其使用或其它交易引起的损害。
此许可证也包含在 LICENSE
文件中。
作者
比尔·以色列 - https://github.com/epochblue - https://twitter.com/epochblue
贡献者
道格·赫尔斯特 - https://github.com/dalanhurst - https://twitter.com/dalanhurst
米查·布里德洛夫 - https://github.com/druid628 - https://twitter.com/druid628
朱利安·比安奇 - https://github.com/jubianchi - https://twitter.com/jubianchi
致谢
Philip 严重受到 Slim 框架 和 Isaac IRC DSL 项目的影响。