Skip to content

Commit

Permalink
update wechat robot
Browse files Browse the repository at this point in the history
  • Loading branch information
ymlluo committed Oct 28, 2021
1 parent 1b565ed commit c5654bc
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 102 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"ext-zip": "*",
"ext-curl": "*",
"ext-json": "*",
"guzzlehttp/guzzle": "~6"
"guzzlehttp/guzzle": "^7.0"
},
"require-dev": {
"phpunit/phpunit": "~9.0",
Expand Down
19 changes: 11 additions & 8 deletions src/Contracts/Channel.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
<?php


namespace Ymlluo\Contracts;
namespace Ymlluo\GroupRobot\Contracts;


use phpDocumentor\Reflection\Types\Mixed_;

interface Channel
{

/**
* 设置 webhook 地址
* @param string $webhook
* @param mixed $webhook
* @return mixed
*/
public function setWebhook(string $webhook);
public function to(string $webhook);

/**
* 发送消息
*
* @param null $webhook
* @return mixed
*/
public function send();
public function send(string $webhook = '');


/**
* 纯文本消息
*
* @param string $content
* @param array $at
* @param array $at
* @return mixed
*/
public function text(string $content, array $at = []);
Expand Down Expand Up @@ -62,10 +65,10 @@ public function file(string $path, string $filename = '');
/**
* 图片消息
*
* @param string $img
* @param string $path
* @return mixed
*/
public function image(string $img);
public function image(string $path);

/**
* 图文消息
Expand All @@ -83,4 +86,4 @@ public function news(array $news);
*/
public function card(array $card);

}
}
4 changes: 2 additions & 2 deletions src/Contracts/Message.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

namespace Ymlluo\Contracts;
namespace Ymlluo\GroupRobot\Contracts;

interface Message
{


}
}
43 changes: 13 additions & 30 deletions src/GroupRobot.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@

namespace Ymlluo\GroupRobot;

use Ymlluo\Contracts\Channel;
use Ymlluo\Notify\Dingtalk;
use Ymlluo\Notify\Feishu;
use Ymlluo\Notify\Wechat;
use Ymlluo\GroupRobot\Contracts\Channel;
use Ymlluo\GroupRobot\Notify\Dingtalk;
use Ymlluo\GroupRobot\Notify\Feishu;
use Ymlluo\GroupRobot\Notify\Wechat;

class GroupRobot
{
protected $channel;
public $channel;
public $configs =[];

public function __construct($channel = '',$configs = [])
public function __construct($channel = '')
{
$this->channel = $this->resolve($channel);
if ($configs){
$this->configs = $configs;
}else{
$this->configs = [];
}
$this->configs = $configs;
}

/**
Expand All @@ -36,12 +30,11 @@ public function channel(Channel $channel)
protected function resolve($name)
{
if (!$name) {
return $this;
return null;
}
$method = ucfirst($name) . 'Notify';

if (method_exists($this, $method)) {
return $this->{$method};
return call_user_func([$this,$method]);
} else {
throw new InvalidArgumentException("Channel [{$name}] is not supported.");
}
Expand All @@ -50,37 +43,28 @@ protected function resolve($name)

/**
* 飞书
*
* @return $this
*/
public function FeishuNotify()
{
$this->channel = new Feishu();
return $this;
return new Feishu();
}

/**
* 企业微信
*
* @return $this
* @return Wechat
*/
public function WechatNotify()
{
$this->channel = new Wechat();
return $this;

return new Wechat();
}

/**
* 钉钉
*
* @return $this
*/
public function DingtalkNotify()
{
$this->channel = new Dingtalk();
return $this;

return new Dingtalk();
}


Expand All @@ -98,5 +82,4 @@ public function __call($method, array $parameters)
return call_user_func_array([$this->channel, $method], $parameters);
}


}
}
47 changes: 27 additions & 20 deletions src/Notify/BaseNotify.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?php


namespace Ymlluo\Notify;
namespace Ymlluo\GroupRobot\Notify;


use GuzzleHttp\Client;
use Psr\Http\Client\ClientInterface;
use Ymlluo\GroupRobot\GroupRobot;

class BaseNotify
Expand All @@ -15,15 +16,21 @@ class BaseNotify

public $httpClient;

public function __construct()
{
$this->httpClient = new Client();
}

/**
* set webhook url
*
* @param string $webhook
* @param $webhook
* @return $this
*/
public function setWebhook(string $webhook)
public function to(string $webhook)
{
$this->webhook = $webhook;
return $this;
}

/**
Expand All @@ -37,36 +44,36 @@ public function raw(array $data)
}

/**
* send message
* 发送消息
*
* @return mixed|\Psr\Http\Message\StreamInterface
* @param null $webhook
* @return array
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function send()
public function send(string $webhook = '')
{
if (!isset($this->message)) {
throw new \Exception('message not set!');
}
if ($webhook) {
$this->webhook = $webhook;
}
if (!$this->webhook) {
throw new \Exception('webhook not set');
}
$response = $this->httpClient()->post($this->webhook, ['json'=>$this->message,'http_errors'=>false]);
if ($response->getStatusCode() === 200){
return json_decode($response->getBody(),true);
}
return $response->getBody();
$response = $this->httpClient->post($webhook, ['json' => $this->message, 'http_errors' => false]);
$result = json_decode((string)$response->getBody(), true);
return ['params' => $this->message, 'result' => $result];
}

/**
* Http client
*
* @return Client
* 自定义 Guzzle 配置项
* @param array $config
* @return $this
*/
public function httpClient()
public function setClient($config = [])
{
$this->httpClient = new Client();
return $this->httpClient;
$this->httpClient = new Client($config);
return $this;
}


}
}
6 changes: 3 additions & 3 deletions src/Notify/Dingtalk.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php


namespace Ymlluo\Notify;
namespace Ymlluo\GroupRobot\Notify;


use Ymlluo\Contracts\Channel;
use Ymlluo\GroupRobot\Contracts\Channel;

class Dingtalk extends BaseNotify implements Channel
{
Expand Down Expand Up @@ -43,4 +43,4 @@ public function card(array $card)
{
// TODO: Implement card() method.
}
}
}
6 changes: 3 additions & 3 deletions src/Notify/Feishu.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php


namespace Ymlluo\Notify;
namespace Ymlluo\GroupRobot\Notify;


use Ymlluo\Contracts\Channel;
use Ymlluo\GroupRobot\Contracts\Channel;

class Feishu extends BaseNotify implements Channel
{
Expand Down Expand Up @@ -43,4 +43,4 @@ public function card(array $card)
{
// TODO: Implement card() method.
}
}
}
Loading

0 comments on commit c5654bc

Please sign in to comment.