|
| 1 | +# Extending the Plugin |
| 2 | + |
| 3 | +The SMS Gateway plugin for Botble CMS allows you to extend its functionality by adding custom SMS drivers. This guide explains how to create a custom SMS driver by extending the plugin with your own implementation, including setting up a configuration form. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +To extend the SMS Gateway plugin, you'll need to: |
| 8 | + |
| 9 | +1. **Create a New Driver Class**: Extend the `AbstractDriver` class to define your custom SMS provider. |
| 10 | +2. **Implement Required Methods**: Implement the abstract methods and provide additional configuration and functionality. |
| 11 | +3. **Create a Configuration Form**: Define a form for configuring the SMS provider settings. |
| 12 | +4. **Register Your Custom Driver**: Integrate your custom driver with the SMS Gateway plugin. |
| 13 | + |
| 14 | +## Creating a Custom SMS Driver |
| 15 | + |
| 16 | +### Step 1: Define Your Driver Class |
| 17 | + |
| 18 | +Create a new PHP class that extends the `AbstractDriver` class. This class will implement the specific functionality for your SMS provider. |
| 19 | + |
| 20 | +```php |
| 21 | +<?php |
| 22 | + |
| 23 | +namespace FriendsOfBotble\Sms\Drivers; |
| 24 | + |
| 25 | +use Botble\Base\Forms\FormAbstract; |
| 26 | +use FriendsOfBotble\Sms\DataTransferObjects\SmsResponse; |
| 27 | +use FriendsOfBotble\Sms\Facades\Sms as SmsFacade; |
| 28 | +use FriendsOfBotble\Sms\Forms\NexmoGatewayForm; |
| 29 | +use Vonage\Client; |
| 30 | +use Vonage\Client\Credentials\Basic; |
| 31 | +use Vonage\SMS\Message\SMS; |
| 32 | + |
| 33 | +class Nexmo extends AbstractDriver |
| 34 | +{ |
| 35 | + protected Client $client; |
| 36 | + |
| 37 | + public function __construct() |
| 38 | + { |
| 39 | + $key = SmsFacade::getSetting('key', 'nexmo'); |
| 40 | + $secret = SmsFacade::getSetting('secret', 'nexmo'); |
| 41 | + |
| 42 | + if (empty($key) || empty($secret)) { |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + $this->client = new Client(new Basic($key, $secret)); |
| 47 | + } |
| 48 | + |
| 49 | + protected function performSend(string $to, string $message): SmsResponse |
| 50 | + { |
| 51 | + $response = $this->client->sms()->send( |
| 52 | + new SMS($to, $this->getFrom(), $message) |
| 53 | + ); |
| 54 | + |
| 55 | + $message = $response->current(); |
| 56 | + |
| 57 | + return new SmsResponse( |
| 58 | + success: $message->getStatus() === 0, |
| 59 | + messageId: $message->getMessageId(), |
| 60 | + response: $response->getAllMessagesRaw(), |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + public function getLogo(): string |
| 65 | + { |
| 66 | + return asset('vendor/core/plugins/sms/images/nexmo.png'); |
| 67 | + } |
| 68 | + |
| 69 | + public function getInstructions(): string |
| 70 | + { |
| 71 | + return view('plugins/sms::instructions.nexmo'); |
| 72 | + } |
| 73 | + |
| 74 | + public function getSettingForm(): FormAbstract |
| 75 | + { |
| 76 | + return NexmoGatewayForm::create(); |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +### Step 2: Implement Required Methods |
| 82 | + |
| 83 | +- **`performSend(string $to, string $message): SmsResponse`**: Implement the logic for sending an SMS message. This method should interact with the SMS provider’s API and return an `SmsResponse` object. |
| 84 | +- **`getLogo(): string`**: Provide the URL to the logo image of your SMS provider. This image will be used in the plugin’s UI. |
| 85 | +- **`getInstructions(): string`**: Return a view or string with instructions for configuring the SMS provider. |
| 86 | +- **`getSettingForm(): FormAbstract`**: Return a form for configuring the SMS provider settings. |
| 87 | + |
| 88 | +### Step 3: Create a Configuration Form |
| 89 | + |
| 90 | +Define a form for configuring the settings of your SMS provider. This form will be used in the admin panel to set up the provider. |
| 91 | + |
| 92 | +#### Example: Nexmo Gateway Form |
| 93 | + |
| 94 | +```php |
| 95 | +<?php |
| 96 | + |
| 97 | +namespace FriendsOfBotble\Sms\Forms; |
| 98 | + |
| 99 | +use Botble\Base\Facades\Html; |
| 100 | +use Botble\Base\Forms\FieldOptions\TextFieldOption; |
| 101 | +use Botble\Base\Forms\Fields\TextField; |
| 102 | + |
| 103 | +class NexmoGatewayForm extends SmsGatewayForm |
| 104 | +{ |
| 105 | + protected array $sensitiveFields = [ |
| 106 | + 'key', |
| 107 | + 'secret', |
| 108 | + ]; |
| 109 | + |
| 110 | + public function setup(): void |
| 111 | + { |
| 112 | + parent::setup(); |
| 113 | + |
| 114 | + $this |
| 115 | + ->add( |
| 116 | + 'key', |
| 117 | + TextField::class, |
| 118 | + TextFieldOption::make() |
| 119 | + ->label(trans('plugins/sms::nexmo.key')) |
| 120 | + ->required() |
| 121 | + ) |
| 122 | + ->add( |
| 123 | + 'secret', |
| 124 | + TextField::class, |
| 125 | + TextFieldOption::make() |
| 126 | + ->label(trans('plugins/sms::nexmo.secret')) |
| 127 | + ->required() |
| 128 | + ) |
| 129 | + ->add( |
| 130 | + 'from', |
| 131 | + TextField::class, |
| 132 | + TextFieldOption::make() |
| 133 | + ->label(trans('plugins/sms::nexmo.from')) |
| 134 | + ->helperText(trans('plugins/sms::nexmo.from_help', [ |
| 135 | + 'link' => Html::link( |
| 136 | + 'https://developer.vonage.com/en/messaging/sms/guides/custom-sender-id?source=messaging', |
| 137 | + trans('plugins/sms::nexmo.here'), |
| 138 | + ['target' => '_blank'] |
| 139 | + ), |
| 140 | + ])) |
| 141 | + ->required() |
| 142 | + ); |
| 143 | + } |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +- **`$sensitiveFields`**: Define sensitive fields that should be protected or masked. |
| 148 | +- **`setup()`**: Configure the form fields, including labels, helper text, and validation requirements. |
| 149 | + |
| 150 | +### Step 4: Register Your Custom Driver |
| 151 | + |
| 152 | +To integrate your custom driver with the SMS Gateway plugin, register it in your plugin's service provider. |
| 153 | + |
| 154 | +```php |
| 155 | +// In your plugin’s service provider |
| 156 | +public function boot() |
| 157 | +{ |
| 158 | + Sms::extend('nexmo', Nexmo::class); |
| 159 | +} |
| 160 | +``` |
| 161 | + |
| 162 | +## Example Usage |
| 163 | + |
| 164 | +Once your custom driver is registered, you can use it as follows: |
| 165 | + |
| 166 | +```php |
| 167 | +// Sending an SMS message |
| 168 | +Sms::driver('nexmo')->send('+11234567890', 'Your OTP code is: 123456'); |
| 169 | +``` |
| 170 | + |
| 171 | +## Troubleshooting |
| 172 | + |
| 173 | +- **Check Logs**: Review the SMS logs to ensure messages are sent correctly and diagnose any issues. |
| 174 | +- **Verify Configuration**: Ensure that your API credentials and settings are correctly configured in the admin panel. |
| 175 | +- **Consult Documentation**: Refer to the documentation of your SMS provider for details on API usage and error codes. |
0 commit comments