Repository navigation
Expand file tree
/
Copy pathlaravel_example.php
More file actions
276 lines (237 loc) · 7.64 KB
/
Copy pathlaravel_example.php
File metadata and controls
276 lines (237 loc) · 7.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/**
* Laravel Integration Example
*
* This example shows how to integrate the Mobile Message SDK with Laravel.
* Follow these steps to set up the integration:
*
* 1. Install the SDK: composer require mobilemessage/php-sdk
* 2. Add configuration to config/services.php
* 3. Create a service provider (optional)
* 4. Use in controllers, jobs, or other Laravel components
*/
// Step 1: Add to config/services.php
/*
'mobile_message' => [
'username' => env('MOBILE_MESSAGE_USERNAME'),
'password' => env('MOBILE_MESSAGE_PASSWORD'),
],
*/
// Step 2: Add to .env file
/*
MOBILE_MESSAGE_USERNAME=your_username
MOBILE_MESSAGE_PASSWORD=your_password
*/
// Step 3: Create a Service Provider (app/Providers/MobileMessageServiceProvider.php)
/*
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use MobileMessage\MobileMessageClient;
class MobileMessageServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->singleton(MobileMessageClient::class, function ($app) {
return new MobileMessageClient(
config('services.mobile_message.username'),
config('services.mobile_message.password')
);
});
}
}
*/
// Step 4: Register the provider in config/app.php
/*
'providers' => [
// ... other providers
App\Providers\MobileMessageServiceProvider::class,
],
*/
// Step 5: Example Controller
/*
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use MobileMessage\MobileMessageClient;
use MobileMessage\DataObjects\Message;
use MobileMessage\Exceptions\MobileMessageException;
class SmsController extends Controller
{
public function __construct(private MobileMessageClient $smsClient)
{
}
public function sendWelcome(Request $request)
{
$request->validate([
'phone' => 'required|string',
'name' => 'required|string',
]);
try {
$response = $this->smsClient->sendMessage(
$request->phone,
"Welcome to our app, {$request->name}! Your account is now active.",
'YourApp',
'welcome-' . auth()->id()
);
if ($response->isSuccess()) {
return response()->json([
'success' => true,
'message' => 'Welcome SMS sent successfully',
'message_id' => $response->getMessageId(),
'cost' => $response->getCost(),
]);
} else {
return response()->json([
'success' => false,
'message' => 'Failed to send SMS',
'status' => $response->getStatus(),
], 400);
}
} catch (MobileMessageException $e) {
return response()->json([
'success' => false,
'message' => 'SMS service error: ' . $e->getMessage(),
], 500);
}
}
public function sendBulkNotification(Request $request)
{
$request->validate([
'message' => 'required|string',
'phones' => 'required|array',
'phones.*' => 'required|string',
]);
$messages = collect($request->phones)->map(function ($phone, $index) use ($request) {
return new Message(
$phone,
$request->message,
'YourApp',
'bulk-' . time() . '-' . $index
);
})->toArray();
try {
$responses = $this->smsClient->sendMessages($messages);
$successful = collect($responses)->filter(fn($r) => $r->isSuccess())->count();
$failed = count($responses) - $successful;
$totalCost = collect($responses)->sum(fn($r) => $r->getCost());
return response()->json([
'success' => true,
'total' => count($responses),
'successful' => $successful,
'failed' => $failed,
'total_cost' => $totalCost,
'results' => $responses,
]);
} catch (MobileMessageException $e) {
return response()->json([
'success' => false,
'message' => 'Bulk SMS error: ' . $e->getMessage(),
], 500);
}
}
public function checkBalance()
{
try {
$balance = $this->smsClient->getBalance();
return response()->json([
'balance' => $balance->getBalance(),
'plan' => $balance->getPlan(),
'has_credits' => $balance->hasCredits(),
]);
} catch (MobileMessageException $e) {
return response()->json([
'error' => 'Failed to check balance: ' . $e->getMessage(),
], 500);
}
}
}
*/
// Step 6: Example Job for Background Processing
/*
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use MobileMessage\MobileMessageClient;
use MobileMessage\Exceptions\MobileMessageException;
class SendSmsJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct(
private string $phone,
private string $message,
private string $sender,
private ?string $customRef = null
) {
}
public function handle(MobileMessageClient $smsClient)
{
try {
$response = $smsClient->sendMessage(
$this->phone,
$this->message,
$this->sender,
$this->customRef
);
if ($response->isSuccess()) {
\Log::info('SMS sent successfully', [
'phone' => $this->phone,
'message_id' => $response->getMessageId(),
'cost' => $response->getCost(),
]);
} else {
\Log::error('SMS failed to send', [
'phone' => $this->phone,
'status' => $response->getStatus(),
]);
}
} catch (MobileMessageException $e) {
\Log::error('SMS service error', [
'phone' => $this->phone,
'error' => $e->getMessage(),
]);
throw $e; // Re-throw to trigger job retry
}
}
}
*/
// Step 7: Example Routes (routes/web.php or routes/api.php)
/*
Route::post('/sms/welcome', [SmsController::class, 'sendWelcome']);
Route::post('/sms/bulk', [SmsController::class, 'sendBulkNotification']);
Route::get('/sms/balance', [SmsController::class, 'checkBalance']);
*/
// Step 8: Example Notification (using Laravel Notifications)
/*
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use MobileMessage\MobileMessageClient;
class SmsNotification extends Notification implements ShouldQueue
{
use Queueable;
public function __construct(private string $message, private string $sender = 'YourApp')
{
}
public function via($notifiable)
{
return ['sms'];
}
public function toSms($notifiable)
{
return [
'phone' => $notifiable->phone,
'message' => $this->message,
'sender' => $this->sender,
];
}
}
*/
echo "This is a Laravel integration example file.\n";
echo "Please follow the commented code above to integrate with your Laravel application.\n";