-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbulk_example.php
More file actions
178 lines (150 loc) Β· 6.58 KB
/
bulk_example.php
File metadata and controls
178 lines (150 loc) Β· 6.58 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
<?php
require_once __DIR__ . '/../vendor/autoload.php';
require_once __DIR__ . '/helpers.php';
use MobileMessage\MobileMessageClient;
use MobileMessage\DataObjects\Message;
use MobileMessage\Exceptions\MobileMessageException;
/**
* Bulk Example - Sending Multiple SMS Messages
*
* This example shows how to send multiple SMS messages efficiently using the Mobile Message PHP SDK.
*
* Setup:
* 1. Copy .env.example to .env in the project root
* 2. Add your Mobile Message API credentials
* 3. Set ENABLE_REAL_SMS_TESTS=true to send actual messages
* 4. Run: php examples/bulk_example.php
*/
try {
echo "π¬ Mobile Message PHP SDK - Bulk Example\n";
echo "========================================\n\n";
// Load credentials from .env
loadEnv(__DIR__ . '/../.env');
$username = $_ENV['API_USERNAME'] ?? null;
$password = $_ENV['API_PASSWORD'] ?? null;
$testPhone = $_ENV['TEST_PHONE_NUMBER'] ?? '0400322583';
$senderId = $_ENV['SENDER_PHONE_NUMBER'] ?? null;
$enableBulkSmsTests = filter_var($_ENV['ENABLE_BULK_SMS_TESTS'] ?? 'false', FILTER_VALIDATE_BOOLEAN);
if (!$username || !$password || $username === 'your_api_username_here') {
throw new Exception('Please configure your API credentials in the .env file');
}
if (!$senderId) {
throw new Exception('Please configure your SENDER_PHONE_NUMBER in the .env file');
}
// Initialise the client
echo "π§ Initialising client...\n";
$client = new MobileMessageClient($username, $password);
// Check account balance
echo "π° Checking account balance...\n";
$balance = $client->getBalance();
echo " Balance: {$balance->getBalance()} credits\n";
echo " Plan: {$balance->getPlan()}\n\n";
// Check if real SMS is enabled
$enableRealSms = filter_var($_ENV['ENABLE_REAL_SMS_TESTS'] ?? 'false', FILTER_VALIDATE_BOOLEAN);
if (!$enableRealSms) {
echo "β οΈ Real SMS sending is disabled.\n";
echo " Set ENABLE_REAL_SMS_TESTS=true in .env to send actual SMS messages.\n";
echo " This example will only validate your setup without sending messages.\n\n";
} elseif (!$enableBulkSmsTests) {
echo "β οΈ Bulk SMS testing is disabled.\n";
echo " Set ENABLE_BULK_SMS_TESTS=true in .env to send bulk SMS messages.\n";
echo " This will send multiple SMS messages and use more credits.\n\n";
// Show what would be sent
echo "π Messages that would be sent:\n";
$timestamp = time();
$sampleMessages = [
new Message($testPhone, "Welcome to our service! Your account is now active.", $senderId, "welcome-{$timestamp}"),
new Message($testPhone, "Reminder: Your appointment is tomorrow at 2 PM.", $senderId, "reminder-{$timestamp}"),
new Message($testPhone, "Thank you for your purchase! Order #12345 is confirmed.", $senderId, "order-{$timestamp}"),
new Message($testPhone, "Your verification code is: 123456", $senderId, "verify-{$timestamp}"),
];
foreach ($sampleMessages as $index => $message) {
$num = $index + 1;
echo " {$num}. To: {$message->getTo()}\n";
echo " Message: {$message->getMessage()}\n";
echo " From: {$message->getSender()}\n";
echo " Ref: {$message->getCustomRef()}\n\n";
}
echo "β
Bulk example setup validated!\n";
exit(0);
}
// Create multiple messages
echo "π Preparing bulk messages...\n";
$timestamp = time();
$messages = [
new Message(
$testPhone,
"Bulk message 1: Welcome to our service! Sent at " . date('H:i:s'),
$senderId,
"bulk-welcome-{$timestamp}"
),
new Message(
$testPhone,
"Bulk message 2: Your account is now active. Sent at " . date('H:i:s'),
$senderId,
"bulk-active-{$timestamp}"
),
new Message(
$testPhone,
"Bulk message 3: Thank you for joining us! Sent at " . date('H:i:s'),
$senderId,
"bulk-thanks-{$timestamp}"
),
new Message(
$testPhone,
"Bulk message 4: This is your final test message. Sent at " . date('H:i:s'),
$senderId,
"bulk-final-{$timestamp}"
),
];
echo " π Prepared " . count($messages) . " messages\n\n";
// Send all messages at once
echo "π¬ Sending bulk messages...\n";
echo "β οΈ This will send " . count($messages) . " SMS messages to {$testPhone}\n";
$responses = $client->sendMessages($messages);
// Process responses
echo "β
Bulk send completed! Processing results...\n\n";
$totalCost = 0;
$successCount = 0;
foreach ($responses as $index => $response) {
$messageNum = $index + 1;
echo "π¨ Message {$messageNum}:\n";
if ($response->isSuccess()) {
$successCount++;
$totalCost += $response->getCost();
echo " β
Status: Sent successfully\n";
echo " π¨ ID: {$response->getMessageId()}\n";
echo " π° Cost: {$response->getCost()}\n";
echo " π± To: {$response->getTo()}\n";
echo " π·οΈ Ref: {$response->getCustomRef()}\n";
} else {
echo " β Status: Failed to send\n";
echo " π± To: {$response->getTo()}\n";
}
echo "\n";
}
// Summary
echo "π Bulk Send Summary:\n";
echo "====================\n";
echo "π€ Total messages: " . count($messages) . "\n";
echo "β
Successful: {$successCount}\n";
echo "β Failed: " . (count($messages) - $successCount) . "\n";
echo "π° Total cost: {$totalCost} credits\n\n";
// Optional: Check status of first message
if ($successCount > 0 && $responses[0]->isSuccess()) {
echo "π Checking status of first message...\n";
sleep(2); // Wait for processing
$status = $client->getMessage($responses[0]->getMessageId());
echo " π Status: {$status->getStatus()}\n";
echo " π
Sent: {$status->getSentAt()}\n";
if ($status->getDeliveredAt()) {
echo " β
Delivered: {$status->getDeliveredAt()}\n";
}
}
echo "\nπ Bulk example completed!\n";
} catch (MobileMessageException $e) {
echo "β Mobile Message API Error: " . $e->getMessage() . "\n";
echo "π§ Please check your API credentials and try again.\n";
} catch (Exception $e) {
echo "β Error: " . $e->getMessage() . "\n";
}