php-wechat-sdk/src/Wechat/Work/Chat.php
2019-02-20 16:28:32 +08:00

91 lines
2.0 KiB
PHP

<?php
namespace Wechat\Work;
use Wechat\Work\Message\MessageInterface;
class Chat
{
protected $id;
protected $work;
public function __construct($id, $work)
{
$this->work = $work;
$this->id = $id;
}
/**
* 发送消息
* @param $msg MessageInterface
* @return mixed
*/
public function send($msg)
{
$request = $this->work->transformForJsonRequest('appchat/send',
array_merge($msg->toArray(), [
'chatid' => $this->id
]));
return $this->work->request($request);
}
public function create(array $users, $owner = null, $name = null)
{
$params = [
'userlist' => $users,
];
if ($name) {
$params['name'] = $name;
}
if ($owner) {
$params['owner'] = $owner;
}
if ($this->id != null) {
$params['chatid'] = $this->id;
}
$request = $this->transformForJsonRequest('POST', 'appchat/create', $params);
$resp = $this->client->send($request);
return json_decode($resp->getBody(), true);
}
public function get($chatId)
{
$request = $this->transformForJsonRequest('GET', 'appchat/get?chatid=' . $chatId);
$resp = $this->client->send($request);
return json_decode($resp->getBody(), true);
}
public function update($chatId, array $add = null, array $del = null, $name = null, $owner = null)
{
$params = [
'chatid' => $chatId,
];
if ($name) {
$params['name'] = $name;
}
if ($owner) {
$params['owner'] = $owner;
}
if ($add) {
$params['add_user_list'] = $add;
}
if ($del) {
$params['del_user_list'] = $del;
}
$request = $this->transformForJsonRequest('POST', 'appchat/update', $params);
$resp = $this->client->send($request);
return json_decode($resp->getBody(), true);
}
}