-
clone this project into your machine
git clone https://github.com/KimelirR/AfricasTalking-laravel-sms.git
-
Install project dependencies
composer install
npm install
-
Create .env file through copy
cp .env.example .env
-
Provide database credentials below in .env file.
DB_DATABASE=? DB_USERNAME=? DB_PASSWORD=?
-
Provide Africatalk credentials below in .env file
AT_USERNAME="YOUR_USERNAME" AT_KEY="YOUR_API_KEY" AT_FROM="FROM_CODE"
-
Run migrations
php artisan migrate
-
Generate key for laravel new application you have installed.
php artisan key:generate && php artisan config:cache
Congratulations you have installed laravel app successfully!
- On terminal split into two
- First one || Start our laravel app
php artisan server
- Second One || Run vite
npm run build
- First one || Start our laravel app
-
On Postman
-
Our post api endpoint [http://127.0.0.1:8000/api/africatalks/sendsms]
-
Set the headers as following
-
Set the body this way...Url_encoded
-
! Lastly send messages. The end
- When Message receivers are more than 1
$object = json_decode(json_encode($result), false);
//We save data from response to ensure
$response_array = $object->data->SMSMessageData->Recipients;
$message = $request->input('message');
$count = count($response_array);
if($count > 1){
#Initialize array to save since multisave to database
$saved = [];
foreach ($response_array as $row) :
$data->to = $row->number;
$data->message = $message;
$data->delivery_status = $row->status;
if($data->delivery_status == "Success"){
$data->delivery_status = 1;
}
else{
$data->delivery_status = 0;
}
$data->created_at =Carbon::now()->format('Y-m-d h:i:s');
$data->updated_at = date('Y-m-d h:i:s');
$saved[] = [
'to'=>$data->to,
'message'=> $data->message,
'delivery_status'=> $data->delivery_status,
'created_at'=>$data->created_at,
'updated_at' => $data->updated_at
];
endforeach;
// SAVE TO DATABASE FOR BACKUP
AfricaTalk::insert($saved);
- When Sending Message to One receiver
$number = $response_array[0]->number;
$status = $response_array[0]->status;
//SAVE TO DATABASE FOR BACKUP
$data->to = $number;
$data->message = $message;
$data->delivery_status = $status;
if($data->delivery_status == "Success"){
$data->delivery_status = 1;
}
else{
$data->delivery_status = 0;
}
$data->save();
Migration defualt 0 which represent 'Failed'
$table->tinyInteger('delivery_status')->default(0);