byjg/sms-client

A generic and extensible lightweight sms client to publish SMS service providers like Twilio and ByJG.

4.9.0 2024-01-05 23:27 UTC

This package is auto-updated.

Last update: 2024-09-16 15:53:32 UTC


README

Build Status Opensource ByJG GitHub source GitHub license GitHub release

This is a simple client to send SMS using different providers.

Features

  • Low code to send SMS
  • Classes totally decoupled
  • Easy to implement new providers

Usage

Using ProviderFactory

// Register the provider and associate with a scheme
ProviderFactory::registerProvider(TwilioMessagingProvider::class);

// Create a provider
$provider = ProviderFactory::create(new Uri("twilio://$accountSid:$authToken@default"));

// Send a message
$response = $byjg->send("12221234567", (new \ByJG\SmsClient\Message("This is a test message")->withSender("+12223217654"));

// Check if the message was sent
if ($response->isSent()) {
    echo "Message sent";
} else {
    echo "Message not sent";
}

Using ProviderFactory to send a message to multiple providers depending on the country

// Register the provider and associate with a scheme
ProviderFactory::registerProvider(TwilioMessagingProvider::class);
ProviderFactory::registerProvider(ByJGSmsProvider::class);

// Define the provider according to the country prefix
ProviderFactory::registerServices("twilio://accoundId:authToken@default", ["+1"]);
ProviderFactory::registerServices("byjg://username:password@default", ["+55"]);

// Send a message and select the provider according to the country prefix
$response = ProviderFactory::createAndSend("+5521900001234", (new \ByJG\SmsClient\Message("This is a test message")));
var_dump($response);

$response = ProviderFactory::createAndSend("+12221234567", (new \ByJG\SmsClient\Message("This is a test message"))->withSender("+12223217654"));
var_dump($response);

Providers

The providers are the classes responsible to send the text message.

All providers have the following interface:

<?php
interface ProviderInterface
{
    public static function schema();

    public function setUp(Uri $uri);

    public function send($to, Message $envelope): ReturnObject;
}

There is no necessary call the method getConnection() because the method publish() and consume() will call it automatically. Use the method getConnection() only if you need to access the connection directly.

Implemented providers

Install

composer require "byjg/sms-client"

Dependencies

Open source ByJG

Open source ByJG