ratiw/line-messaging

Simplify sending LINE messages using Messaging API.

1.2.0 2024-10-10 07:18 UTC

This package is auto-updated.

Last update: 2024-11-10 07:26:22 UTC


README

Simplify sending LINE messages using Messaging API for Laravel

Latest Version on Packagist Total Downloads

This package was developed to replace the use LINE Notify as it will be discontinued on March 31, 2025.

Basic Usage

use Ratiw\LineMessaging\LineMessaging;
use Illuminate\Http\Client\Response;

// simple text message to a user
$response = LineMessaging::channel('YOUR_CHANNEL_ACCESS_TOKEN')
    ->toUser('USER_ID')
    ->text('Hi, this is a test message.');

// sticker message to a group chat
$response = LineMessaging::channel('YOUR_CHANNEL_ACCESS_TOKEN')
    ->toGroup('GROUP_ID')
    ->sticker('6359', '11069851');

// text with emojis
$response = LineMessaging::channel('YOUR_CHANNEL_ACCESS_TOKEN')
    ->toGroup('GROUP_ID')
    ->text('Hey, $ LINE emoji $', [
        ['productId' => '5ac2213e040ab15980c9b447', 'emojiId' => '009'],
        ['productId' => '5ac21d59031a6752fb806d5d', 'emojiId' => '004'],
    ]);

Understanding LINE Messaging API

In order to send LINE messages via LINE Messaging API, you need the following:

  • Channel access token
  • User ID, Group ID, or Room ID
  • what type of message you want to send

Getting those information can be tricky, but you usually do it once and then you can reuse it.

If you do not know how to get those information, please refer to the LINE Messaging API documentation, Or search for video tutorials on YouTube.

Getting User ID, Group ID, or Room ID

You can definitely use a free webhook.site to gather user IDs with the LINE Messaging API, but there's a key point to remember: The user ID will only be included in the webhook data if the user has consented to share their profile information with your LINE Official Account.

That means the user has to add your LINE Official Account as a friend (and, you'll get the user ID from the webhook), or invite your LINE Official Account to a group chat (and, you'll get the group ID from the webhook).

Message Types

This package currently supports the following message types:

  • Text
  • Sticker
  • Image
  • Video
  • Audio
  • Location

More might be added in the future, but no promises here. 😃

Installation

You can install the package via composer:

composer require ratiw/line-messaging

Usage

Use the LineMessaging::channel() to specify the channel access token and chain one of the following methods to specify the target user, group, or room.

  • toUser("USER_ID")
  • toGroup("GROUP_ID")
  • toRoom("ROOM_ID")

Then, chain one of the available message types to send the message.

  • text(message, emojis, quoteToken)
  • sticker(packageId, stickerId)
  • image(imageUrl, previewUrl)
  • video(videoUrl, previewImageUrl, trackingId)
  • audio(audioUrl, duration)
  • location(title, address, latitude, longitude)

Sending text message

text(string $message, $emojis = [], string $quoteToken = null)

// send a text message to a user
LineMessaging::channel('YOUR_CHANNEL_ACCESS_TOKEN')
    ->toUser('USER_ID')
    ->text('Hello, world!');

// send a text message to a group chat
LineMessaging::channel('YOUR_CHANNEL_ACCESS_TOKEN')
    ->toGroup('GROUP_ID')
    ->text('Hello, world!');

Sending text message with emojis

To include emojis in the text message, you have to specify the emoji placeholders in the text message.

The placeholder is the $ character in the message.

Then, you have to includes an array of emoji objects in the second argument.

Each emoji object must have the following keys:

  • productId: The product ID of the emoji.
  • emojiId: The emoji ID of the emoji.
LineMessaging::channel('YOUR_CHANNEL_ACCESS_TOKEN')
    ->toUser('USER_ID')
    ->text('$ Happy birthday! $', [
        ['productId' => '5ac2213e040ab15980c9b447', 'emojiId' => '007'],
        ['productId' => '5ac2213e040ab15980c9b447', 'emojiId' => '009'],
    ]);

See

  • LINE emojis here.
  • LINE text message type here.
  • LINE text message reference here.

Sending sticker

sticker($packageId, $stickerId)

LineMessaging::channel('YOUR_CHANNEL_ACCESS_TOKEN')
    ->toGroup('GROUP_ID')
    ->sticker('6359', '11069851');

See

  • LINE stickers list here.
  • LINE sticker message type here.
  • LINE sticker message reference here.

Sending image

image(string $imageUrl, string $previewUrl = null)

LineMessaging::channel('YOUR_CHANNEL_ACCESS_TOKEN')
    ->toGroup('GROUP_ID')
    ->image('https://example.com/image.jpg', 'https://example.com/image_preview.jpg');

Note

  • previewUrl is optional.
  • given URL must be https scheme

See

  • LINE images message type here.
  • LINE image message reference here.

Sending video

video(string $videoUrl, string $previewImageUrl = null, string $trackingId = null)

LineMessaging::channel('YOUR_CHANNEL_ACCESS_TOKEN')
    ->toUser('USER_ID')
    ->video('https://example.com/video.mp4', 'https://example.com/video_preview.jpg', 'TRACKING_ID');

Note

  • previewImageUrl and trackingId are optional.
  • given URL must be https scheme

Sending audio

audio(string $audioUrl, int $duration)

LineMessaging::channel('YOUR_CHANNEL_ACCESS_TOKEN')
    ->toUser('USER_ID')
    ->audio('https://example.com/video.m4a', 50000);

Note

  • given URL must be https scheme
  • duration is in milliseconds

Sending location

location(string $title, string $address, float $latitude, float $longitude)

LineMessaging::channel('YOUR_CHANNEL_ACCESS_TOKEN')
    ->toUser('USER_ID')
    ->location('MOCA Bangkok', '499 Kamphaeng Phet 6 Rd, Lat Yao, Chatuchak, Bangkok 10900', 13.853197788942376, 100.56302862528158);

Testing

composer test

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

Laravel Package Boilerplate

This package was generated using the Laravel Package Boilerplate.