Skip to content

Commit

Permalink
添加twilio支持 (overtrue#72)
Browse files Browse the repository at this point in the history
* 添加twilio gateway

* readme添加内容

* 添加常规错误检查

* 测试

* 移除不需要的引用
  • Loading branch information
tradzero authored and overtrue committed Jan 3, 2018
1 parent c095c62 commit 01c0974
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,19 @@ $easySms->send(13188888888, $message);
]
```

### [twilio](https://www.twilio.com)

短信使用 `content`
发送对象需要 使用`+`添加区号

```php
'twilio' => [
'account_sid' => '', // sid
'from' => '', // 发送的号码 可以在控制台购买
'token' => '', // apitoken
],
```

## License

MIT
69 changes: 69 additions & 0 deletions src/Gateways/TwilioGateway.php
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);
}
}
56 changes: 56 additions & 0 deletions tests/Gateways/TwilioGatewayTest.php
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));
}
}

0 comments on commit 01c0974

Please sign in to comment.