Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Broadcast. #193

Merged
merged 3 commits into from
Jan 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
129 changes: 129 additions & 0 deletions src/Broadcast/Broadcast.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

/*
* This file is part of the EasyWeChat.
*
* (c) overtrue <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

/**
* Broadcast.php.
*
* @author overtrue <[email protected]>
* @copyright 2015 overtrue <[email protected]>
*
* @link https://github.com/overtrue
* @link http://overtrue.me
*/

namespace EasyWeChat\Broadcast;

use EasyWeChat\Core\AbstractAPI;
use EasyWeChat\Core\Exceptions\HttpException;

/**
* Class Broadcast.
*/
class Broadcast extends AbstractAPI
{
const API_SEND_BY_GROUP = 'https://api.weixin.qq.com/cgi-bin/message/mass/sendall';
const API_SEND_BY_OPENID = 'https://api.weixin.qq.com/cgi-bin/message/mass/send';
const API_DELETE = 'https://api.weixin.qq.com/cgi-bin/message/mass/delete';
const API_PREVIEW = 'https://api.weixin.qq.com/cgi-bin/message/mass/preview';
const API_GET = 'http://api.weixin.qq.com/cgi-bin/message/mass/get';

const PREVIEW_BY_OPENID = 'touser';
const PREVIEW_BY_WXH = 'towxname';

const MSG_TYPE_TEXT = 'text'; // 文本
const MSG_TYPE_NEWS = 'news'; // 图文
const MSG_TYPE_VOICE = 'voice'; // 语音
const MSG_TYPE_IMAGE = 'image'; // 图片
const MSG_TYPE_VIDEO = 'video'; // 视频
const MSG_TYPE_CARD = 'card'; // 卡券

/**
* Send a message.
*
* @param string $msgType message type
* @param mixed $message message
* @param mixed $to
*
* @return mixed
*/
public function send($msgType, $message, $to = null)
{
$message = (new MessageBuilder())->msgType($msgType)->message($message)->to($to)->build();

$api = is_array($to) ? self::API_SEND_BY_OPENID : self::API_SEND_BY_GROUP;

return $this->post($api, $message);
}

/**
* Preview a message.
*
* @param string $msgType message type
* @param mixed $message message
* @param string $to
* @param string $by
*
* @return mixed
*/
public function preview($msgType, $message, $to, $by = self::PREVIEW_BY_OPENID)
{
$message = (new MessageBuilder())->msgType($msgType)->message($message)->to($to)->buildPreview($by);

return $this->post(self::API_PREVIEW, $message);
}

/**
* Delete a broadcast.
*
* @param string $msgId
*
* @return bool
*/
public function delete($msgId)
{
$options = [
'msg_id' => $msgId,
];

return $this->post(self::API_DELETE, $options);
}

/**
* Get a broadcast status.
*
* @param string $msgId
*
* @return array
*/
public function status($msgId)
{
$options = [
'msg_id' => $msgId,
];

return $this->post(self::API_GET, $options);
}

/**
* post request.
*
* @param string $url
* @param array|string $options
*
* @return array|bool
*
* @throws HttpException
*/
private function post($url, $options)
{
return $this->parseJSON('post', [$url, $options]);
}
}
22 changes: 22 additions & 0 deletions src/Broadcast/LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 overtrue <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Loading