MessageClient.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace EasyWeChat\Work\ExternalContact;
  11. use EasyWeChat\Kernel\BaseClient;
  12. use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
  13. /**
  14. * Class MessageClient.
  15. *
  16. * @author milkmeowo <milkmeowo@gmail.com>
  17. */
  18. class MessageClient extends BaseClient
  19. {
  20. /**
  21. * Required attributes.
  22. *
  23. * @var array
  24. */
  25. protected $required = ['content', 'media_id', 'title', 'url', 'pic_media_id', 'appid', 'page'];
  26. protected $textMessage = [
  27. 'content' => '',
  28. ];
  29. protected $imageMessage = [
  30. 'media_id' => '',
  31. ];
  32. protected $linkMessage = [
  33. 'title' => '',
  34. 'picurl' => '',
  35. 'desc' => '',
  36. 'url' => '',
  37. ];
  38. protected $miniprogramMessage = [
  39. 'title' => '',
  40. 'pic_media_id' => '',
  41. 'appid' => '',
  42. 'page' => '',
  43. ];
  44. /**
  45. * 添加企业群发消息模板
  46. *
  47. * @see https://work.weixin.qq.com/api/doc#90000/90135/91560
  48. *
  49. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  50. *
  51. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  52. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  53. * @throws \GuzzleHttp\Exception\GuzzleException
  54. */
  55. public function submit(array $msg)
  56. {
  57. $params = $this->formatMessage($msg);
  58. return $this->httpPostJson('cgi-bin/externalcontact/add_msg_template', $params);
  59. }
  60. /**
  61. * 获取企业群发消息发送结果.
  62. *
  63. * @see https://work.weixin.qq.com/api/doc#90000/90135/91561
  64. *
  65. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  66. *
  67. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  68. * @throws \GuzzleHttp\Exception\GuzzleException
  69. */
  70. public function get(string $msgId)
  71. {
  72. return $this->httpPostJson('cgi-bin/externalcontact/get_group_msg_result', [
  73. 'msgid' => $msgId,
  74. ]);
  75. }
  76. /**
  77. * 发送新客户欢迎语.
  78. *
  79. * @see https://work.weixin.qq.com/api/doc#90000/90135/91688
  80. *
  81. * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
  82. *
  83. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  84. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  85. * @throws \GuzzleHttp\Exception\GuzzleException
  86. */
  87. public function sendWelcome(string $welcomeCode, array $msg)
  88. {
  89. $formattedMsg = $this->formatMessage($msg);
  90. $params = array_merge($formattedMsg, [
  91. 'welcome_code' => $welcomeCode,
  92. ]);
  93. return $this->httpPostJson('cgi-bin/externalcontact/send_welcome_msg', $params);
  94. }
  95. /**
  96. * @return array
  97. *
  98. * @throws InvalidArgumentException
  99. */
  100. protected function formatMessage(array $data = [])
  101. {
  102. $params = $data;
  103. if (!empty($params['text'])) {
  104. $params['text'] = $this->formatFields($params['text'], $this->textMessage);
  105. }
  106. if (!empty($params['image'])) {
  107. $params['image'] = $this->formatFields($params['image'], $this->imageMessage);
  108. }
  109. if (!empty($params['link'])) {
  110. $params['link'] = $this->formatFields($params['link'], $this->linkMessage);
  111. }
  112. if (!empty($params['miniprogram'])) {
  113. $params['miniprogram'] = $this->formatFields($params['miniprogram'], $this->miniprogramMessage);
  114. }
  115. return $params;
  116. }
  117. /**
  118. * @return array
  119. *
  120. * @throws InvalidArgumentException
  121. */
  122. protected function formatFields(array $data = [], array $default = [])
  123. {
  124. $params = array_merge($default, $data);
  125. foreach ($params as $key => $value) {
  126. if (in_array($key, $this->required, true) && empty($value) && empty($default[$key])) {
  127. throw new InvalidArgumentException(sprintf('Attribute "%s" can not be empty!', $key));
  128. }
  129. $params[$key] = empty($value) ? $default[$key] : $value;
  130. }
  131. return $params;
  132. }
  133. }