Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

integrate SMS API #395

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/Http/Controllers/FormationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Http\Requests\FormationRequest;
use App\Models\Formation;
use App\Models\Participant;
use App\Providers\ParticipantAdded;
use Illuminate\Http\Request;

class FormationController extends Controller
Expand Down Expand Up @@ -67,6 +68,9 @@ public function store(FormationRequest $request)
}
$formation->participants()->saveMany($participants->all());
$formation->refresh();
foreach ($formation->participants as $participant) {
ParticipantAdded::dispatch($participant->participant_tele);
}
}
$result = $formation;
$status = 200;
Expand Down
17 changes: 17 additions & 0 deletions app/Models/SMSTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class SMSTemplate extends Model
{
use HasFactory;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'sms_template';
}
3 changes: 3 additions & 0 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class EventServiceProvider extends ServiceProvider
Registered::class => [
SendEmailVerificationNotification::class,
],
ParticipantAdded::class => [
SendSMSNotification::class,
],
];

/**
Expand Down
37 changes: 37 additions & 0 deletions app/Providers/ParticipantAdded.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Providers;

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ParticipantAdded
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public $participant_num;
/**
* Create a new event instance.
*
* @return void
*/
public function __construct($participant_num)
{
$this->participant_num = $participant_num;
}

/**
* Get the channels the event should broadcast on.
*
* @return \Illuminate\Broadcasting\Channel|array
*/
public function broadcastOn()
{
return new PrivateChannel('channel-name');
}
}
49 changes: 49 additions & 0 deletions app/Providers/SendSMSNotification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Providers;

use App\Models\SMSTemplate;
use App\Providers\ParticipantAdded;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class SendSMSNotification
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Handle the event.
*
* @param \App\Providers\ParticipantAdded $event
* @return void
*/
public function handle(ParticipantAdded $event)
{
$sms_template = SMSTemplate::find(1);
$message = $sms_template->message ?? 'SMS est envoyé';
$url = 'https://ag2m.ma/api.php';
$data = [
'user' => 'stopdrogue',
'password' => 'sToPdr0gUE2023',
'gsm' => $event->participant_num,
'sms' => $message,
];
$options = [
'http' => [
'header' => 'Content-Type: application/x-www-form-urlencoded\r\n',
'method' => 'POST',
'content' => http_build_query($data),
],
];
$context = stream_context_create($options);
file_get_contents($url, false, $context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateSmsTemplateTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('sms_template', function (Blueprint $table) {
$table->id();
$table->string('message');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('sms_template');
}
}
1 change: 1 addition & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function run()
ZoneSeeder::class,
ProgrammeSeeder::class,
PlaceSeeder::class,
SMSTemplateSeeder::class,
]
);
}
Expand Down
25 changes: 25 additions & 0 deletions database/seeders/SMSTemplateSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class SMSTemplateSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('sms_template')->insert(
[
[
'message' => 'Vous êtes un participant.',
],
]
);
}
}