Message.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. <?php
  2. namespace Easemob;
  3. use Easemob\Http\Http;
  4. /**
  5. * \~chinese
  6. * Message 用来发送消息
  7. *
  8. * \~english
  9. * The `Message` is used to send message
  10. */
  11. final class Message
  12. {
  13. /**
  14. * @ignore
  15. * @var Auth $auth 授权对象
  16. */
  17. private $auth;
  18. /// @cond
  19. public function __construct($auth)
  20. {
  21. $this->auth = $auth;
  22. }
  23. /// @endcond
  24. /**
  25. * \~chinese
  26. * \brief
  27. * 发送文本消息
  28. *
  29. * @param string $target_type 发送的目标类型;users:给用户发消息,chatgroups:给群发消息,chatrooms:给聊天室发消息
  30. * @param array $target 发送的目标;注意这里需要用数组,数组内添加的最大用户数默认 600 个,即使只有一个用户,也要用数组 [‘u1’];给用户发送时数组元素是用户名,给群组发送时,数组元素是 groupid。
  31. * @param array $message 消息内容
  32. * @param string $from 表示消息发送者;无此字段 Server 会默认设置为 "from": "admin",有 from 字段但值为空串 ("") 时请求失败
  33. * @param string $sync_device 消息发送成功后,是否将消息同步给发送方。true:是;false(默认):否。
  34. * @param boolean $isOnline 该参数值为 true 时,代表 routetype 的值为 “ROUTE_ONLINE”,表示发送消息时只有接收方在线时,才进行消息投递。若接收方离线,将不会收到此条消息。
  35. * @return array 发送给的目标和对应消息 id 的数组或者错误
  36. *
  37. * \~english
  38. * \brief
  39. * Send text message
  40. *
  41. * @param string $target_type Type of target to send; Users: send messages to users, chatgroups: send messages to groups, chatrooms: send messages to chat rooms
  42. * @param array $target Target of transmission; Note that you need to use the array here. The maximum number of users added in the array is 600 by default. Even if there is only one user, you should also use the array ['u1 ']; When sending to the user, the array element is the user name. When sending to the group, the array element is the groupid.
  43. * @param array $message Message content
  44. * @param string $from Indicates the sender of the message; Without this field, the server will default to "from": "admin". When there is a from field but the value is an empty string (""), the request fails
  45. * @param string $sync_device Whether to synchronize the message to the sender after the message is sent successfully. True: Yes; False (default): No.
  46. * @param boolean $isOnline When the parameter value is true, the value representing routetype is "route_online", which means that the message is delivered only when the receiver is online. If the receiver is offline, it will not receive this message.
  47. * @return array The array or error of the target and corresponding message ID sent to
  48. */
  49. public function text($target_type, $target, $message, $from = 'admin', $sync_device = false, $isOnline = false)
  50. {
  51. return $this->send('txt', $target_type, $target, $message, $from, $sync_device, $isOnline);
  52. }
  53. /**
  54. * \~chinese
  55. * \brief
  56. * 发送图片消息
  57. *
  58. * @param string $target_type 发送的目标类型;users:给用户发消息,chatgroups:给群发消息,chatrooms:给聊天室发消息
  59. * @param array $target 发送的目标;注意这里需要用数组,数组内添加的最大用户数默认 600 个,即使只有一个用户,也要用数组 [‘u1’];给用户发送时数组元素是用户名,给群组发送时,数组元素是 groupid。
  60. * @param array $message 消息内容
  61. * @param string $from 表示消息发送者;无此字段 Server 会默认设置为 "from": "admin",有 from 字段但值为空串 ("") 时请求失败
  62. * @param string $sync_device 消息发送成功后,是否将消息同步给发送方。true:是;false(默认):否。
  63. * @param boolean $isOnline 该参数值为 true 时,代表 routetype 的值为 “ROUTE_ONLINE”,表示发送消息时只有接收方在线时,才进行消息投递。若接收方离线,将不会收到此条消息。
  64. * @return array 发送给的目标和对应消息 id 的数组或者错误
  65. *
  66. * \~english
  67. * \brief
  68. * Send picture message
  69. *
  70. * @param string $target_type Type of target to send; Users: send messages to users, chatgroups: send messages to groups, chatrooms: send messages to chat rooms
  71. * @param array $target Target of transmission; Note that you need to use the array here. The maximum number of users added in the array is 600 by default. Even if there is only one user, you should also use the array ['u1 ']; When sending to the user, the array element is the user name. When sending to the group, the array element is the groupid.
  72. * @param array $message Message content
  73. * @param string $from Indicates the sender of the message; Without this field, the server will default to "from": "admin". When there is a from field but the value is an empty string (""), the request fails
  74. * @param string $sync_device Whether to synchronize the message to the sender after the message is sent successfully. True: Yes; False (default): No.
  75. * @param boolean $isOnline When the parameter value is true, the value representing routetype is "route_online", which means that the message is delivered only when the receiver is online. If the receiver is offline, it will not receive this message.
  76. * @return array The array or error of the target and corresponding message ID sent to
  77. */
  78. public function image($target_type, $target, $message, $from = 'admin', $sync_device = false, $isOnline = false)
  79. {
  80. return $this->send('img', $target_type, $target, $message, $from, $sync_device, $isOnline);
  81. }
  82. /**
  83. * \~chinese
  84. * \brief
  85. * 发送语音消息
  86. *
  87. * @param string $target_type 发送的目标类型;users:给用户发消息,chatgroups:给群发消息,chatrooms:给聊天室发消息
  88. * @param array $target 发送的目标;注意这里需要用数组,数组内添加的最大用户数默认 600 个,即使只有一个用户,也要用数组 [‘u1’];给用户发送时数组元素是用户名,给群组发送时,数组元素是 groupid。
  89. * @param array $message 消息内容
  90. * @param string $from 表示消息发送者;无此字段 Server 会默认设置为 "from": "admin",有 from 字段但值为空串 ("") 时请求失败
  91. * @param string $sync_device 消息发送成功后,是否将消息同步给发送方。true:是;false(默认):否。
  92. * @param boolean $isOnline 该参数值为 true 时,代表 routetype 的值为 “ROUTE_ONLINE”,表示发送消息时只有接收方在线时,才进行消息投递。若接收方离线,将不会收到此条消息。
  93. * @return array 发送给的目标和对应消息 id 的数组或者错误
  94. *
  95. * \~english
  96. * \brief
  97. * Send voice message
  98. *
  99. * @param string $target_type Type of target to send; Users: send messages to users, chatgroups: send messages to groups, chatrooms: send messages to chat rooms
  100. * @param array $target Target of transmission; Note that you need to use the array here. The maximum number of users added in the array is 600 by default. Even if there is only one user, you should also use the array ['u1 ']; When sending to the user, the array element is the user name. When sending to the group, the array element is the groupid.
  101. * @param array $message Message content
  102. * @param string $from Indicates the sender of the message; Without this field, the server will default to "from": "admin". When there is a from field but the value is an empty string (""), the request fails
  103. * @param string $sync_device Whether to synchronize the message to the sender after the message is sent successfully. True: Yes; False (default): No.
  104. * @param boolean $isOnline When the parameter value is true, the value representing routetype is "route_online", which means that the message is delivered only when the receiver is online. If the receiver is offline, it will not receive this message.
  105. * @return array The array or error of the target and corresponding message ID sent to
  106. */
  107. public function audio($target_type, $target, $message, $from = 'admin', $sync_device = false, $isOnline = false)
  108. {
  109. return $this->send('audio', $target_type, $target, $message, $from, $sync_device, $isOnline);
  110. }
  111. /**
  112. * \~chinese
  113. * \brief
  114. * 发送视频消息
  115. *
  116. * @param string $target_type 发送的目标类型;users:给用户发消息,chatgroups:给群发消息,chatrooms:给聊天室发消息
  117. * @param array $target 发送的目标;注意这里需要用数组,数组内添加的最大用户数默认 600 个,即使只有一个用户,也要用数组 [‘u1’];给用户发送时数组元素是用户名,给群组发送时,数组元素是 groupid。
  118. * @param array $message 消息内容
  119. * @param string $from 表示消息发送者;无此字段 Server 会默认设置为 "from": "admin",有 from 字段但值为空串 ("") 时请求失败
  120. * @param string $sync_device 消息发送成功后,是否将消息同步给发送方。true:是;false(默认):否。
  121. * @param boolean $isOnline 该参数值为 true 时,代表 routetype 的值为 “ROUTE_ONLINE”,表示发送消息时只有接收方在线时,才进行消息投递。若接收方离线,将不会收到此条消息。
  122. * @return array 发送给的目标和对应消息 id 的数组或者错误
  123. *
  124. * \~english
  125. * \brief
  126. * Send video message
  127. *
  128. * @param string $target_type Type of target to send; Users: send messages to users, chatgroups: send messages to groups, chatrooms: send messages to chat rooms
  129. * @param array $target Target of transmission; Note that you need to use the array here. The maximum number of users added in the array is 600 by default. Even if there is only one user, you should also use the array ['u1 ']; When sending to the user, the array element is the user name. When sending to the group, the array element is the groupid.
  130. * @param array $message Message content
  131. * @param string $from Indicates the sender of the message; Without this field, the server will default to "from": "admin". When there is a from field but the value is an empty string (""), the request fails
  132. * @param string $sync_device Whether to synchronize the message to the sender after the message is sent successfully. True: Yes; False (default): No.
  133. * @param boolean $isOnline When the parameter value is true, the value representing routetype is "route_online", which means that the message is delivered only when the receiver is online. If the receiver is offline, it will not receive this message.
  134. * @return array The array or error of the target and corresponding message ID sent to
  135. */
  136. public function video($target_type, $target, $message, $from = 'admin', $sync_device = false, $isOnline = false)
  137. {
  138. return $this->send('video', $target_type, $target, $message, $from, $sync_device, $isOnline);
  139. }
  140. /**
  141. * \~chinese
  142. * \brief
  143. * 发送文件消息
  144. *
  145. * @param string $target_type 发送的目标类型;users:给用户发消息,chatgroups:给群发消息,chatrooms:给聊天室发消息
  146. * @param array $target 发送的目标;注意这里需要用数组,数组内添加的最大用户数默认 600 个,即使只有一个用户,也要用数组 [‘u1’];给用户发送时数组元素是用户名,给群组发送时,数组元素是 groupid。
  147. * @param array $message 消息内容
  148. * @param string $from 表示消息发送者;无此字段 Server 会默认设置为 "from": "admin",有 from 字段但值为空串 ("") 时请求失败
  149. * @param string $sync_device 消息发送成功后,是否将消息同步给发送方。true:是;false(默认):否。
  150. * @param boolean $isOnline 该参数值为 true 时,代表 routetype 的值为 “ROUTE_ONLINE”,表示发送消息时只有接收方在线时,才进行消息投递。若接收方离线,将不会收到此条消息。
  151. * @return array 发送给的目标和对应消息 id 的数组或者错误
  152. *
  153. * \~english
  154. * \brief
  155. * Send file message
  156. *
  157. * @param string $target_type Type of target to send; Users: send messages to users, chatgroups: send messages to groups, chatrooms: send messages to chat rooms
  158. * @param array $target Target of transmission; Note that you need to use the array here. The maximum number of users added in the array is 600 by default. Even if there is only one user, you should also use the array ['u1 ']; When sending to the user, the array element is the user name. When sending to the group, the array element is the groupid.
  159. * @param array $message Message content
  160. * @param string $from Indicates the sender of the message; Without this field, the server will default to "from": "admin". When there is a from field but the value is an empty string (""), the request fails
  161. * @param string $sync_device Whether to synchronize the message to the sender after the message is sent successfully. True: Yes; False (default): No.
  162. * @param boolean $isOnline When the parameter value is true, the value representing routetype is "route_online", which means that the message is delivered only when the receiver is online. If the receiver is offline, it will not receive this message.
  163. * @return array The array or error of the target and corresponding message ID sent to
  164. */
  165. public function file($target_type, $target, $message, $from = 'admin', $sync_device = false, $isOnline = false)
  166. {
  167. return $this->send('file', $target_type, $target, $message, $from, $sync_device, $isOnline);
  168. }
  169. /**
  170. * \~chinese
  171. * \brief
  172. * 发送位置消息
  173. *
  174. * @param string $target_type 发送的目标类型;users:给用户发消息,chatgroups:给群发消息,chatrooms:给聊天室发消息
  175. * @param array $target 发送的目标;注意这里需要用数组,数组内添加的最大用户数默认 600 个,即使只有一个用户,也要用数组 [‘u1’];给用户发送时数组元素是用户名,给群组发送时,数组元素是 groupid。
  176. * @param array $message 消息内容
  177. * @param string $from 表示消息发送者;无此字段 Server 会默认设置为 "from": "admin",有 from 字段但值为空串 ("") 时请求失败
  178. * @param string $sync_device 消息发送成功后,是否将消息同步给发送方。true:是;false(默认):否。
  179. * @param boolean $isOnline 该参数值为 true 时,代表 routetype 的值为 “ROUTE_ONLINE”,表示发送消息时只有接收方在线时,才进行消息投递。若接收方离线,将不会收到此条消息。
  180. * @return array 发送给的目标和对应消息 id 的数组或者错误
  181. *
  182. * \~english
  183. * \brief
  184. * Send location message
  185. *
  186. * @param string $target_type Type of target to send; Users: send messages to users, chatgroups: send messages to groups, chatrooms: send messages to chat rooms
  187. * @param array $target Target of transmission; Note that you need to use the array here. The maximum number of users added in the array is 600 by default. Even if there is only one user, you should also use the array ['u1 ']; When sending to the user, the array element is the user name. When sending to the group, the array element is the groupid.
  188. * @param array $message Message content
  189. * @param string $from Indicates the sender of the message; Without this field, the server will default to "from": "admin". When there is a from field but the value is an empty string (""), the request fails
  190. * @param string $sync_device Whether to synchronize the message to the sender after the message is sent successfully. True: Yes; False (default): No.
  191. * @param boolean $isOnline When the parameter value is true, the value representing routetype is "route_online", which means that the message is delivered only when the receiver is online. If the receiver is offline, it will not receive this message.
  192. * @return array The array or error of the target and corresponding message ID sent to
  193. */
  194. public function location($target_type, $target, $message, $from = 'admin', $sync_device = false, $isOnline = false)
  195. {
  196. return $this->send('loc', $target_type, $target, $message, $from, $sync_device, $isOnline);
  197. }
  198. /**
  199. * \~chinese
  200. * \brief
  201. * 发送透传消息
  202. *
  203. * @param string $target_type 发送的目标类型;users:给用户发消息,chatgroups:给群发消息,chatrooms:给聊天室发消息
  204. * @param array $target 发送的目标;注意这里需要用数组,数组内添加的最大用户数默认 600 个,即使只有一个用户,也要用数组 [‘u1’];给用户发送时数组元素是用户名,给群组发送时,数组元素是 groupid。
  205. * @param array $message 消息内容
  206. * @param string $from 表示消息发送者;无此字段 Server 会默认设置为 "from": "admin",有 from 字段但值为空串 ("") 时请求失败
  207. * @param string $sync_device 消息发送成功后,是否将消息同步给发送方。true:是;false(默认):否。
  208. * @param boolean $isOnline 该参数值为 true 时,代表 routetype 的值为 “ROUTE_ONLINE”,表示发送消息时只有接收方在线时,才进行消息投递。若接收方离线,将不会收到此条消息。
  209. * @return array 发送给的目标和对应消息 id 的数组或者错误
  210. *
  211. * \~english
  212. * \brief
  213. * Send transparent message
  214. *
  215. * @param string $target_type Type of target to send; Users: send messages to users, chatgroups: send messages to groups, chatrooms: send messages to chat rooms
  216. * @param array $target Target of transmission; Note that you need to use the array here. The maximum number of users added in the array is 600 by default. Even if there is only one user, you should also use the array ['u1 ']; When sending to the user, the array element is the user name. When sending to the group, the array element is the groupid.
  217. * @param array $message Message content
  218. * @param string $from Indicates the sender of the message; Without this field, the server will default to "from": "admin". When there is a from field but the value is an empty string (""), the request fails
  219. * @param string $sync_device Whether to synchronize the message to the sender after the message is sent successfully. True: Yes; False (default): No.
  220. * @param boolean $isOnline When the parameter value is true, the value representing routetype is "route_online", which means that the message is delivered only when the receiver is online. If the receiver is offline, it will not receive this message.
  221. * @return array The array or error of the target and corresponding message ID sent to
  222. */
  223. public function cmd($target_type, $target, $message, $from = 'admin', $sync_device = false, $isOnline = false)
  224. {
  225. return $this->send('cmd', $target_type, $target, $message, $from, $sync_device, $isOnline);
  226. }
  227. /**
  228. * \~chinese
  229. * \brief
  230. * 发送自定义消息
  231. *
  232. * @param string $target_type 发送的目标类型;users:给用户发消息,chatgroups:给群发消息,chatrooms:给聊天室发消息
  233. * @param array $target 发送的目标;注意这里需要用数组,数组内添加的最大用户数默认 600 个,即使只有一个用户,也要用数组 [‘u1’];给用户发送时数组元素是用户名,给群组发送时,数组元素是 groupid。
  234. * @param array $message 消息内容
  235. * @param string $from 表示消息发送者;无此字段 Server 会默认设置为 "from": "admin",有 from 字段但值为空串 ("") 时请求失败
  236. * @param string $sync_device 消息发送成功后,是否将消息同步给发送方。true:是;false(默认):否。
  237. * @param boolean $isOnline 该参数值为 true 时,代表 routetype 的值为 “ROUTE_ONLINE”,表示发送消息时只有接收方在线时,才进行消息投递。若接收方离线,将不会收到此条消息。
  238. * @return array 发送给的目标和对应消息 id 的数组或者错误
  239. *
  240. * \~english
  241. * \brief
  242. * Send custom message
  243. *
  244. * @param string $target_type Type of target to send; Users: send messages to users, chatgroups: send messages to groups, chatrooms: send messages to chat rooms
  245. * @param array $target Target of transmission; Note that you need to use the array here. The maximum number of users added in the array is 600 by default. Even if there is only one user, you should also use the array ['u1 ']; When sending to the user, the array element is the user name. When sending to the group, the array element is the groupid.
  246. * @param array $message Message content
  247. * @param string $from Indicates the sender of the message; Without this field, the server will default to "from": "admin". When there is a from field but the value is an empty string (""), the request fails
  248. * @param string $sync_device Whether to synchronize the message to the sender after the message is sent successfully. True: Yes; False (default): No.
  249. * @param boolean $isOnline When the parameter value is true, the value representing routetype is "route_online", which means that the message is delivered only when the receiver is online. If the receiver is offline, it will not receive this message.
  250. * @return array The array or error of the target and corresponding message ID sent to
  251. */
  252. public function custom($target_type, $target, $message, $from = 'admin', $sync_device = false, $isOnline = false)
  253. {
  254. return $this->send('custom', $target_type, $target, $message, $from, $sync_device, $isOnline);
  255. }
  256. /**
  257. * \~chinese
  258. * \brief
  259. * 获取用户离线消息数
  260. *
  261. * @param string $username 用户名
  262. * @return int|array 用户离线消息数量或者错误
  263. *
  264. * \~english
  265. * \brief
  266. * Get the number of user offline messages
  267. *
  268. * @param string $username User name
  269. * @return int|array Number of offline messages or errors
  270. */
  271. public function countMissedMessages($username)
  272. {
  273. if (!trim($username)) {
  274. \Easemob\exception('Please enter username');
  275. }
  276. $uri = $this->auth->getBaseUri() . '/users/' . $username . '/offline_msg_count';
  277. $resp = Http::get($uri, $this->auth->headers());
  278. if (!$resp->ok()) {
  279. return \Easemob\error($resp);
  280. }
  281. $data = $resp->data();
  282. return $data['data'][$username];
  283. }
  284. /**
  285. * \~chinese
  286. * \brief
  287. * 获取某条离线消息状态
  288. *
  289. * @param string $username 用户名
  290. * @param string $msgId 消息 ID 编号
  291. * @return string|array 离线消息状态(delivered:表示状态为消息已投递;undelivered:表示消息未投递;msg_not_found:消息不存在)或者错误
  292. *
  293. * \~english
  294. * \brief
  295. * Get the status of an offline message
  296. *
  297. * @param string $username User name
  298. * @param string $msgId Message ID number
  299. * @return string|array Offline message status (delivered: indicates that the message has been delivered; undelivered: indicates that the message has not been delivered; msg_not_found: the message does not exist) or error
  300. */
  301. public function isMessageDeliveredToUser($username, $msgId)
  302. {
  303. if (!trim($username)) {
  304. \Easemob\exception('Please enter username');
  305. }
  306. if (!trim($msgId)) {
  307. \Easemob\exception('Please enter message ID');
  308. }
  309. $uri = $this->auth->getBaseUri() . '/users/' . $username . '/offline_msg_status/' . $msgId;
  310. $resp = Http::get($uri, $this->auth->headers());
  311. if (!$resp->ok()) {
  312. return \Easemob\error($resp);
  313. }
  314. $data = $resp->data();
  315. return $data['data'][$msgId];
  316. }
  317. /**
  318. * \~chinese
  319. * \brief
  320. * 获取历史消息文件下载地址
  321. *
  322. * \details
  323. * 导出聊天记录接口不是实时接口,获取成功存在一定的延时,不能够作为实时拉取消息的接口使用。以下 API 均需要企业管理员权限才能访问。此接口一次只能获取一个小时的历史消息。
  324. *
  325. * @param int $datetime 时间,每次只能获取一小时的消息,格式为 yyyyMMddHH 如 2018112717。
  326. * @return string|array 聊天记录文件下载地址或者错误
  327. *
  328. * \~english
  329. * \brief
  330. * Get the download address of historical message file
  331. *
  332. * \details
  333. * The export chat record interface is not a real-time interface. There is a certain delay in obtaining success, so it can not be used as an interface for real-time pulling messages. The following APIs require enterprise administrator privileges to access. This interface can only get historical messages for one hour at a time.
  334. *
  335. * @param int $datetime Time, only one hour of messages can be obtained at a time. The format is yyyymmddhh, such as 2018112717.
  336. * @return string|array Chat record file download address or error
  337. */
  338. public function getHistoryAsUri($dateTime)
  339. {
  340. $dateTime = (int)$dateTime;
  341. if (!$dateTime) {
  342. \Easemob\exception('Please enter the time period to get');
  343. }
  344. $uri = $this->auth->getBaseUri() . '/chatmessages/' . $dateTime;
  345. $resp = Http::get($uri, $this->auth->headers());
  346. if (!$resp->ok()) {
  347. return \Easemob\error($resp);
  348. }
  349. $data = $resp->data();
  350. return isset($data['data'][0]['url']) ? $data['data'][0]['url'] : $data;
  351. }
  352. /**
  353. * \~chinese
  354. * \brief
  355. * 下载消息历史文件到本地
  356. *
  357. * @param int $datetime 时间,每次只能获取一小时的消息,格式为 yyyyMMddHH 如 2018112717。
  358. * @param string $filename 下载后的文件名,消息历史文件是 gz 压缩的。
  359. * @return boolean|array 下载成功或错误
  360. *
  361. * \~english
  362. * \brief
  363. * Download message history file to local
  364. *
  365. * @param int $datetime Time, only one hour of messages can be obtained at a time. The format is yyyymmddhh, such as 2018112717.
  366. * @param string $filename The downloaded file name and message history file are GZ compressed.
  367. * @return boolean|array Download success or error
  368. */
  369. public function getHistoryAsLocalFile($dateTime, $filename)
  370. {
  371. $fileurl = $this->getHistoryAsUri($dateTime);
  372. return copy($fileurl, $filename);
  373. // if (is_string($fileurl)) {
  374. // header("Content-Description: File Transfer");
  375. // header("Content-Type: application/octet-stream");
  376. // header("Content-Disposition: attachment;filename=".$filename);
  377. // header("Content-Transfer-Encoding: binary");
  378. // header("Expires: 0");
  379. // header("Cache-Control: must-revalidate");
  380. // header("Pragma: public");
  381. // header("Content-Length: ". filesize($fileurl));
  382. // ob_clean();
  383. // flush();
  384. // readfile($fileurl);
  385. // exit();
  386. // }
  387. }
  388. /**
  389. * \~chinese
  390. * \brief
  391. * 服务端消息撤回
  392. *
  393. * \details
  394. * 应用管理员可调用接口撤回发送的消息,默认时限为 2 分钟,如需调整请联系环信商务经理。
  395. *
  396. * @param array $msg 要撤回的消息,一维数组代表撤回一条消息,二维数组代表撤回多条消息
  397. * - `msg_id` String 类型,撤回消息的消息 ID。
  398. * - `to` 可选,String 类型,撤回消息的接收方。如果不提供则消息体找不到就撤回不了。单聊为接收方用户名称,群组为群 ID,聊天室为聊天室 ID。
  399. * - `chat_type` String 类型,撤回消息的三种消息类型:单聊:chat;群聊:group_chat;聊天室:chatroom。
  400. * - `from` 可选,String 类型,消息撤回方,不传默认使用的是 admin,默认消息撤回方为原消息发送者。你可以通过用户 ID 指定消息撤回方。
  401. * - `force` boolean 类型,是否为强制撤回:
  402. * - true:是,即超过服务器保存消息时间消息也可以被撤回,具体见服务器消息保存时长;
  403. * - false:否,若设置的消息撤回时限超过服务端的消息保存时间,请求消息撤回时消息可能由于过期已在服务端删除,消息撤回请求会失败,即无法从收到该消息的客户端撤回该消息。
  404. * @return array 撤回的消息或者错误
  405. *
  406. * \~english
  407. * \brief
  408. * Server message withdrawal
  409. *
  410. * \details
  411. * The application administrator can call the interface to withdraw the sent message. The default time limit is 2 minutes. If you need to adjust, please contact the business manager of Huanxin.
  412. *
  413. * @param array $msgs For the message to be withdrawn, one-dimensional array represents withdrawing one message, and two-dimensional array represents withdrawing multiple messages
  414. * - `msg_id` String type, the message ID of the withdrawal message.
  415. * - `to` Optional, String type, The recipient of the recall message. If it is not provided, the message body cannot be found and cannot be withdrawn. The single chat is the user name of the receiver, the group is the group ID, and the chat room is the chat room ID.
  416. * - `chat_type` String type,Three message types of recall messages: single chat: chat; Group chat: Group_ chat; Chat room: chatroom.
  417. * - `from` Optional, String type,The message withdrawing party is not transmitted. By default, admin is used. By default, the message withdrawing party is the original message sender. You can specify the message withdrawing party through the user ID.
  418. * - `force` boolean type, Forced withdrawal:
  419. * - true: Yes, that is, the message can also be withdrawn after the server saves the message. See the server message saving time for details;
  420. * - false: No, if the set message withdrawal time limit exceeds the message saving time of the server, the message may have been deleted at the server due to expiration when requesting message withdrawal, and the message withdrawal request will fail, that is, the message cannot be withdrawn from the client receiving the message.
  421. * @return array Withdrawn message or error
  422. */
  423. public function withdraw($msgs)
  424. {
  425. // 一维数组标识
  426. $OneFlag = false;
  427. if (count($msgs) == count($msgs, 1)) {
  428. // 一维数组
  429. $OneFlag = true;
  430. $this->authMsg($msgs);
  431. } else {
  432. // 多维数组
  433. foreach ($msgs as $msg) {
  434. $this->authMsg($msg);
  435. }
  436. }
  437. $body = compact('msgs');
  438. $uri = $this->auth->getBaseUri() . '/messages/recall';
  439. $resp = Http::post($uri, $body, $this->auth->headers());
  440. if (!$resp->ok()) {
  441. return \Easemob\error($resp);
  442. }
  443. $data = $resp->data();
  444. return isset($data['data']['msgs']) ? $data['data']['msgs'] : $data['data'];
  445. }
  446. /**
  447. * \~chinese
  448. * \brief
  449. * 服务端单向删除会话
  450. *
  451. * @param string $username 用户名
  452. * @param string $channel 要删除的会话 ID。
  453. * @param string $type 会话类型。chat:单聊会话;groupchat:群聊会话。
  454. * @param string $delete_roam 是否删除服务端消息,不允许为空。true:是;false:否。
  455. * @return boolean 成功或者错误
  456. *
  457. * \~english
  458. * \brief
  459. * Server side one-way deletion session
  460. *
  461. * @param string $username User name
  462. * @param string $channel Session ID to delete.
  463. * @param string $type Session type. Chat: single chat session; Group chat: group chat conversation.
  464. * @param string $delete_roam Whether to delete the server message. It cannot be empty. True: Yes; False: No.
  465. * @return boolean Success or error
  466. */
  467. public function deleteSession($username, $channel, $type, $delete_roam = true)
  468. {
  469. if (!trim($username)) {
  470. return \Easemob\exception('Please enter username');
  471. }
  472. $uri = $this->auth->getBaseUri() . '/users/'.$username.'/user_channel';
  473. $delete_roam = (bool)$delete_roam;
  474. $body = compact('channel', 'type', 'delete_roam');
  475. $resp = Http::delete($uri, $body, $this->auth->headers());
  476. if (!$resp->ok()) {
  477. return \Easemob\error($resp);
  478. }
  479. $data = $resp->data();
  480. return isset($data['data']['result']) && $data['data']['result'] === 'ok' ? true : false;
  481. }
  482. /**
  483. * @ignore 验证消息参数
  484. * @param array $msg 消息参数
  485. */
  486. private function authMsg($msg)
  487. {
  488. if (!is_array($msg) || empty($msg)) {
  489. return \Easemob\exception('Please enter a message to recall');
  490. }
  491. if (!isset($msg['msg_id']) || !trim($msg['msg_id']) || !isset($msg['chat_type']) || !trim($msg['chat_type']) || !isset($msg['force'])) {
  492. return \Easemob\exception('Please enter msg_id, chat_type, force');
  493. }
  494. }
  495. /**
  496. * @ignore 发送消息
  497. * @param string $type 消息类型;txt:文本消息,img:图片消息,loc:位置消息,audio:语音消息,video:视频消息,file:文件消息,cmd:透传消息,custom:自定义消息
  498. * @param string $target_type 发送的目标类型;users:给用户发消息,chatgroups:给群发消息,chatrooms:给聊天室发消息
  499. * @param array $target 发送的目标;注意这里需要用数组,数组内添加的最大用户数默认 600 个,即使只有一个用户,也要用数组 ['u1'];给用户发送时数组元素是用户名,给群组发送时,数组元素是 groupid。
  500. * @param mixed $message 消息内容
  501. * @param string $from 表示消息发送者;无此字段 Server 会默认设置为 "from": "admin",有 from 字段但值为空串 ("") 时请求失败
  502. * @param string $sync_device 消息发送成功后,是否将消息同步给发送方。true:是;false(默认):否。
  503. * @param boolean $isOnline 该参数值为 true 时,代表 routetype 的值为 “ROUTE_ONLINE”,表示发送消息时只有接收方在线时,才进行消息投递。若接收方离线,将不会收到此条消息。
  504. * @return array 发送给的目标和对应消息 id 的数组或者错误
  505. */
  506. private function send($type, $target_type, $target, $message, $from = 'admin', $sync_device = false, $isOnline = false)
  507. {
  508. if (!trim($type)) {
  509. \Easemob\exception('Please enter type');
  510. }
  511. if (!trim($target_type)) {
  512. \Easemob\exception('Please enter target_type');
  513. }
  514. if (!is_array($target) || empty($target)) {
  515. \Easemob\exception('Please enter target');
  516. }
  517. if (!is_array($message)) {
  518. \Easemob\exception('Please enter message');
  519. }
  520. if (!trim($from)) {
  521. \Easemob\exception('If the message sender is delivered, it cannot be empty');
  522. }
  523. if (isset($message['ext'])) {
  524. if (!$message['ext']) {
  525. \Easemob\exception('If there is no extended attribute, please remove the EXT field');
  526. } elseif (!is_array($message['ext'])) {
  527. \Easemob\exception('The extended attribute, if any, must be an array');
  528. }
  529. $ext = $message['ext'];
  530. unset($message['ext']);
  531. }
  532. if ($type == 'txt') {
  533. $msg = array('msg' => $message['msg']);
  534. } else {
  535. $msg = $message;
  536. }
  537. switch ($type) {
  538. case 'txt':
  539. // 文本消息
  540. // $msg = array(
  541. // 'msg' => $message['msg'],
  542. // );
  543. break;
  544. case 'img': case 'audio':
  545. // 图片消息 | 语音消息
  546. $msg['url'] = $this->auth->getBaseUri() . '/chatfiles/' . $msg['uuid'];
  547. unset($msg['uuid']);
  548. break;
  549. case 'video':
  550. // 视频消息
  551. $msg['url'] = $this->auth->getBaseUri() . '/chatfiles/' . $msg['uuid'];
  552. $msg['thumb'] = $this->auth->getBaseUri() . '/chatfiles/' . $msg['thumb_uuid'];
  553. unset($msg['uuid'], $msg['thumb_uuid']);
  554. break;
  555. case 'loc': case 'cmd':
  556. // 位置消息 | 透传消息
  557. break;
  558. case 'custom':
  559. // 自定义消息
  560. if (!isset($message['customEvent']) || !preg_match('/^[a-zA-Z0-9-_\/\.]{1,32}$/', $message['customEvent'])) {
  561. \Easemob\exception('User defined event type format error');
  562. }
  563. if (isset($message['customExts']) && !is_array($message['customExts'])) {
  564. \Easemob\exception('User defined event attribute format error');
  565. } elseif (isset($message['customExts'])) {
  566. if (count($message['customExts']) > 16) {
  567. \Easemob\exception('User defined event attributes can contain at most 16 elements');
  568. } else {
  569. foreach ($message['customExts'] as $key => $val) {
  570. if (!is_string($key) || !is_string($val)) {
  571. \Easemob\exception('User defined event attribute element key values can only be strings');
  572. }
  573. }
  574. }
  575. }
  576. break;
  577. }
  578. $msg['type'] = $type;
  579. $uri = $this->auth->getBaseUri() . '/messages?useMsgId=true';
  580. $body = compact('target_type', 'target', 'msg', 'from');
  581. if (isset($ext)) {
  582. $body['ext'] = $ext;
  583. }
  584. if ((bool)$sync_device) {
  585. $body['sync_device'] = true;
  586. }
  587. if ((bool)$isOnline) {
  588. $body['routetype'] = 'ROUTE_ONLINE';
  589. }
  590. $resp = Http::post($uri, $body, $this->auth->headers());
  591. if (!$resp->ok()) {
  592. return \Easemob\error($resp);
  593. }
  594. $data = $resp->data();
  595. return $data['data'];
  596. }
  597. }