2017 © Pedro Peláez
 

library line-bot-sdk

SDK of the LINE BOT API for PHP

image

linecorp/line-bot-sdk

SDK of the LINE BOT API for PHP

  • Thursday, July 19, 2018
  • by moznion
  • Repository
  • 89 Watchers
  • 372 Stars
  • 118,495 Installations
  • PHP
  • 14 Dependents
  • 0 Suggesters
  • 425 Forks
  • 10 Open issues
  • 18 Versions
  • 13 % Grown

The README.md

LINE Messaging API SDK for PHP

Build Status, (*1)

Introduction

The LINE Messaging API SDK for PHP makes it easy to develop bots using LINE Messaging API, and you can create a sample bot within minutes., (*2)

Documentation

See the official API documentation for more information., (*3)

  • English: https://developers.line.biz/en/docs/messaging-api/overview/
  • Japanese: https://developers.line.biz/ja/docs/messaging-api/overview/

Requirements

  • PHP 8.1 or later

Installation

Install the LINE Messaging API SDK using Composer., (*4)

$ composer require linecorp/line-bot-sdk

Getting started

Create the bot client instance

The bot client instance is a handler of the Messaging API., (*5)

$client = new \GuzzleHttp\Client();
$config = new \LINE\Clients\MessagingApi\Configuration();
$config->setAccessToken('<channel access token>');
$messagingApi = new \LINE\Clients\MessagingApi\Api\MessagingApiApi(
  client: $client,
  config: $config,
);

You must use the Client with GuzzleHttp\ClientInterface implementation., (*6)

Call API

You can call an API through the messagingApi instance., (*7)

A very simple example:, (*8)

$message = new TextMessage(['type' => 'text','text' => 'hello!']);
$request = new ReplyMessageRequest([
    'replyToken' => '<reply token>',
    'messages' => [$message],
]);
$response = $messagingApi->replyMessage($request);

This procedure sends a message to the destination that is associated with <reply token>., (*9)

We also support setter style., (*10)

$message = (new TextMessage())
  ->setType(\LINE\Constants\MessageType::TEXT)
  ->setText('hello!');
$request = (new ReplyMessageRequest)
  ->setReplyToken('<reply token>')
  ->setMessages([$message]);
try {
  $messagingApi->replyMessage($request);
  // Success
} catch (\LINE\Clients\MessagingApi\ApiException $e) {
  // Failed
  echo $e->getCode() . ' ' . $e->getResponseBody();
}

How to get x-line-request-id header and error message

You may need to store the x-line-request-id header obtained as a response from several APIs. In this case, please use ~WithHttpInfo functions. You can get headers and status codes. The x-line-accepted-request-id or content-type header can also be obtained in the same way., (*11)

$request = new ReplyMessageRequest([
    'replyToken' => $replyToken,
    'messages' => [$textMessage = (new TextMessage(['text' => 'reply with http info', 'type' => MessageType::TEXT]))],
]);
$response = $messagingApi->replyMessageWithHttpInfo($request);
$this->logger->info('body:' . $response[0]);
$this->logger->info('http status code:' . $response[1]);
$this->logger->info('headers(x-line-request-id):' . $response[2]['x-line-request-id'][0]);

You can get error messages from \LINE\Clients\MessagingApi\ApiException when you use MessagingApiApi. Each client defines its own exception class., (*12)

try {
    $profile = $messagingApi->getProfile("invalid-userId");
} catch (\LINE\Clients\MessagingApi\ApiException $e) {
    $headers = $e->getResponseHeaders();
    $lineRequestId = isset($headers['x-line-request-id']) ? $headers['x-line-request-id'][0] : 'Not Available';
    $httpStatusCode = $e->getCode();
    $errorMessage = $e->getResponseBody();

    $this->logger->info("x-line-request-id: $lineRequestId");
    $this->logger->info("http status code: $httpStatusCode");
    $this->logger->info("error response: $errorMessage");
}

When you need to get x-line-accepted-request-id header from error response, you can get it: $headers['x-line-accepted-request-id'][0]., (*13)

Components

Webhook

LINE's server sends user actions (such as a message, image, or location) to your bot server. Request of that contains event(s); event is action of the user., (*14)

The following shows how the webhook is handled:, (*15)

  1. Receive webhook from LINE's server.
  2. Parse request payload by EventRequestParser#parseEventRequest($body, $channelSecret, $signature).
  3. Iterate parsed events and some react as you like.

The following examples show how webhooks are handled:, (*16)

More information

For more information, see the official API documents and PHPDoc. If it's your first time using this library, we recommend taking a look at examples and the PHPDoc of \LINE ., (*17)

Hints

Examples

This repository contains two examples of how to use the LINE Messaging API., (*18)

EchoBot

A simple sample implementation. This application reacts to text messages that are sent from users., (*19)

KitchenSink

A full-stack (and slightly complex) sample implementation. This application demonstrates a practical use of the LINE Messaging API., (*20)

PHPDoc

https://line.github.io/line-bot-sdk-php/, (*21)

This library provides PHPDoc to describe how to use the methods. You can generate the documentation using phpDocumenter using the following command., (*22)

$ wget https://github.com/phpDocumentor/phpDocumentor/releases/download/v3.3.1/phpDocumentor.phar $ php phpDocumentor.phar run -d src -t docs The HTML files are generated in docs/., (*23)

Official API documentation

Official API documents shows the detail of Messaging API and fundamental usage of SDK., (*24)

See also

Laravel Support

Easy to use from Laravel. After installed, add LINE_BOT_CHANNEL_ACCESS_TOKEN to .env, (*25)

LINE_BOT_CHANNEL_ACCESS_TOKEN=<Channel Access Token>

then you can use facades like following., (*26)

$profile = \LINEMessagingApi::pushMessage(....);

Facade uses \GuzzleHttp\Client by default. If you want to change the config, run, (*27)

$ php artisan vendor:publish --provider="LINE\Laravel\LINEBotServiceProvider" --tag=config

Then line-bot.php will be published to config/ dir. If you want to configure a custom header, do the following., (*28)

return [
    'channel_access_token' => env('LINE_BOT_CHANNEL_ACCESS_TOKEN'),
    'channel_id' => env('LINE_BOT_CHANNEL_ID'),
    'channel_secret' => env('LINE_BOT_CHANNEL_SECRET'),
    'client' => [
        'config' => [
          'headers' => ['X-Foo' => 'Bar'],
        ],
    ],
];

Help and media

FAQ: https://developers.line.biz/en/faq/, (*29)

News: https://developers.line.biz/en/news/, (*30)

Versioning

This project respects semantic versioning., (*31)

See http://semver.org/, (*32)

Contributing

Please check CONTRIBUTING before making a contribution., (*33)

For hacking instructions, please refer HACKING.md., (*34)

License

Copyright 2016 LINE Corporation

Licensed under the Apache License, version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

The Versions

19/07 2018

dev-master

9999999-dev https://github.com/line/line-bot-sdk-php

SDK of the LINE BOT API for PHP

  Sources   Download

Apache-2.0 Apache License Version 2.0

The Requires

  • php >=5.5

 

The Development Requires

by Avatar moznion
by Satoru Yoshihara

bot sdk line

19/07 2018

3.1.0

3.1.0.0 https://github.com/line/line-bot-sdk-php

SDK of the LINE BOT API for PHP

  Sources   Download

Apache License Version 2.0

The Requires

  • php >=5.5

 

The Development Requires

by Avatar moznion
by Satoru Yoshihara

bot sdk line

29/06 2018

3.0.0

3.0.0.0 https://github.com/line/line-bot-sdk-php

SDK of the LINE BOT API for PHP

  Sources   Download

Apache License Version 2.0

The Requires

  • php >=5.5

 

The Development Requires

by Avatar moznion
by Satoru Yoshihara

bot sdk line

20/09 2017

2.3.0

2.3.0.0 https://github.com/line/line-bot-sdk-php

SDK of the LINE BOT API for PHP

  Sources   Download

Apache License Version 2.0

The Requires

  • php >=5.4

 

The Development Requires

by Avatar moznion
by Satoru Yoshihara

bot sdk line

03/08 2017

2.2.0

2.2.0.0 https://github.com/line/line-bot-sdk-php

SDK of the LINE BOT API for PHP

  Sources   Download

Apache License Version 2.0

The Requires

  • php >=5.4

 

The Development Requires

by Avatar moznion
by Satoru Yoshihara

bot sdk line

31/05 2017

2.1.0

2.1.0.0 https://github.com/line/line-bot-sdk-php

SDK of the LINE BOT API for PHP

  Sources   Download

Apache License Version 2.0

The Requires

  • php >=5.4

 

The Development Requires

by Avatar moznion
by Satoru Yoshihara

bot sdk line

26/05 2017

2.0.0

2.0.0.0 https://github.com/line/line-bot-sdk-php

SDK of the LINE BOT API for PHP

  Sources   Download

Apache License Version 2.0

The Requires

  • php >=5.4

 

The Development Requires

by Avatar moznion
by Satoru Yoshihara

bot sdk line

27/04 2017

1.6.0

1.6.0.0 https://github.com/line/line-bot-sdk-php

SDK of the LINE BOT API for PHP

  Sources   Download

Apache License Version 2.0

The Requires

  • php >=5.4

 

The Development Requires

by Avatar moznion
by Satoru Yoshihara

bot sdk line

21/02 2017

1.5.0

1.5.0.0 https://github.com/line/line-bot-sdk-php

SDK of the LINE BOT API for PHP

  Sources   Download

Apache License Version 2.0

The Requires

  • php >=5.4

 

The Development Requires

by Avatar moznion
by Satoru Yoshihara

bot sdk line

20/01 2017

1.4.0

1.4.0.0 https://github.com/line/line-bot-sdk-php

SDK of the LINE BOT API for PHP

  Sources   Download

Apache License Version 2.0

The Requires

  • php >=5.6

 

The Development Requires

by Avatar moznion
by Satoru Yoshihara

bot sdk line

26/11 2016

1.3.0

1.3.0.0 https://github.com/line/line-bot-sdk-php

SDK of the LINE BOT API for PHP

  Sources   Download

Apache License Version 2.0

The Requires

  • php >=5.6

 

The Development Requires

by Avatar moznion
by Satoru Yoshihara

bot sdk line

21/11 2016

1.2.1

1.2.1.0 https://github.com/line/line-bot-sdk-php

SDK of the LINE BOT API for PHP

  Sources   Download

Apache License Version 2.0

The Requires

  • php >=5.6

 

The Development Requires

by Avatar moznion
by Satoru Yoshihara

bot sdk line

17/11 2016

1.2.0

1.2.0.0 https://github.com/line/line-bot-sdk-php

SDK of the LINE BOT API for PHP

  Sources   Download

Apache License Version 2.0

The Requires

  • php >=5.6

 

The Development Requires

by Avatar moznion
by Satoru Yoshihara

bot sdk line

19/10 2016

1.1.0

1.1.0.0 https://github.com/line/line-bot-sdk-php

SDK of the LINE BOT API for PHP

  Sources   Download

Apache License Version 2.0

The Requires

  • php >=5.6

 

The Development Requires

by Avatar moznion
by Satoru Yoshihara

bot sdk line

29/09 2016

1.0.0

1.0.0.0 https://github.com/line/line-bot-sdk-php

SDK of the LINE BOT API for PHP

  Sources   Download

Apache License Version 2.0

The Requires

  • php >=5.6

 

The Development Requires

by Avatar moznion

bot sdk line

27/04 2016

0.1.0

0.1.0.0 http://github.com/line/line-bot-sdk-php

SDK of the LINE BOT API Trial for PHP

  Sources   Download

Apache License Version 2.0

The Requires

 

The Development Requires

by Avatar moznion

bot sdk line

27/04 2016

0.0.2

0.0.2.0 http://github.com/line/line-bot-sdk-php

SDK of the LINE BOT API Trial for PHP

  Sources   Download

Apache License Version 2.0

The Requires

 

The Development Requires

by Avatar moznion

bot sdk line

27/04 2016

0.0.1

0.0.1.0 http://github.com/line/line-bot-sdk-php

SDK of the LINE BOT API Trial for PHP

  Sources   Download

Apache License Version 2.0

The Requires

 

The Development Requires

by Avatar moznion

bot sdk line