Skip to content

Commit

Permalink
fix(food-party): rare case that tg could not send image
Browse files Browse the repository at this point in the history
  • Loading branch information
ahbanavi committed Aug 23, 2024
1 parent d3eba98 commit bcb0b65
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 15 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ FOOD_PARTY_SCHEDULE="*/15 * * * *"
MARKET_PARTY_SCHEDULE="*/15 * * * *"
DEFAULT_LATITUDE=36.32112700482277
DEFAULT_LONGITUDE=59.53740119934083
DEFAULT_IMAGE=https://raw.githubusercontent.com/ahbanavi/goshne/main/resource/default.jpg

APP_NAME="GoshnePrro"
APP_ENV=local
Expand Down
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,18 @@ docker compose exec app php artisan migrate

Besides the default Laravel environment variables, you can set the following variables specifically for Goshne Prro:

| Var | Description | Required | Default |
|---------------------------|----------------------------------------------------------------|----------|-----------------------------|
| TELEGRAM_BOT_TOKEN | Telegram bot token | **Yes** | - |
| TELEGRAM_BOT_BASE_URI | Base URI (Bridge) for the Telegram Bot API | No | https://api.telegram.org |
| MARKET_PARTY_PRODUCTS_TTL | Time-to-live (TTL) for market party products caches in seconds | No | 900 |
| MARKET_PARTY_NOTIFY_TTL | TTL for market party notifications cache in seconds | No | 43200 |
| FOOD_PARTY_NOTIFY_TTL | TTL for food party notifications cache in seconds | No | 43200 |
| FOOD_PARTY_SCHEDULE | Cron schedule for food party runs | No | "*/15 * * * *" |
| MARKET_PARTY_SCHEDULE | Cron schedule for market party runs | No | "*/15 * * * *" |
| DEFAULT_LATITUDE | Default latitude for UI map | No | 36.32112700482277 (Mashhad) |
| DEFAULT_LONGITUDE | Default longitude for UI map | No | 59.53740119934083 (Mashhad) |
| Var | Description | Required | Default |
|---------------------------|----------------------------------------------------------------|----------|--------------------------------------------------------------------------------------|
| TELEGRAM_BOT_TOKEN | Telegram bot token | **Yes** | - |
| TELEGRAM_BOT_BASE_URI | Base URI (Bridge) for the Telegram Bot API | No | https://api.telegram.org |
| MARKET_PARTY_PRODUCTS_TTL | Time-to-live (TTL) for market party products caches in seconds | No | 900 |
| MARKET_PARTY_NOTIFY_TTL | TTL for market party notifications cache in seconds | No | 43200 |
| FOOD_PARTY_NOTIFY_TTL | TTL for food party notifications cache in seconds | No | 43200 |
| FOOD_PARTY_SCHEDULE | Cron schedule for food party runs | No | "*/15 * * * *" |
| MARKET_PARTY_SCHEDULE | Cron schedule for market party runs | No | "*/15 * * * *" |
| DEFAULT_LATITUDE | Default latitude for UI map | No | 36.32112700482277 (Mashhad) |
| DEFAULT_LONGITUDE | Default longitude for UI map | No | 59.53740119934083 (Mashhad) |
| DEFAULT_IMAGE | Default image for notifications | No | [Image](https://raw.githubusercontent.com/ahbanavi/goshne/main/resource/default.jpg) |

## License
This project is licensed under the [Creative Commons Attribution-NonCommercial 4.0 International License](./LICENSE.md). You are free to share, copy and adapt the material in any medium or format for non-commercial purposes with proper attribution, providing a link to the license, and indicating if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
2 changes: 1 addition & 1 deletion app/Notifications/MarketPartyNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function toTelegram($notifiable)
'🛵 '.($vendor['isPro'] ? '<s>'.number_format($vendor['deliveryFee']).' ت</s> <b> ارسال رایگان (پرو)</b>' : number_format($vendor['deliveryFee']).' ت')."\n\n".
"⌛️ {$product['marketPartyCapacity']} موجود ({$product['capacity']} قابل سفارش، کف ".number_format($product['minOrder']).' ت)'
)
->photo(empty($product['mainImage']) ? 'https://raw.githubusercontent.com/ahbanavi/goshne/main/resource/default.jpg' : $product['mainImage'])
->photo(empty($product['mainImage']) ? config('goshne.default.image') : $product['mainImage'])
->button('🛒 سوپر مارکت ', $vendor_url);
}

Expand Down
29 changes: 29 additions & 0 deletions app/Notifications/MyTelegramChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace App\Notifications;

use Illuminate\Notifications\Notification;
use NotificationChannels\Telegram\TelegramChannel;

class MyTelegramChannel extends TelegramChannel
{
public function send(mixed $notifiable, Notification $notification, bool $second = false): ?array
{
try {
$rsp = parent::send($notifiable, $notification);
} catch (\NotificationChannels\Telegram\Exceptions\CouldNotSendNotification $e) {
if ($e->getMessage() == 'Telegram responded with an error `400 - Bad Request: wrong file identifier/HTTP URL specified`') {
if (empty($notification->product['main_image']) || $second) {
$notification->product['main_image'] = config('goshne.default.image');
} else {
$notification->product['main_image'] = $notification->product['main_image'].'?v='.now()->timestamp;
}
$rsp = self::send($notifiable, $notification, true);
} else {
throw $e;
}
}

return $rsp;
}
}
6 changes: 3 additions & 3 deletions app/Notifications/SnappFoodPartyNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ public function __construct(
public bool $isLast = false,
) {}

public function via($notifiable): array
public function via($notifiable): string
{
return ['telegram'];
return MyTelegramChannel::class;
}

public function toTelegram($notifiable)
Expand All @@ -43,7 +43,7 @@ public function toTelegram($notifiable)
'🛵 '.number_format($product['deliveryFee'])." ت\n\n".
"⌛️ {$product['remaining']} موجود ({$product['capacity']} قابل سفارش، کف ".number_format($product['minOrder']).' ت)'
)
->photo(empty($product['main_image']) ? 'https://raw.githubusercontent.com/ahbanavi/goshne/main/resource/default.jpg' : $product['main_image'])
->photo(empty($product['main_image']) ? config('goshne.default.image') : $product['main_image'])
->button('🛍️ خرید محصول', $product_url)
->button('🍽 منو '.$product['vendorTypeTitle'], $vendor_url);
}
Expand Down
1 change: 1 addition & 0 deletions config/goshne.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
'default' => [
'latitude' => env('DEFAULT_LATITUDE', 36.32112700482277),
'longitude' => env('DEFAULT_LONGITUDE', 59.53740119934083),
'image' => env('DEFAULT_IMAGE', 'https://raw.githubusercontent.com/ahbanavi/goshne/main/resource/default.jpg'),
],

];

0 comments on commit bcb0b65

Please sign in to comment.