forked from overtrue/easy-sms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 添加twilio gateway * readme添加内容 * 添加常规错误检查 * 测试 * 移除不需要的引用
- Loading branch information
Showing
3 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
<?php | ||
|
||
namespace Overtrue\EasySms\Gateways; | ||
|
||
use Overtrue\EasySms\Contracts\MessageInterface; | ||
use Overtrue\EasySms\Exceptions\GatewayErrorException; | ||
use Overtrue\EasySms\Support\Config; | ||
use Overtrue\EasySms\Traits\HasHttpRequest; | ||
use GuzzleHttp\Exception\ClientException; | ||
|
||
/** | ||
* Class TwilioGateway | ||
* @see https://www.twilio.com/docs/api/messaging/send-messages | ||
*/ | ||
class TwilioGateway extends Gateway | ||
{ | ||
use HasHttpRequest; | ||
|
||
const ENDPOINT_URL = 'https://api.twilio.com/2010-04-01/Accounts/%s/Messages.json'; | ||
|
||
protected $errorStatuses = [ | ||
'failed', | ||
'undelivered' | ||
]; | ||
|
||
public function getName() | ||
{ | ||
return 'twilio'; | ||
} | ||
|
||
public function send($to, MessageInterface $message, Config $config) | ||
{ | ||
$accountSid = $config->get('account_sid'); | ||
$endpoint = $this->buildEndPoint($accountSid); | ||
|
||
$params = [ | ||
'To' => $to, | ||
'From' => $config->get('from'), | ||
'Body' => $message->getContent(), | ||
]; | ||
|
||
try { | ||
$result = $this->request('post', $endpoint, [ | ||
'auth' => [ | ||
$accountSid, | ||
$config->get('token') | ||
], | ||
'form_params' => $params | ||
]); | ||
if (in_array($result['status'], $this->errorStatuses) || !is_null($result['error_code'])) { | ||
throw new GatewayErrorException($result['message'], $result['error_code'], $result); | ||
} | ||
} catch (ClientException $e) { | ||
throw new GatewayErrorException($e->getMessage(), $e->getCode()); | ||
} | ||
return $result; | ||
} | ||
|
||
/** | ||
* build endpoint url | ||
* | ||
* @param string $accountSid | ||
* @return string | ||
*/ | ||
protected function buildEndPoint($accountSid) | ||
{ | ||
return sprintf(self::ENDPOINT_URL, $accountSid); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the overtrue/easy-sms. | ||
* | ||
* (c) overtrue <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Overtrue\EasySms\Tests\Gateways; | ||
|
||
use Overtrue\EasySms\Gateways\TwilioGateway; | ||
use Overtrue\EasySms\Message; | ||
use Overtrue\EasySms\Support\Config; | ||
use Overtrue\EasySms\Tests\TestCase; | ||
|
||
class TwilioGatewayTest extends TestCase | ||
{ | ||
public function testGetName() | ||
{ | ||
$this->assertSame('twilio', (new TwilioGateway([]))->getName()); | ||
} | ||
|
||
public function testSend() | ||
{ | ||
$config = [ | ||
'account_sid' => 'mock-api-account-sid', | ||
'from' => 'mock-from', | ||
'token' => 'mock-token', | ||
]; | ||
$gateway = \Mockery::mock(TwilioGateway::class.'[request]', [$config])->shouldAllowMockingProtectedMethods(); | ||
|
||
$gateway->shouldReceive('request')->andReturn([ | ||
'status' => 'queued', | ||
'from' => 'mock-from', | ||
'to' => '+8618888888888', | ||
'body' => '【twilio】This is a test message.', | ||
'sid' => 'mock-api-account-sid', | ||
'error_code' => null, | ||
]); | ||
|
||
$message = new Message(['content' => '【twilio】This is a test message.']); | ||
$config = new Config($config); | ||
|
||
$this->assertSame([ | ||
'status' => 'queued', | ||
'from' => 'mock-from', | ||
'to' => '+8618888888888', | ||
'body' => '【twilio】This is a test message.', | ||
'sid' => 'mock-api-account-sid', | ||
'error_code' => null, | ||
], $gateway->send('+8618888888888', $message, $config)); | ||
} | ||
} |