-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRobot.php
168 lines (141 loc) · 3.84 KB
/
Robot.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* author : [email protected]
* createTime : 2020/11/9 5:31 下午
* description : 机器人library
*/
class Gring_Hi_Robot {
private $webhook;
private $toId;
private $curlServer;
private $attaches = array();
public $proxy;
/**
* Gring_Hi_Robot constructor.
* @param $webhook
*/
public function __construct($webhook) {
$this->webhook = $webhook;
$this->curlServer = new Gring_Service_Curl();
}
/**
* @param Gring_Hi_Contracts_MessageInterface $contractsMessage
* @return $this
*/
public function addAttach(Gring_Hi_Contracts_MessageInterface $contractsMessage) {
$this->attaches[] = $contractsMessage;
return $this;
}
/**
* @param $contractsMessage
* @return $this
*/
public function addAttaches($contractsMessage) {
foreach ($contractsMessage as $message) {
if ($message instanceof Gring_Hi_Contracts_MessageInterface) {
$this->addAttach($message);
} else {
throw new InvalidArgumentException('type is not support.');
}
}
return $this;
}
/**
* @return array
*/
public function getAttaches() {
if (empty($this->attaches)) {
throw new InvalidArgumentException('type must exist.');
}
return $this->attaches;
}
/**
* @param $proxy
* @return $this
*/
public function setProxy($proxy) {
$this->proxy = $proxy;
return $this;
}
/**
* @return mixed
*/
public function getProxy() {
return $this->proxy;
}
/**
* @param array $toId
* @return $this
*/
public function setToId($toId = array()) {
if (!is_array($toId)) {
throw new InvalidArgumentException(sprintf('toid [%s] must array.', $this->toId));
}
$this->toId = $toId;
return $this;
}
/**
* @return mixed
*/
private function getToId() {
return $this->toId;
}
/**
* @return array[]
*/
private function getHeader() {
return array(
"header" => array(
'toid' => $this->getToId(),
),
);
}
/**
* @return mixed
* @throws Exception
*/
public function response() {
return $this->parseResponse(
$this->curlServer->withProxy($this->proxy)
->to($this->webhook)
->asJsonRequest()
->withData($this->getResponseData())
->post()
);
}
/**
* @param $response
* @return mixed
* @throws Exception
*/
public function parseResponse($response) {
$response = json_decode($response, true);
if (!$response || $response['errcode'] != Gring_Hi_ErrCode::SUCCESS) {
throw new Exception(Gring_Hi_ErrCode::getMsg($response['errcode']), $response['errcode']);
}
if (isset($response['data']['fail']) && !empty($response['data']['fail']) && $response['data']['fail'] != Gring_Hi_ErrCode::SUCCESS) {
$errno = array_values($response['data']['fail']);
throw new Exception(Gring_Hi_ErrCode::getMsg($errno[0]), $errno[0]);
}
return $response;
}
/**
* @return array[][]
*/
private function getResponseData() {
$body = array();
/* @var Gring_Hi_Contracts_MessageInterface $attach */
foreach ($this->getAttaches() as $key => $attach) {
$body[$key] = $attach->getBody();
}
$responseData = array(
"message" => array(
"body" => $body,
),
);
if (!empty($this->getToId())) {
$responseData['message'] = $this->getHeader();
}
return $responseData;
}
}