ka4ivan/laravel-notification-channel-instagram
To begin using the ka4ivan/laravel-notification-channel-instagram package, follow these minimal steps:
Install the Package
composer require ka4ivan/laravel-notification-channel-instagram
Set Up Instagram Bot
config/services.php:
'instagram' => [
'api_version' => env('INSTAGRAM_API_VERSION', '22.0'),
'access_token' => env('INSTAGRAM_ACCESS_TOKEN', ''),
'profile_id' => env('INSTAGRAM_PROFILE_ID', ''),
'start_buttons' => [
['question' => 'Start', 'payload' => 'start'],
],
],
Configure Start Buttons Run the artisan command to set up interactive start buttons:
php artisan instagram:set-start-buttons
First Notification Use Case
Create a notification class (e.g., ChannelConnected) and send a test message:
use NotificationChannels\Instagram\InstagramChannel;
use NotificationChannels\Instagram\InstagramMessage;
use Illuminate\Notifications\Notification;
class ChannelConnected extends Notification
{
public function via($notifiable)
{
return [InstagramChannel::class];
}
public function toInstagram($notifiable)
{
return InstagramMessage::create()
->text('Welcome! Your channel is now connected.');
}
}
Send the notification via:
$user->notify(new ChannelConnected());
Basic Text Notification
return InstagramMessage::create('Your order #12345 is confirmed!');
Rich Message with Attachments
return InstagramMessage::create()
->text('Check out your new product!')
->attach(AttachmentType::IMAGE, 'https://example.com/image.jpg');
Interactive Buttons
return InstagramMessage::create()
->text('How can we help?')
->buttons([
Button::create('View Website', 'https://example.com')->isTypeWebUrl(),
Button::create('Contact Support', ['user' => $userId])->isTypePostback(),
]);
Dynamic Routing
Override routeNotificationForInstagram() in your User model:
public function routeNotificationForInstagram()
{
return $this->instagram_id; // Store this in your DB
}
Queue Notifications Use Laravel’s queue system to avoid API rate limits:
$user->notify(new ChannelConnected())->onQueue('instagram');
Override Default Config Set API version/tokens per message:
return InstagramMessage::create()
->setApiVersion('v23.0')
->setAccessToken('custom_token')
->text('Custom config message.');
Batch Sending
Use attachMany() for carousel-style messages:
return InstagramMessage::create()
->attachMany(AttachmentType::IMAGE, [
'https://example.com/image1.jpg',
'https://example.com/image2.jpg',
]);
Error Handling
Wrap notifications in a try-catch to log failures:
try {
$user->notify(new ChannelConnected());
} catch (\Exception $e) {
\Log::error('Instagram notification failed: ' . $e->getMessage());
}
Access Token Expiry
Rate Limiting
Profile ID Mismatch
profile_id in config/services.php matches the Instagram Business account linked to your Facebook Page.Attachment Size Limits
Button Limitations
web_url or postback.Enable API Logging
Add this to config/services.php to log API responses:
'debug' => env('INSTAGRAM_DEBUG', false),
Test in Sandbox Use Instagram’s Sandbox Mode for development to avoid rate limits.
Check Webhook Status If using postback buttons, verify webhook subscriptions via:
curl -X GET "https://graph.instagram.com/me/webhooks?access_token=ACCESS_TOKEN"
Custom Button Types
Extend the Button class to support additional types (e.g., phone_number):
namespace App\Notifications\Instagram;
use NotificationChannels\Instagram\Components\Button;
class CustomButton extends Button
{
public function isTypePhoneNumber()
{
$this->type = 'phone_number';
return $this;
}
}
Override Message Builder
Create a custom InstagramMessage class to add features:
namespace App\Notifications\Instagram;
use NotificationChannels\Instagram\InstagramMessage as BaseMessage;
class CustomMessage extends BaseMessage
{
public function quickReplies($replies)
{
$this->quickReplies = $replies;
return $this;
}
}
Add Analytics Track sent messages by extending the channel:
namespace App\Notifications\Channels;
use NotificationChannels\Instagram\InstagramChannel as BaseChannel;
class CustomInstagramChannel extends BaseChannel
{
public function send($notifiable, array $message)
{
\Log::info('Sent Instagram notification to: ' . $notifiable->instagram_id);
parent::send($notifiable, $message);
}
}
API Version
Always specify the latest stable version in config/services.php (e.g., v22.0). Check Instagram’s API Changelog for breaking changes.
Start Buttons
The instagram:set-start-buttons command must be run after configuring access_token and profile_id. Re-run if buttons disappear.
Environment Variables
Use .env for sensitive data:
INSTAGRAM_ACCESS_TOKEN=your_token_here
INSTAGRAM_PROFILE_ID=your_profile_id
INSTAGRAM_API_VERSION=v22.0
How can I help you explore Laravel packages today?