Skip to content

Commit

Permalink
Merge pull request #163 from HanSon/dev
Browse files Browse the repository at this point in the history
增加拓展支持
  • Loading branch information
Hanson authored Jun 16, 2017
2 parents 0b82fd8 + f2cfd6a commit 4c7558f
Show file tree
Hide file tree
Showing 13 changed files with 287 additions and 75 deletions.
4 changes: 4 additions & 0 deletions example/Example.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public function run()

$robot->messageHandler->setHandler([MessageHandler::class, 'messageHandler']);

$robot->messageExtension->load([
// some extensions
]);

$robot->observer->setQrCodeObserver([Observer::class, 'setQrCodeObserver']);

$robot->observer->setLoginSuccessObserver([Observer::class, 'setLoginSuccessObserver']);
Expand Down
49 changes: 0 additions & 49 deletions example/Handlers/Service/GuessNumber.php

This file was deleted.

23 changes: 0 additions & 23 deletions example/Handlers/Type/TextType.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,6 @@ public static function messageHandler(Collection $message, Friends $friends, Gro
$username = $groups->getUsernameByNickname('vbot 反馈群');
$groups->addMember($username, $message['from']['UserName']);
}

if ($message['fromType'] === 'Friend') {
Text::send($message['from']['UserName'], static::reply($message['content'], $message['from']['UserName']));
}

if ($message['fromType'] === 'Group' && $message['isAt']) {
Text::send($message['from']['UserName'], static::reply($message['pure'], $message['from']['UserName']));
}
}
}

private static function reply($content, $id)
{
try {
$result = vbot('http')->post('http://www.tuling123.com/openapi/api', [
'key' => '9c598b601d8e47acafd81f07770d4bba',
'info' => $content,
'userid' => $id,
], true);

return isset($result['url']) ? $result['text'].$result['url'] : $result['text'];
} catch (\Exception $e) {
return '图灵API连不上了,再问问试试';
}
}
}
2 changes: 0 additions & 2 deletions example/MessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Hanson\Vbot\Example\Handlers\Contact\ExperienceGroup;
use Hanson\Vbot\Example\Handlers\Contact\FeedbackGroup;
use Hanson\Vbot\Example\Handlers\Contact\Hanson;
use Hanson\Vbot\Example\Handlers\Service\GuessNumber;
use Hanson\Vbot\Example\Handlers\Type\RecallType;
use Hanson\Vbot\Example\Handlers\Type\TextType;
use Hanson\Vbot\Message\Emoticon;
Expand Down Expand Up @@ -40,7 +39,6 @@ public static function messageHandler(Collection $message)

TextType::messageHandler($message, $friends, $groups);
RecallType::messageHandler($message);
GuessNumber::messageHandler($message);

if ($message['type'] === 'new_friend') {
Text::send($message['from']['UserName'], '客官,等你很久了!感谢跟 vbot 交朋友,如果可以帮我点个star,谢谢了!https://github.com/HanSon/vbot');
Expand Down
12 changes: 12 additions & 0 deletions example/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,16 @@
],
],
],
/*
* 拓展配置
* ==============================
* 如果加载拓展则必须加载此配置项
*/
'extension' => [
// 管理员配置(必选),优先加载 remark_name
'admin' => [
'remark' => '',
'nickname' => 'vbot',
],
],
];
3 changes: 3 additions & 0 deletions src/Core/MessageHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public function listen($server = null)
{
$this->vbot->beforeMessageObserver->trigger();

$this->vbot->messageExtension->initExtensions();

$time = 0;

while (true) {
Expand Down Expand Up @@ -122,6 +124,7 @@ private function handleMessage($selector)
if ($this->handler) {
call_user_func_array($this->handler, [$collection]);
}
$this->vbot->messageExtension->exec($collection);
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/Exceptions/ExtensionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Hanson\Vbot\Exceptions;

use Exception;

class ExtensionException extends Exception
{
}
147 changes: 147 additions & 0 deletions src/Extension/AbstractMessageHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

namespace Hanson\Vbot\Extension;

use Hanson\Vbot\Exceptions\ExtensionException;
use Hanson\Vbot\Message\Text;
use Illuminate\Support\Collection;

abstract class AbstractMessageHandler
{
public $version = '1.0';

public $author = 'HanSon';

public $name;

public $zhName;

public static $status = true;

public static $admin;

/**
* 拓展配置.
*
* @var
*/
public static $config;

/**
* 初始化拓展.
*/
public function init()
{
static::$config = vbot('config')->get('extension.'.$this->name);

$this->admin();

$this->register();
}

/**
* 注册拓展时的操作.
*/
abstract public function register();

/**
* 开发者需要实现的方法.
*
* @param Collection $collection
*
* @return mixed
*/
abstract public function handler(Collection $collection);

/**
* 消息处理器.
*
* @param Collection $collection
*/
final public function messageHandler(Collection $collection)
{
if ($collection['type'] === 'text' && $this->isAdmin($collection['username'])) {
if (starts_with($collection['content'], $this->name.' ')) {
$content = str_replace($this->name.' ', '', $collection['content']);

switch ($content) {
case 'info':
$this->applicationInfo($collection);
break;
case 'on':
$this->setStatus(true, $collection);
break;
case 'off':
$this->setStatus(false, $collection);
break;
default:
break;
}
}
}

if (!static::$status) {
return;
}

$this->handler($collection);
}

final public function applicationInfo($collection)
{
$status = static::$status ? '' : '';

$admin = static::$admin;

Text::send($collection['from']['UserName'], "当前应用名称:{$this->zhName}\n名称:{$this->name}\n状态:{$status}\n版本:{$this->version}\n作者:{$this->author}\n管理员 Username:{$admin}");
}

/**
* 设置拓展开关.
*
* @param bool $boolean
* @param $collection
*/
final public function setStatus(bool $boolean, $collection)
{
static::$status = $boolean;

$status = static::$status ? '' : '';

Text::send($collection['from']['UserName'], "应用:{$this->zhName} 状态已更改为:{$status}");
}

/**
* 设置管理员.
*
* @throws ExtensionException
*/
final public function admin()
{
$remark = vbot('config')->get('extension.admin.remark');

if ($remark) {
static::$admin = vbot('friends')->getUsernameByRemarkName($remark);
}

if (!$remark && ($nickname = vbot('config')->get('extension.admin.nickname'))) {
static::$admin = vbot('friends')->getUsernameByNickname($nickname);
}

if (!static::$admin) {
throw new ExtensionException('extension admin invalid.');
}
}

/**
* 判断是否管理员.
*
* @param $username
*
* @return bool
*/
private function isAdmin($username)
{
return $username === static::$admin || $username === vbot('myself')->username;
}
}
77 changes: 77 additions & 0 deletions src/Extension/MessageExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace Hanson\Vbot\Extension;

use Hanson\Vbot\Exceptions\ExtensionException;
use Hanson\Vbot\Foundation\Vbot;

class MessageExtension
{
/**
* @var Vbot
*/
protected $vbot;

protected $extensions = [];

public function __construct(Vbot $vbot)
{
$this->vbot = $vbot;
}

/**
* 读取消息拓展.
*
* @param $extensions
*
* @throws ExtensionException
*/
public function load($extensions)
{
if (!is_array($extensions)) {
throw new ExtensionException('extensions must pass an array.');
}

foreach ($extensions as $extension) {
$this->addExtension($extension);
}
}

/**
* 初始化拓展.
*/
public function initExtensions()
{
foreach ($this->extensions as $extension) {
(new $extension())->init();
}
}

/**
* 执行拓展.
*
* @param $collection
*/
public function exec($collection)
{
foreach ($this->extensions as $extension) {
(new $extension())->messageHandler($collection);
}
}

/**
* 添加消息拓展.
*
* @param $extension
*
* @throws ExtensionException
*/
private function addExtension($extension)
{
if ($extension instanceof AbstractMessageHandler) {
throw new ExtensionException($extension.' is not extend AbstractMessageHandler');
}

$this->extensions[] = $extension;
}
}
2 changes: 2 additions & 0 deletions src/Foundation/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Hanson\Vbot\Exceptions\ArgumentException;
use Hanson\Vbot\Exceptions\ConfigErrorException;
use Hanson\Vbot\Exceptions\CreateGroupException;
use Hanson\Vbot\Exceptions\ExtensionException;
use Hanson\Vbot\Exceptions\FetchUuidException;
use Hanson\Vbot\Exceptions\LoginFailedException;
use Hanson\Vbot\Exceptions\LoginTimeoutException;
Expand All @@ -26,6 +27,7 @@ class ExceptionHandler
LoginTimeoutException::class,
ConfigErrorException::class,
LoginFailedException::class,
ExtensionException::class,
];

/**
Expand Down
Loading

0 comments on commit 4c7558f

Please sign in to comment.