|
| 1 | +--- |
| 2 | +title: SMS API |
| 3 | +--- |
| 4 | + |
| 5 | +<Since version="4.5" issueNumber="MDL-79808" /> |
| 6 | + |
| 7 | +The SMS API lets you send SMS messages using configured gateways, fetch messages that were previously sent, and check on their status. |
| 8 | + |
| 9 | +## Sending an SMS |
| 10 | + |
| 11 | +Messages can be sent using the `send()` method of the SMS Manager class, which should be fetched using Dependency Injection, for example: |
| 12 | + |
| 13 | +```php title="Sending a message" |
| 14 | +$message = \core\di::get(\core_sms\manager::class) |
| 15 | + ->send( |
| 16 | + recipientnumber: '+61987654321', |
| 17 | + content: 'This is the content of the message', |
| 18 | + component: 'mod_example', |
| 19 | + messagetype: 'demonstrationmessage', |
| 20 | + recipientuserid: $user->id, |
| 21 | + issensitive: false, |
| 22 | + async: false, |
| 23 | + ); |
| 24 | +``` |
| 25 | + |
| 26 | +:::info Message lengths |
| 27 | + |
| 28 | +A single SMS sent by the API may consist of up to 480 UTF-8 characters. It is up to the message _gateway_ plugin to determine how this message is sent to the recipient. |
| 29 | + |
| 30 | +Any message longer than the maximum length will be immediately rejected. |
| 31 | + |
| 32 | +::: |
| 33 | + |
| 34 | +### Sending messages containing sensitive information |
| 35 | + |
| 36 | +When sending a message containing something like a 2FA login token, you should make use of the `issensitive` flag. |
| 37 | + |
| 38 | +Passing this flag prevents the SMS subsystem from storing the content of the message in the message log. |
| 39 | + |
| 40 | +The `send()` method return an instance of `\core_sms\message` which can be used to check on the message status. |
| 41 | + |
| 42 | +## Fetching messages |
| 43 | + |
| 44 | +Every sent message is stored in the database for subsequent reporting, and to check statuses. |
| 45 | + |
| 46 | +Messages can be fetched from the database by calling the `\core_sms\manager::get_message()` and `\core_sms\manager::get_messages()` methods and supplying a filter. |
| 47 | + |
| 48 | +```php title="Fetching messages" |
| 49 | +$message = \core\di::get(\core_sms\manager::class) |
| 50 | + ->get_message(['id' => $id]); |
| 51 | + |
| 52 | +$messages = \core\di::get(\core_sms\manager::class) |
| 53 | + ->get_messages(['recipientuserid' => $userid]); |
| 54 | +``` |
| 55 | + |
| 56 | +:::note Sensitive content |
| 57 | + |
| 58 | +If the message was sent with the `issensitive` flag the message body will not be stored. |
| 59 | + |
| 60 | +::: |
| 61 | + |
| 62 | +## Checking the status of a message |
| 63 | + |
| 64 | +Once a message is sent, a status is recorded against it. This can be used to determine whether the message was sent successfully. |
| 65 | + |
| 66 | +:::info |
| 67 | + |
| 68 | +The level of status information available will depend on individual message gateways and recipient regions. In some regions delivery status may be available, but not in others. |
| 69 | + |
| 70 | +::: |
| 71 | + |
| 72 | +Message status can be checked using the `\core_sms\message::$status` property. |
| 73 | + |
| 74 | +Statuses are represented by a PHP Enum object with each status having a translatable description, and methods to determine whether the message was sent, is failed, or still in-progress. |
| 75 | + |
| 76 | +```php title="Checking the status of a message" |
| 77 | +$message = \core\di::get(\core_sms\manager::class) |
| 78 | + ->get_message(['id' => $id]); |
| 79 | + |
| 80 | +// Check if the message is failed. |
| 81 | +$message->is_failed(); |
| 82 | + |
| 83 | +// Check if the message is still in transit. |
| 84 | +$message->is_in_progress(); |
| 85 | + |
| 86 | +// Check if the message is sent. |
| 87 | +$message->is_sent(); |
| 88 | + |
| 89 | +// Get the description of the state. |
| 90 | +$message->description(); |
| 91 | +``` |
| 92 | + |
| 93 | +```mermaid |
| 94 | +graph TD |
| 95 | +
|
| 96 | + classDef failed fill:#f00,color:white,font-weight:bold,stroke:yellow |
| 97 | + classDef inprogress fill:orange |
| 98 | + classDef success fill:green,color:white,font-weight:bold |
| 99 | +
|
| 100 | + unknown["UNKNOWN"] |
| 101 | + MOS["MESSAGE_OVER_SIZE"]:::failed |
| 102 | + GNA["GATEWAY_NOT_AVAILABLE"]:::failed |
| 103 | + GQ["GATEWAY_QUEUED"]:::inprogress |
| 104 | + GS["GATEWAY_SENT"]:::success |
| 105 | + GF["GATEWAY_FAILED"]:::failed |
| 106 | + GR["GATEWAY_REJECTED"]:::failed |
| 107 | + MLC{Message length check} |
| 108 | + GWSEL{Gateway selection} |
| 109 | +
|
| 110 | + direction TB |
| 111 | + start[/Start/] --> |Initial state| unknown |
| 112 | + unknown --> |Initial message checks| MLC |
| 113 | + MLC --> |Message over length| MOS |
| 114 | + MLC --> |Message within limits| GWSEL |
| 115 | +
|
| 116 | + GWSEL --> |No gateway available to send this message| GNA |
| 117 | + GWSEL --> |Message passed to Gateway| Gateway |
| 118 | +
|
| 119 | + subgraph Gateway |
| 120 | + GQ --> |The gateway rejected the message| GR |
| 121 | + GQ --> |Sent to recipient by Gateway| GS |
| 122 | + GQ --> |Gateway failed to send the message| GF |
| 123 | + end |
| 124 | +``` |
0 commit comments