PHP框架,用于编写机器人

1.0 2015-08-02 22:05 UTC

This package is not auto-updated.

Last update: 2024-09-21 16:01:48 UTC


README

这是什么?

Alyosha是一个用于IRC机器人的框架,基本上是一个用于任何目的的事件驱动框架——无论是跨社交媒体机器人还是Web服务器(请勿这样做)。

为什么?

因为其他IRC框架完全专注于IRC,而我希望有一个跨媒体的平台。通过编写模块,您可以连接Slack、Facebook机器人等。

它是用PHP编写的,因为我非常喜欢这种语言,而且在我开始考虑这个项目的异步需求之前就开始写了。

使用方法

需求

安装

使用这个库创建一个新的composer项目。

mkdir myBot && cd myBot
composer init --require CyrilleHugues/Alyosha:*
composer install

将以下bot.php添加到项目中

<?php

require_once __DIR__ . '/vendor/autoload.php';

use Alyosha\Core\Kernel;

class AppKernel extends Kernel
{
    protected function registerModules()
    {
        return [];
    }
}

$kernel = new AppKernel(__DIR__ . '/vendor/CyrilleHugues/Alyosha/config.yml');
$kernel->run();

现在您可以启动机器人了,因为它没有任何模块,所以它什么也不做。

php bot.php

您的第一次使用

假设您使用IRC与一些朋友聊天,并想在leet时间(13:37)庆祝。为此,您需要以下模块

  • TimeModule
    • 每秒触发time_module.date事件,以便在您想要的时间触发事件。
  • IrcModule
    • 连接到IRC服务器和频道,并为每个活动发送事件
  • IrcAdminModule
    • 您不想没有主人的机器人,对吧?

因此,让我们在bot.php中将它们添加到其中。

use Alyosha\Core\Kernel;
use Alyosha\IRC\IrcModule;
use Alyosha\IrcAdmin\IrcAdminModule;
use Alyosha\Time\TimeModule;

class AppKernel extends Kernel
{
    protected function registerModules()
    {
        return [
            new IrcModule(),
            new IrcAdminModule(),
            new TimeModule(),
        ];
    }
}

再次执行脚本:php bot.php

哇,它正在写一堆文本!

是的,它连接到了irc.iiens.net服务器,并为您处理了连接到Alyosha频道的所有繁琐工作。

很好,这不是我想连接的地方。

好的,让我们谈谈配置。

配置

如果您阅读了您的bot.php脚本,您有这条线

$kernel = new AppKernel(__DIR__ . '/vendor/CyrilleHugues/Alyosha/config.yml');

它使用lib中的一个.yml文件创建内核。让我们写下自己的config.yml。让我们从IRC配置开始。

irc:
    servers:
        super:
            address: my.super.server
            port: 6667
            nickname: i_m_a_robot
            chans:
                - doc_chan

在这里,我们配置了机器人连接到my.super.server,昵称为i_m_a_robot。您可以通过添加另一个键来使其连接到多个服务器(请参阅https://symfony.com.cn/doc/3.3/components/yaml.html)。

对于此服务器,我们让它连接到一个频道列表,doc_chan。

由于我们负责,我们使用IrcAdminModule,它需要一个密码来验证您。

security:
    password: <your password to administrate the bot>

现在,让我们让我们的机器人考虑这个配置,通过修改以下行

$kernel = new AppKernel(__DIR__ . '/vendor/CyrilleHugues/Alyosha/config.yml');

$kernel = new AppKernel(__DIR__ . '/config.yml');

并再次启动机器人。它使用提供的昵称连接到您的频道……并且什么也不做。让我们改变这一点。

脚本编写

配置composer来自动加载我们的类

看,我不是一个怪物,我会像PHP4一样发送脚本,使用includerequire。将以下内容添加到您的composer.json

...
    },
    "autoload": {
        "psr-4": {"Bot\\": "src/"}
    }
}

并再次运行composer install。现在您可以在src中编写PHP类,并像在所有现代PHP文件中一样使用use Bot\Smthg

创建您的第一个模块

创建一个包含以下内容的src/LeetModule.php

<?php

namespace Bot;

use Alyosha\Core\Event\EventInterface;
use Alyosha\Core\Module\AbstractModule;
use Alyosha\IRC\Event\Command\MessageCommand;
use Alyosha\IRC\Event\IrcCommandEvent;
use Alyosha\Time\TimeEvent;

class LeetModule extends AbstractModule
{
    private $servers = [];
    private $events;

    // the name of the module for all other modules
    public function getName()
    {
        return 'leet_module';
    }

    // You can add it in the `config.yml` and it will be available here to start your module
    public function start(array $config)
    {
        // we get all the channels of the configuration
        foreach ($config['irc']['servers'] as $serverName => $serverConfig) {
            $this->servers[$serverName] = $serverConfig['chans']
        }
    }

    // Which are the event i want for this module
    // Here I only want the time
    public function getSubscriptions()
    {
      return [
          'time_module.date'
      ];
    }

    // Alyosha use this function to notify every module of an event they are subscribed to.
    public function notify(EventInterface $event)
    {
        // always check the type of event before executing logic
        if ($event instanceof TimeEvent && $event->getDate()->format('H:i:s') === "13:37:00") {
            foreach ($this->servers as $serverName => $chanList) {
                foreach ($chanList as $chan) {
                  // We create a message to ask the IRCModule to send a message on a chan.
                  new MessageCommand($serverName, $chan, "LEET TIME !!");
                  // We put the message in an envelope to send it to IRCModule via Alyosha.
                  $this->events[] = new IrcCommandEvent($messageCommand);
                }
            }
        }
    }

    // here we send the new events created
    public function getEvents()
    {
      $events = $this->events;
      $this->events = [];
      return $events;
    }
}

并将其添加到bot.php脚本中。

use Bot\LeetModule;

...

protected function registerModules()
{
    return [
        new IrcModule(),
        new IrcAdminModule(),
        new TimeModule(),
        new LeetModule(),
    ];
}

现在您可以使用php bot.php执行,并且每天在13:37时,它将在所有频道中输出“LEET TIME!!”。

管理Alyosha

不要将脚本留在计算机/服务器上的任意一个随机窗口中。请使用PM2来管理Alyosha进程。