Files
YNWA Fawzy b374a27f94 Enhancements:
- bump packages versions.
- add product image and product link to feed.
- Move the rss to notification instead of tempalte
- new product list layout ( still in work)

Fixes:
- fix authentication not required.
- add docker link to README.md
- update discord link
- clear cache when new store is added for a product

In Progress:
- walmart might not work properly as i am still working on it

Took 4 hours 50 minutes
2024-12-04 19:32:15 +04:00

136 lines
4.4 KiB
PHP

<?php
namespace App\Notifications;
use App\Models\RssFeedItem;
use App\NotificationsChannels\AppriseChannel;
use App\NotificationsChannels\NtfyChannel;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;
use NotificationChannels\Telegram\TelegramFile;
class ProductDiscounted extends Notification
{
use Queueable;
public $product_temp_link;
public $notification_title;
public $notification_text;
/**
* Create a new notification instance.
*/
public function __construct(
public $product_id,
public $product_name,
public $store_name ,
public $price,
public $highest_price,
public $lowest_price,
public $product_url,
public $image,
public $currency,
public $tags,
){
$this->product_temp_link= URL::temporarySignedRoute("products.show" , now()->addMinutes(15) , ['product'=>$this->product_id]);
$this->notification_title="For Just $this->price - Discount For " . Str::words($this->product_name , 5);
$this->notification_text="{$this->product_name}, is at {$this->currency}{$this->price} <br>" .
"----------------<br>".
"Highest Price: {$this->highest_price} <br>".
"Lowest Price: {$this->lowest_price} <br>";
RssFeedItem::create([
"data"=> [
'title' =>$this->notification_title,
'summary' => Str::replace("<br>" , "&#xa;" ,$this->notification_text),
'updated' => now()->toDateTimeString(),
'product_id' => $this->product_id,
'image' => $this->image,
'name' => $this->product_name,
'link' => $this->product_url,
'authorName' => "Discount Bandit",
]
]);
}
/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
$channels=[];
if (env('NTFY_CHANNEL_ID'))
$channels[]=NtfyChannel::class;
if (env('APPRISE_URL'))
$channels[]=AppriseChannel::class;
if (env('TELEGRAM_BOT_TOKEN') && env('TELEGRAM_CHANNEL_ID'))
$channels[]='telegram';
return $channels;
}
public function toNtfy(object $notifiable): array {
$extra_headers=[
'Title'=>$this->notification_title,
"Actions"=> [
[
"action" => "view",
"label" => "Open in $this->store_name",
"url" => $this->product_url,
],[
"action" => "view",
"label" => "See Trend",
"url" => $this->product_temp_link,
],
],
"Attach"=>$this->image,
"X-Tags"=>"money_with_wings" . $this->tags
];
return ["content" => $this->notification_text ,"headers"=> $extra_headers ];
}
public function toTelegram($notifiable){
return TelegramFile::create()
->photo($this->image)
->token(env("TELEGRAM_BOT_TOKEN"))
->to(env('TELEGRAM_CHANNEL_ID'))
->content(
"$this->product_name, is at $this->currency $this->price \n " .
"--------------------------\n" .
"Highest Price: $this->highest_price \n" .
"Lowest Price: $this->lowest_price \n".
"--------------------------\n" .
$this->tags
)
->button('View Product', $this->product_url)
->button('View Trend', $this->product_temp_link);
}
public function toApprise(object $notifiable): array {
$content=[
'title'=>"For Just $this->price - Discount For " . Str::words($this->product_name , 5),
'body'=>$this->notification_text .
"----------------<br>
Product URL: . {$this->product_url}",
'attach'=>[$this->image],
'format'=>'html'
];
return ["content" => $content ];
}
}