Skip to content

Instantly share code, notes, and snippets.

@BitBy73Bit
Created April 26, 2018 08:36
Show Gist options
  • Select an option

  • Save BitBy73Bit/08e15591fc56bc315b493c69ed233988 to your computer and use it in GitHub Desktop.

Select an option

Save BitBy73Bit/08e15591fc56bc315b493c69ed233988 to your computer and use it in GitHub Desktop.
<?php
namespace App\Helpers;
use Illuminate\Support\Facades\Log;
use Propaganistas\LaravelPhone\PhoneNumber;
use Illuminate\Support\Facades\Mail;
use App\UserNotification;
use App\Mail\NotificationMail;
use Twilio;
/*
* A custom helper to encapsulate all notificiation engine logic
*/
class Notification
{
public static function sendSms($id, $to, $message) {
Log::debug("Attempting to send a SMS message", ['to' => $to, 'message' => $message]);
// Attempt to send the message
try {
// Make sure the phone number is in the right format
$to = PhoneNumber::make($to, 'GB')->formatE164();
// Send request to twilio
$result = Twilio::message($to, $message);
} catch (\Services_Twilio_RestException $e) {
Log::critical("Sending SMS failed", ['error' => $e->getMessage()]);
abort(500, "Sending the SMS message failed");
}
$date = new \DateTime;
$now = $date->format('Y-m-d H:i:s');
// Store notification in DB
if($id != "TEST") {
$notificaionRecord = new UserNotification;
$notificaionRecord->type = "sms";
$notificaionRecord->recipient = $to;
$notificaionRecord->user_id = $id;
$notificaionRecord->message = $message;
$notificaionRecord->sent_at = $now;
$notificaionRecord->save();
}
return true;
}
public static function sendVoiceCall($id, $to, $message) {
Log::debug("Attempting to send a voice call", ['to' => $to, 'message' => $message]);
// Attempt to send the message
try {
// Make sure the phone number is in the right format
$to = PhoneNumber::make($to, 'GB')->formatE164();
// Send request to twilio
$result = Twilio::call($to, function($call) use(&$message) {
$call->say($message, ['voice' => 'alice', 'language' => 'en-GB', 'loop' => 2]);
});
} catch (\Services_Twilio_RestException $e) {
Log::critical("Sending voice call failed", ['error' => $e->getMessage()]);
abort(500, "Sending the voice call failed");
}
$date = new \DateTime;
$now = $date->format('Y-m-d H:i:s');
// Store notification in DB
if($id != "TEST") {
$notificaionRecord = new UserNotification;
$notificaionRecord->type = "phone";
$notificaionRecord->recipient = $to;
$notificaionRecord->user_id = $id;
$notificaionRecord->message = $message;
$notificaionRecord->sent_at = $now;
$notificaionRecord->save();
}
return true;
}
public static function sendEmail($id, $to, $message) {
Log::debug("Attempting to send an email", ['to' => $to, 'message' => $message]);
Mail::to($to)->send(new NotificationMail($message));
$date = new \DateTime;
$now = $date->format('Y-m-d H:i:s');
// Store notification in DB
if($id != "TEST") {
$notificaionRecord = new UserNotification;
$notificaionRecord->type = "email";
$notificaionRecord->recipient = $to;
$notificaionRecord->user_id = $id;
$notificaionRecord->message = $message;
$notificaionRecord->sent_at = $now;
$notificaionRecord->save();
}
return true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment