Messenger.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. */
  6. namespace EasyWeChat\MiniProgram\Business;
  7. use EasyWeChat\Kernel\Exceptions\RuntimeException;
  8. use EasyWeChat\Kernel\Messages\Message;
  9. use EasyWeChat\Kernel\Messages\Raw as RawMessage;
  10. use EasyWeChat\Kernel\Messages\Text;
  11. /**
  12. * Class MessageBuilder.
  13. *
  14. * @author wangdongzhao <elim051@163.com>
  15. */
  16. class Messenger
  17. {
  18. /**
  19. * Messages to send.
  20. *
  21. * @var \EasyWeChat\Kernel\Messages\Message;
  22. */
  23. protected $message;
  24. /**
  25. * Messages target user open id.
  26. *
  27. * @var string
  28. */
  29. protected $to;
  30. /**
  31. * Messages sender staff id.
  32. *
  33. * @var string
  34. */
  35. protected $account;
  36. /**
  37. * Customer service instance.
  38. *
  39. * @var \EasyWeChat\MiniProgram\Business\Client
  40. */
  41. protected $client;
  42. /**
  43. * Messages businessId
  44. *
  45. * @var int
  46. */
  47. protected $businessId;
  48. /**
  49. * MessageBuilder constructor.
  50. *
  51. * @param \EasyWeChat\MiniProgram\Business\Client $client
  52. */
  53. public function __construct(Client $client)
  54. {
  55. $this->client = $client;
  56. }
  57. /**
  58. * Set message to send.
  59. *
  60. * @param string|Message $message
  61. *
  62. * @return Messenger
  63. */
  64. public function message($message)
  65. {
  66. if (is_string($message)) {
  67. $message = new Text($message);
  68. }
  69. $this->message = $message;
  70. return $this;
  71. }
  72. /**
  73. * Set staff account to send message.
  74. *
  75. * @return Messenger
  76. */
  77. public function by(string $account)
  78. {
  79. $this->account = $account;
  80. return $this;
  81. }
  82. /**
  83. * @return Messenger
  84. */
  85. public function from(string $account)
  86. {
  87. return $this->by($account);
  88. }
  89. /**
  90. * Set target user open id.
  91. *
  92. * @param string $openid
  93. *
  94. * @return Messenger
  95. */
  96. public function to($openid)
  97. {
  98. $this->to = $openid;
  99. return $this;
  100. }
  101. /**
  102. * Set target business id.
  103. *
  104. * @param string $businessId
  105. *
  106. * @return Messenger
  107. */
  108. public function business($businessId)
  109. {
  110. $this->businessId = $businessId;
  111. return $this;
  112. }
  113. /**
  114. * Send the message.
  115. *
  116. * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
  117. *
  118. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  119. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  120. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  121. */
  122. public function send()
  123. {
  124. if (empty($this->message)) {
  125. throw new RuntimeException('No message to send.');
  126. }
  127. if ($this->message instanceof RawMessage) {
  128. $message = json_decode($this->message->get('content'), true);
  129. } else {
  130. $prepends = [
  131. 'touser' => $this->to,
  132. ];
  133. if ($this->account) {
  134. $prepends['customservice'] = ['kf_account' => $this->account];
  135. }
  136. if ($this->businessId) {
  137. $prepends['businessid'] = $this->businessId;
  138. }
  139. $message = $this->message->transformForJsonRequest($prepends);
  140. }
  141. return $this->client->send($message);
  142. }
  143. /**
  144. * Return property.
  145. *
  146. * @return mixed
  147. */
  148. public function __get(string $property)
  149. {
  150. if (property_exists($this, $property)) {
  151. return $this->$property;
  152. }
  153. return null;
  154. }
  155. }