Skip to content

Instantly share code, notes, and snippets.

@alexivenkov
Last active November 16, 2015 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexivenkov/53753023e2baa204d712 to your computer and use it in GitHub Desktop.
Save alexivenkov/53753023e2baa204d712 to your computer and use it in GitHub Desktop.
new SMS drivers
<?php
namespace SimpleSoftwareIO\SMS\Drivers;
use GuzzleHttp\Client;
use SimpleSoftwareIO\SMS\OutgoingMessage;
class EPochtaSMS extends AbstractSMS implements DriverInterface
{
const API_VERSION = '3.0';
const DEFAULT_CURRENCY = 'RUR';
/** @var string $apiBase */
protected $apiBase = 'http://atompark.com/api/sms';
/** @var Client $client */
protected $client;
/** @var string $publicKey */
protected $publicKey;
/** @var string $privateKey */
protected $privateKey;
public function __construct(Client $client, $publicKey, $privateKey)
{
$this->apiBase .= '/'.self::API_VERSION;
$this->client = $client;
$this->publicKey = $publicKey;
$this->privateKey = $privateKey;
}
/**
* Apply public key fro each request
*
* @param array|string $values
* @param null $key
*/
public function buildBody($values, $key = null)
{
$values['key'] = $this->publicKey;
parent::buildBody($values, $key);
}
/**
* Calculate control sum for each request.
*
* @param array $params
* @param string $methodName
* @return string
*/
protected function getControlSum($params, $methodName)
{
$params['version'] = self::API_VERSION;
$params['action'] = $methodName;
$params['key'] = $this->publicKey;
ksort($params);
$sum = '';
foreach($params as $param) {
$sum .= $param;
}
$sum .= $this->privateKey;
return md5($sum);
}
/**
* Return account balance in json format
*
* @param string $currency
* @return string
*/
protected function getBalance($currency)
{
$data = [
'currency' => $currency,
];
$data['sum'] = $this->getControlSum($data, 'getUserBalance');
$this->buildCall('/getUserBalance');
$this->buildBody($data);
$response = $this->postRequest();
return (string)$response->getBody();
}
/**
* Process sms sending
*
* @param OutgoingMessage $message
* @return \GuzzleHttp\Psr7\Response
*/
public function send(OutgoingMessage $message)
{
$phone = $message->getTo();
$phone = array_pop($phone);
$messageText = $message->composeMessage();
$messageData = $message->getData();
unset($messageData['message']);
$data = [
'text' => $messageText,
'phone' => $phone
];
$data = array_merge($data, $messageData);
$data['sum'] = $this->getControlSum($data, 'sendSMS');
$this->buildCall('/sendSMS');
$this->buildBody($data);
return $this->postRequest();
}
public function checkMessages(Array $options = array())
{
throw new \RuntimeException('EpochtaSMS check message is not supported.');
}
public function getMessage($messageId)
{
throw new \RuntimeException('EpochtaSMS get message is not supported.');
}
public function receive($raw)
{
throw new \RuntimeException('EpochtaSMS receive is not supported.');
}
protected function processReceive($rawMessage)
{
throw new \RuntimeException('EpochtaSMS processReceive is not supported.');
}
}
<?php
namespace SimpleSoftwareIO\SMS\Drivers;
use SimpleSoftwareIO\SMS\OutgoingMessage;
use GuzzleHttp\Client;
class IqSMS extends AbstractSMS implements DriverInterface
{
protected $apiBase = 'http://gate.iqsms.ru';
protected $balanceUrl = 'http://api.iqsms.ru/messages/v2/balance/';
/**
* @var Client $client
*/
protected $client;
public function __construct(Client $client, Array $auth)
{
$this->client = $client;
$this->auth = $auth;
}
/**
* Returns balance in string representation
*
* @return string "RUB;15.0;0.0"
*/
protected function getBalance()
{
$this->apiBase = $this->balanceUrl;
$response = $this->postRequest();
return (string) $response->getBody();
}
/**
* Return available senders. One sender per row.
*
* @return string
*/
protected function getAvailableSenders()
{
$this->buildCall('/senders');
$response = $this->postRequest();
return (string) $response->getBody();
}
/**
* Process sms sending
*
* @param OutgoingMessage $message
* @return \GuzzleHttp\Psr7\Response
*/
public function send(OutgoingMessage $message)
{
$phone = $message->getTo();
$phone = array_pop($phone);
$messageText = $message->composeMessage();
$data = [
'phone' => $phone,
'text' => $messageText,
];
$messageData = $message->getData();
unset($messageData['message']);
$this->buildCall('/send');
$this->buildBody(array_merge($data, $messageData));
return $this->postRequest();
}
/**
* Return array of messages statuses by array of messages ids
*
* @param array $options
* @return array ['123456' => 'delivered']
*/
public function checkMessages(Array $options = array())
{
$this->buildCall('/status');
$statusCache = [];
foreach ($options as $messageId) {
$this->buildBody(['id' => $messageId]);
$response = $this->postRequest();
$result = explode('=', (string)$response->getBody());
$statusCache[$result[0]] = $result[1];
}
return $statusCache;
}
public function getMessage($messageId)
{
throw new \RuntimeException('IqSMS get message is not supported.');
}
public function receive($raw)
{
throw new \RuntimeException('IqSMS push messages is not supported.');
}
protected function processReceive($rawMessage)
{
throw new \RuntimeException('IqSMS push messages is not supported.');
}
}
<?php namespace SimpleSoftwareIO\SMS;
/**
* Simple-SMS
* Simple-SMS is a package made for Laravel to send/receive (polling/pushing) text messages.
*
* @link http://www.simplesoftware.io
* @author SimpleSoftware support@simplesoftware.io
*
*/
use GuzzleHttp\Client;
use Illuminate\Support\ServiceProvider;
use SimpleSoftwareIO\SMS\Drivers\CallFireSMS;
use SimpleSoftwareIO\SMS\Drivers\EmailSMS;
use SimpleSoftwareIO\SMS\Drivers\EPochtaSMS;
use SimpleSoftwareIO\SMS\Drivers\EZTextingSMS;
use SimpleSoftwareIO\SMS\Drivers\MozeoSMS;
use SimpleSoftwareIO\SMS\Drivers\TwilioSMS;
use SmartCrowd\Sms\Drivers\IqSMS;
class SMSServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;
/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$this->publishes([
__DIR__.'/../../config/sms.php' => config_path('sms.php'),
]);
}
/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app->bind('sms', function ($app) {
$sender = $this->registerSender();
$sms = new SMS($sender);
$sms->setContainer($app);
$sms->setLogger($app['log']);
$sms->setQueue($app['queue']);
//Set the from and pretending settings
if ($from = config('sms.from', false)) $sms->alwaysFrom($from);
$sms->setPretending(config('sms.pretend', false));
return $sms;
});
}
/**
* Register the correct driver based on the config file.
*
* @return CallFireSMS|EmailSMS|EZTextingSMS|MozeoSMS|TwilioSMS
* @throws \InvalidArgumentException
*/
public function registerSender()
{
$driver = config('sms.driver');
switch ($driver) {
case 'email':
return new EmailSMS($this->app['mailer']);
case 'twilio':
return $this->buildTwilio();
case 'eztexting':
return $this->buildEZTexting();
case 'callfire':
return $this->buildCallFire();
case 'mozeo':
return $this->buildMozeo();
case 'iqsms' :
return $this->buildIqSms();
case 'epochta' :
return $this->buildEPochta();
default:
throw new \InvalidArgumentException('Invalid SMS driver.');
}
}
protected function buildTwilio()
{
return new TwilioSMS(
new \Services_Twilio(
config('sms.twilio.account_sid'),
config('sms.twilio.auth_token')
),
config('sms.twilio.auth_token'),
$this->app['request']->url(),
config('sms.twilio.verify')
);
}
protected function buildEZTexting()
{
$provider = new EZTextingSMS(new Client);
$data = [
'User' => config('sms.eztexting.username'),
'Password' => config('sms.eztexting.password')
];
$provider->buildBody($data);
return $provider;
}
protected function buildCallFire()
{
$provider = new CallFireSMS(new Client);
$provider->setUser(config('sms.callfire.app_login'));
$provider->setPassword(config('sms.callfire.app_password'));
return $provider;
}
protected function buildMozeo()
{
$provider = new MozeoSMS(new Client);
$auth = [
'companykey' => config('sms.mozeo.companyKey'),
'username' => config('sms.mozeo.username'),
'password' => config('sms.mozeo.password'),
];
$provider->buildBody($auth);
return $provider;
}
protected function buildIqSms()
{
$auth = [
'username' => config('sms.iqsms.login'),
'password' => config('sms.iqsms.password'),
];
$provider = new IqSMS(new Client, $auth);
return $provider;
}
protected function buildEPochta()
{
$provider = new EPochtaSMS(new Client, config('sms.epochta.publicKey'), config('sms.epochta.privateKey'));
return $provider;
}
/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array('sms', 'emailsms', 'twiliosms', 'mozeosms', 'eztextingsms', 'callfiresms', 'iqsms', 'epochta');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment