|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the overtrue/wechat. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) overtrue <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the MIT license that is bundled |
|
9
|
|
|
* with this source code in the file LICENSE. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* MessageBuilder.php. |
|
14
|
|
|
* |
|
15
|
|
|
* @author overtrue <[email protected]> |
|
16
|
|
|
* @copyright 2015 overtrue <[email protected]> |
|
17
|
|
|
* |
|
18
|
|
|
* @link https://github.com/overtrue |
|
19
|
|
|
* @link http://overtrue.me |
|
20
|
|
|
*/ |
|
21
|
|
|
namespace EasyWeChat\Staff; |
|
22
|
|
|
|
|
23
|
|
|
use EasyWeChat\Core\Exceptions\InvalidArgumentException; |
|
24
|
|
|
use EasyWeChat\Core\Exceptions\RuntimeException; |
|
25
|
|
|
use EasyWeChat\Message\AbstractMessage; |
|
26
|
|
|
use EasyWeChat\Message\Raw as RawMessage; |
|
27
|
|
|
use EasyWeChat\Message\Text; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Class MessageBuilder. |
|
31
|
|
|
*/ |
|
32
|
|
|
class MessageBuilder |
|
33
|
|
|
{ |
|
34
|
|
|
/** |
|
35
|
|
|
* Message to send. |
|
36
|
|
|
* |
|
37
|
|
|
* @var \EasyWeChat\Message\AbstractMessage; |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $message; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Message target user open id. |
|
43
|
|
|
* |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $to; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Message sender staff id. |
|
50
|
|
|
* |
|
51
|
|
|
* @var string |
|
52
|
|
|
*/ |
|
53
|
|
|
protected $account; |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Staff instance. |
|
57
|
|
|
* |
|
58
|
|
|
* @var \EasyWeChat\Staff\Staff |
|
59
|
|
|
*/ |
|
60
|
|
|
protected $staff; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* MessageBuilder constructor. |
|
64
|
|
|
* |
|
65
|
|
|
* @param \EasyWeChat\Staff\Staff $staff |
|
66
|
|
|
*/ |
|
67
|
5 |
|
public function __construct(Staff $staff) |
|
68
|
|
|
{ |
|
69
|
5 |
|
$this->staff = $staff; |
|
70
|
5 |
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Set message to send. |
|
74
|
|
|
* |
|
75
|
|
|
* @param string|AbstractMessage $message |
|
76
|
|
|
* |
|
77
|
|
|
* @return MessageBuilder |
|
78
|
|
|
* |
|
79
|
|
|
* @throws InvalidArgumentException |
|
80
|
|
|
*/ |
|
81
|
3 |
|
public function message($message) |
|
82
|
|
|
{ |
|
83
|
3 |
|
if (is_string($message)) { |
|
84
|
2 |
|
$message = new Text(['content' => $message]); |
|
85
|
2 |
|
} |
|
86
|
|
|
|
|
87
|
3 |
|
$this->message = $message; |
|
88
|
|
|
|
|
89
|
3 |
|
return $this; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Set staff account to send message. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $account |
|
96
|
|
|
* |
|
97
|
|
|
* @return MessageBuilder |
|
98
|
|
|
*/ |
|
99
|
2 |
|
public function by($account) |
|
100
|
|
|
{ |
|
101
|
2 |
|
$this->account = $account; |
|
102
|
|
|
|
|
103
|
2 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Set target user open id. |
|
108
|
|
|
* |
|
109
|
|
|
* @param string $openId |
|
110
|
|
|
* |
|
111
|
|
|
* @return MessageBuilder |
|
112
|
|
|
*/ |
|
113
|
2 |
|
public function to($openId) |
|
114
|
|
|
{ |
|
115
|
2 |
|
$this->to = $openId; |
|
116
|
|
|
|
|
117
|
2 |
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Send the message. |
|
122
|
|
|
* |
|
123
|
|
|
* @return bool |
|
124
|
|
|
* |
|
125
|
|
|
* @throws RuntimeException |
|
126
|
|
|
*/ |
|
127
|
2 |
|
public function send() |
|
128
|
|
|
{ |
|
129
|
2 |
|
if (empty($this->message)) { |
|
130
|
1 |
|
throw new RuntimeException('No message to send.'); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
2 |
|
$transformer = new Transformer(); |
|
134
|
|
|
|
|
135
|
2 |
|
if ($this->message instanceof RawMessage) { |
|
136
|
1 |
|
$message = $this->message->get('content'); |
|
137
|
1 |
|
} else { |
|
138
|
1 |
|
$content = $transformer->transform($this->message); |
|
139
|
|
|
|
|
140
|
1 |
|
$message = array_merge([ |
|
141
|
1 |
|
'touser' => $this->to, |
|
142
|
1 |
|
'customservice' => ['kf_account' => $this->account], |
|
143
|
1 |
|
], $content); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
2 |
|
return $this->staff->send($message); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Return property. |
|
151
|
|
|
* |
|
152
|
|
|
* @param $property |
|
153
|
|
|
* |
|
154
|
|
|
* @return mixed |
|
155
|
|
|
*/ |
|
156
|
3 |
|
public function __get($property) |
|
157
|
|
|
{ |
|
158
|
3 |
|
if (property_exists($this, $property)) { |
|
159
|
3 |
|
return $this->$property; |
|
160
|
|
|
} |
|
161
|
1 |
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|