ka4ivan/laravel-notification-channel-instagram
This package makes it easy to send notifications using the Instagram Messenger with Laravel.
You can install the package via composer:
composer require ka4ivan/laravel-notification-channel-instagram
curl -X GET "https://graph.instagram.com/me?fields=id,username&access_token=ACCESS_TOKEN"
Next we need to add tokens to our Laravel configurations. Create a new Instagram section inside config/services.php and place the page token there:
// 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',
],
],
],
Run the command to set the start buttons
php artisan instagram:set-start-buttons
Possible options for the command
{--access_token= : Instagram access token}
{--profile_id= : Instagram profile ID}
{--api_version= : Instagram API version (default from config)}'
This command will add the start buttons that appear when entering the chat for the first time
You can now use the Instagram channel in your via() method, inside the InvoicePaid class. The to($recipientId) (IGSID) method defines the Instagram user, you want to send the notification to.
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()
->to($notifiable->instagram_id) // Optional
->setApiVersion('22.0') // Optional
->setAccessToken('ABCD') // Optional
->setProfileId('abcd') // Optional
->text('Congratulations, the communication channel is connected');
}
}
The notification will be sent from your Instagram page, whose page token you have configured earlier. Here's a screenshot preview of the notification inside the chat window.
return InstagramMessage::create('You have just paid your monthly fee! Thanks');
return InstagramMessage::create()
->attach(AttachmentType::IMAGE, url('images/'.$this->product->id))
->to($notifiable->instagram_id);
return InstagramMessage::create()
->to($notifiable->instagram_id)
->text('How can we help?')
->buttons([
Button::create('View Website', $url)->isTypeWebUrl(),
Button::create('Start Chatting', ['user' => $this->user->id])->isTypePostback() // Custom payload sent back to your server
]);
You can either send the notification by providing with the page-scoped user id of the recipient to the to($recipientId) (IGSID) method like shown in the above example or add a routeNotificationForInstagram() method in your notifiable model:
/**
* Route notifications for the Instagram channel.
*
* @return int
*/
public function routeNotificationForInstagram()
{
return $this->instagram_id;
}
to($recipientId): (string) User (recipient) Instagram ID (IGSID).text(''): (string) Notification message.attach($attachmentType, $url): (AttachmentType, string) An attachment type (IMAGE, AUDIO, VIDEO) and the url of this attachment.attachMany($attachmentType, $urls): (AttachmentType, array) An attachment type (IMAGE, AUDIO, VIDEO) and the urls array of this attachments.buttons($buttons = []): (array) An array of "Call to Action" buttons (Created using NotificationChannels\Instagram\Components\Button::create()). You can add up to 3 buttons of one of the following types: web_url or postback. See Button methods below for more details.setApiVersion($apiVersion): (string) Set Default Graph API Version.setAccessToken($accessToken): (string) Set the access token used for authenticating API requests.setProfileId($profileId): (string) Set the Instagram profile ID for API requests.title(''): (string) Button Title.data(''): (string) Button Data - It can be a web url or postback data.type(''): (string) Button Type - web_url or postback. Use ButtonType enumerator for guaranteeing valid valuesisTypeWebUrl(): Helper method to create a web_url type button.isTypePostback(): Helper method to create a postback type button.Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.
How can I help you explore Laravel packages today?