[callaction url=”https://www.youtube.com/user/JPlaya01″ background_color=”#333333″ text_color=”#ffffff” button_text=”Go Now” button_background_color=”#e64429″]Subscribe To My Youtube Page[/callaction]
Delayed Responses
So now we have Facebook integration in our Laravel app, but there is one problem, the Laravel app sends the SMS alert right away. We want to give the users the option to select when the text message goes out. In order to do this we will implement Laravel queues to achieve this task. We will make a new job and move the TodoCreatedListener code into that new job. Let’s get started.
Enter this command into the terminal to create a new job
php artisan make:job SendText --queued
this scaffolds a new job class for you, open that class and copy the following code
<? php
namespace AppJobs;
use AppJobsJob;
use Mail;
use IlluminateQueueSerializesModels;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsBusSelfHandling;
use IlluminateContractsQueueShouldQueue;
class SendText extends Job implements SelfHandling, ShouldQueue {
use InteractsWithQueue, SerializesModels;
public $text;
public $phone;
/** * Create a new job instance.
* * @return void */
public function __construct($text,$phone) {
//
$this->text = $text;
$this->phone = $phone;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
Mail::raw($this->text, function ($message){
$message->from(env('MAIL_USERNAME','john.smith@email.com'));
$message->to($this->phone);
//var_dump($this->phone); exit();
});
}
}
Notice it’s the code from the TodoCreatedListener with a minor changes. Now you can go to that class and change the code to this:
<? php namespace AppListeners;
use Mail;
use AppEventsTodoCreatedEvent;
use IlluminateQueueInteractsWithQueue;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationBusDispatchesJobs;
use AppJobsSendText;
class TodoCreatedListener implements ShouldQueue {
use DispatchesJobs;
/** * Create the event listener.
* * @return void */
public function __construct() { // }
/** * Handle the event.
* * @param TodoCreatedEvent $event
* @return void */
public function handle(TodoCreatedEvent $event) {
//set todo
$text = $event->todo->content;
$job = (new SendText($text,$event->phone))->delay($event->timer);
$this->dispatch($job);
}
}
Notice the delay($event->timer)? That allows us to programmatically delay the job until the user’s desired time. Head over to the TodoController class and modify the store method to look like this:
/**
* Store a newly created resource in storage.
*
* @param IlluminateHttpRequest $request
* @return IlluminateHttpResponse
*/
public function store(Request $request)
{
//create Todo Model and
$todo = new Todo;
if ($request->session()->has('user')) {
$user = $request->session()->get('user');
//var_dump($user); exit();
$todo->facebook_id = $user[0]['id'];
//var_dump($todo->facebook_id);exit();
}
$todo->content = $request->content;
$todo->save();
//var_dump($todo);exit();
//launch event
$phone = $request->phone . $request->gateway;
//dd($phone);
$timer = $request->timer;
event(new TodoCreatedEvent($todo,$phone,$timer));
//(new SendText($todo->content,$phone))->delay(30);
return redirect('/');
}
That takes care of the backend, don’t forget to run a queue listener to handle the jobs. Now head over to the welcome view and in the forms add these fields (or whatever times you want):
Now
1 Minute
5 minutes
10 minutes
15 Minutes
30 Minutes
1 Hour
12 Hours
24 Hours
To see the live version of the Laravel click here, and don’t forget to check the Github.
This blog was... how do yoou say it? Relevant!! Finally I've found something that heled me.
Cheers!