|
| 1 | +--- |
| 2 | +title: SMS gateway plugin |
| 3 | +tags: |
| 4 | + - SMS |
| 5 | + - Gateway |
| 6 | + - SMS gateway |
| 7 | + - Notification |
| 8 | +--- |
| 9 | + |
| 10 | +<Since version="4.5" issueNumber="MDL-83406" /> |
| 11 | + |
| 12 | +SMS gateway plugins allows you to create SMS gateway provider, which can be used to send SMS notification to users from your Moodle instance. |
| 13 | +For example, you use MFA (Multi-Factor Authentication) to user authentication in Moodle and you use AWS as your SMS gateway provider. You can |
| 14 | +now build more SMS Gateway providers to allow sending SMS to your users. |
| 15 | + |
| 16 | +## File structure |
| 17 | + |
| 18 | +SMS gateway plugins are located in the /sms/gateway directory. A plugin should not include any custom files outside its own |
| 19 | +plugin folder. |
| 20 | + |
| 21 | +Each plugin is in a separate subdirectory and consists of a number of _mandatory files_ and any other files the developer is going to use. |
| 22 | + |
| 23 | +:::important |
| 24 | + |
| 25 | +Some important files are described below. See the [common plugin files](../../commonfiles/index.mdx) documentation for details of other |
| 26 | +files which may be useful in your plugin. |
| 27 | + |
| 28 | +::: |
| 29 | + |
| 30 | +<details> |
| 31 | + <summary>The directory layout for the `smsgateway` plugin.</summary> |
| 32 | + |
| 33 | +```console |
| 34 | +sms/gateway/example |
| 35 | +├── classes |
| 36 | +│ ├── gateway.php |
| 37 | +│ ├── hook_listener.php |
| 38 | +│ └── privacy |
| 39 | +│ └── provider.php |
| 40 | +├── lang |
| 41 | +│ └── en |
| 42 | +│ └── smsgateway_example.php |
| 43 | +├── settings.php |
| 44 | +└── version.php |
| 45 | +``` |
| 46 | + |
| 47 | +</details> |
| 48 | + |
| 49 | +## Key files |
| 50 | + |
| 51 | +There are a number of key files within the plugin, described below. |
| 52 | + |
| 53 | +### gateway.php |
| 54 | +Each plugin must implement this class and should have the exact class name. The core_sms api will pick the extended methods from this class. |
| 55 | + |
| 56 | +```php title="Implementing the base SMS gateway" |
| 57 | + |
| 58 | +class gateway extends \core_sms\gateway { |
| 59 | + |
| 60 | + #[\Override] |
| 61 | + public function send( |
| 62 | + message $message, |
| 63 | + ): message { |
| 64 | + // Sample code to send an SMS message. |
| 65 | + $config = (object)json_decode($awsconfig, true, 512, JSON_THROW_ON_ERROR); |
| 66 | + $class = '\smsgateway_aws\local\service\\' . $config->gateway; |
| 67 | + $recipientnumber = manager::format_number( |
| 68 | + phonenumber: $message->recipientnumber, |
| 69 | + countrycode: isset($config->countrycode) ?? null, |
| 70 | + ); |
| 71 | + if (class_exists($class)) { |
| 72 | + $status = call_user_func( |
| 73 | + $class . '::send_sms_message', |
| 74 | + $message->content, |
| 75 | + $recipientnumber, |
| 76 | + $config, |
| 77 | + ); |
| 78 | + } |
| 79 | + return $message->with( |
| 80 | + status: $status, |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + #[\Override] |
| 85 | + public function get_send_priority(message $message): int { |
| 86 | + return 50; |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +``` |
| 91 | +### hook_listener.php |
| 92 | +There a couple of hooks dispatched from the core_sms API which can be listened by the plugin. It is necessary for plugins developers to assess |
| 93 | +these hooks and implement accordingly. |
| 94 | + |
| 95 | +#### after_sms_gateway_form_hook |
| 96 | +This hook will allow plugins to add their relevant form field from the plugin to allow users to add required configs for the SMS gateway. |
| 97 | + |
| 98 | + |
| 99 | +```php title="Listener method for after_sms_gateway_form_hook" |
| 100 | + |
| 101 | +public static function set_form_definition_for_aws_sms_gateway(after_sms_gateway_form_hook $hook): void { |
| 102 | + if ($hook->plugin !== 'smsgateway_example') { |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + $gateways = [ |
| 107 | + 'smsgateway_example' => get_string('list', 'smsgateway_example'), |
| 108 | + ]; |
| 109 | + $mform->addElement( |
| 110 | + 'select', |
| 111 | + 'gateway', |
| 112 | + get_string('gateway', 'smsgateway_example'), |
| 113 | + $gateways, |
| 114 | + ); |
| 115 | +} |
| 116 | + |
| 117 | +``` |
| 118 | +:::info |
| 119 | + |
| 120 | +For a real plugin example, please look at the [AWS SMS Gateway plugin](https://github.com/moodle/moodle/tree/main/sms/gateway/aws). |
| 121 | + |
| 122 | +::: |
0 commit comments