Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 18 additions & 20 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ Usage:
from flask import Flask, request, abort

from linebot.v3 import (
LineBotClient,
WebhookHandler
)
from linebot.v3.exceptions import (
InvalidSignatureError
)
from linebot.v3.messaging import (
Configuration,
ApiClient,
MessagingApi,
ReplyMessageRequest,
TextMessage
)
Expand All @@ -60,7 +58,7 @@ Usage:

app = Flask(__name__)

configuration = Configuration(access_token='YOUR_CHANNEL_ACCESS_TOKEN')
line_bot_client = LineBotClient(channel_access_token='YOUR_CHANNEL_ACCESS_TOKEN')
handler = WebhookHandler('YOUR_CHANNEL_SECRET')


Expand All @@ -85,14 +83,12 @@ Usage:

@handler.add(MessageEvent, message=TextMessageContent)
def handle_message(event):
with ApiClient(configuration) as api_client:
line_bot_api = MessagingApi(api_client)
line_bot_api.reply_message_with_http_info(
ReplyMessageRequest(
reply_token=event.reply_token,
messages=[TextMessage(text=event.message.text)]
)
line_bot_client.reply_message(
ReplyMessageRequest(
reply_token=event.reply_token,
messages=[TextMessage(text=event.message.text)]
)
)

if __name__ == "__main__":
app.run()
Expand Down Expand Up @@ -122,6 +118,7 @@ WebhookParser
parser = linebot.v3.WebhookParser(
'YOUR_CHANNEL_SECRET',
skip_signature_verification=lambda: False
)

parse(self, body, signature, as_payload=False)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -141,7 +138,7 @@ If the signature does NOT match, ``InvalidSignatureError`` is raised.
payload = parser.parse(body, signature, as_payload=True)

for event in payload.events:
do_something(payload.event, payload.destination)
do_something(event, payload.destination)

WebhookHandler
~~~~~~~~~~~~~~
Expand All @@ -159,6 +156,7 @@ WebhookHandler
handler = linebot.v3.WebhookHandler(
'YOUR_CHANNEL_SECRET',
skip_signature_verification=lambda: False
)

handle(self, body, signature)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand All @@ -178,16 +176,16 @@ Add a **handler** method by using this decorator.

.. code:: python

@handler.add(MessageEvent, message=TextMessage)
@handler.add(MessageEvent, message=TextMessageContent)
def handle_message(event):
line_bot_api.reply_message(
line_bot_client.reply_message(
ReplyMessageRequest(
reply_token=event.reply_token,
messages=[TextMessage(text=event.message.text)]
)
)

When the event is an instance of MessageEvent and event.message is an instance of TextMessage,
When the event is an instance of MessageEvent and event.message is an instance of TextMessageContent,
this handler method is called.

.. code:: python
Expand Down Expand Up @@ -286,7 +284,7 @@ Thus, you can send a JSON designed with `Flex Message Simulator <https://develop

bubble_string = """{ type:"bubble", ... }"""
message = FlexMessage(alt_text="hello", contents=FlexContainer.from_json(bubble_string))
line_bot_api.reply_message(
line_bot_client.reply_message(
ReplyMessageRequest(
reply_token=event.reply_token,
messages=[message]
Expand All @@ -302,7 +300,7 @@ The ``x-line-accepted-request-id`` or ``content-type`` header can also be obtain

.. code:: python

response = line_bot_api.reply_message_with_http_info(
response = line_bot_client.reply_message_with_http_info(
ReplyMessageRequest(
reply_token=event.reply_token,
messages=[TextMessage(text='see application log')]
Expand All @@ -312,13 +310,13 @@ The ``x-line-accepted-request-id`` or ``content-type`` header can also be obtain
app.logger.info("Got x-line-request-id: " + response.headers['x-line-request-id'])
app.logger.info("Got response with http body: " + str(response.data))

You can get error messages from ``ApiException`` when you use ``MessagingApi``. Each client defines its own exception class.
You can get error messages from ``ApiException`` when you use ``LineBotClient``. Each underlying client defines its own exception class.

.. code:: python

from linebot.v3.messaging import ApiException, ErrorResponse
try:
line_bot_api.reply_message_with_http_info(
line_bot_client.reply_message_with_http_info(
ReplyMessageRequest(
reply_token='invalid-reply-token',
messages=[TextMessage(text='see application log')]
Expand Down Expand Up @@ -365,7 +363,7 @@ If you keep using old line-bot-sdk library (``version < 3.x``) but use ``3.x``,

::

LineBotSdkDeprecatedIn30: Call to deprecated method get_bot_info. (Use 'from linebot.v3.messaging import MessagingApi' and 'MessagingApi(...).get_bot_info(...)' instead. See https://github.com/line/line-bot-sdk-python/blob/master/README.rst for more details.) -- Deprecated since version 3.0.0.
LineBotSdkDeprecatedIn30: Call to deprecated method get_bot_info. (Use 'from linebot.v3 import LineBotClient' and 'LineBotClient(...).get_bot_info(...)' instead. See https://github.com/line/line-bot-sdk-python/blob/master/README.rst for more details.) -- Deprecated since version 3.0.0.


If it's noisy, you can suppress this warning as follows.
Expand Down
9 changes: 9 additions & 0 deletions docs/source/linebot.v3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ linebot.v3 package

.. automodule:: linebot.v3

Client
------

.. autoclass:: linebot.v3.line_bot_client.LineBotClient
:members:

.. autoclass:: linebot.v3.async_line_bot_client.AsyncLineBotClient
:members:

linebot.v3.webhook module
--------------------------

Expand Down
15 changes: 4 additions & 11 deletions examples/aiohttp-echo/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,10 @@
from aiohttp.web_runner import TCPSite

from linebot.v3 import (
WebhookParser
WebhookParser,
AsyncLineBotClient,
)
from linebot.v3.messaging import (
Configuration,
AsyncApiClient,
AsyncMessagingApi,
TextMessage,
ReplyMessageRequest
)
Expand All @@ -52,13 +50,9 @@
print('Specify LINE_CHANNEL_ACCESS_TOKEN as environment variable.')
sys.exit(1)

configuration = Configuration(
access_token=channel_access_token
)


class Handler:
def __init__(self, line_bot_api: AsyncMessagingApi, parser: WebhookParser):
def __init__(self, line_bot_api: AsyncLineBotClient, parser: WebhookParser):
self.line_bot_api = line_bot_api
self.parser = parser

Expand Down Expand Up @@ -87,8 +81,7 @@ async def echo(self, request):


async def main(port=8000):
async_api_client = AsyncApiClient(configuration)
line_bot_api = AsyncMessagingApi(async_api_client)
line_bot_api = AsyncLineBotClient(channel_access_token=channel_access_token)
parser = WebhookParser(channel_secret)

handler = Handler(line_bot_api, parser)
Expand Down
12 changes: 2 additions & 10 deletions examples/fastapi-echo/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@

from fastapi import Request, FastAPI, HTTPException

from linebot.v3.webhook import WebhookParser
from linebot.v3 import WebhookParser, AsyncLineBotClient
from linebot.v3.messaging import (
AsyncApiClient,
AsyncMessagingApi,
Configuration,
ReplyMessageRequest,
TextMessage
)
Expand All @@ -44,13 +41,8 @@
print('Specify LINE_CHANNEL_ACCESS_TOKEN as environment variable.')
sys.exit(1)

configuration = Configuration(
access_token=channel_access_token
)

app = FastAPI()
async_api_client = AsyncApiClient(configuration)
line_bot_api = AsyncMessagingApi(async_api_client)
line_bot_api = AsyncLineBotClient(channel_access_token=channel_access_token)
parser = WebhookParser(channel_secret)


Expand Down
25 changes: 9 additions & 16 deletions examples/flask-echo/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
from argparse import ArgumentParser

from flask import Flask, request, abort
from linebot import (
WebhookParser
from linebot.v3 import (
WebhookParser,
LineBotClient,
)
from linebot.v3.exceptions import (
InvalidSignatureError
Expand All @@ -29,9 +30,6 @@
TextMessageContent,
)
from linebot.v3.messaging import (
Configuration,
ApiClient,
MessagingApi,
ReplyMessageRequest,
TextMessage
)
Expand All @@ -49,10 +47,7 @@
sys.exit(1)

parser = WebhookParser(channel_secret)

configuration = Configuration(
access_token=channel_access_token
)
line_bot_api = LineBotClient(channel_access_token=channel_access_token)


@app.route("/callback", methods=['POST'])
Expand All @@ -75,14 +70,12 @@ def callback():
continue
if not isinstance(event.message, TextMessageContent):
continue
with ApiClient(configuration) as api_client:
line_bot_api = MessagingApi(api_client)
line_bot_api.reply_message_with_http_info(
ReplyMessageRequest(
reply_token=event.reply_token,
messages=[TextMessage(text=event.message.text)]
)
line_bot_api.reply_message(
ReplyMessageRequest(
reply_token=event.reply_token,
messages=[TextMessage(text=event.message.text)]
)
)

return 'OK'

Expand Down
23 changes: 8 additions & 15 deletions examples/flask-echo/app_with_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

from flask import Flask, request, abort
from linebot.v3 import (
WebhookHandler
WebhookHandler,
LineBotClient,
)
from linebot.v3.exceptions import (
InvalidSignatureError
Expand All @@ -28,9 +29,6 @@
TextMessageContent,
)
from linebot.v3.messaging import (
Configuration,
ApiClient,
MessagingApi,
ReplyMessageRequest,
TextMessage
)
Expand All @@ -48,10 +46,7 @@
sys.exit(1)

handler = WebhookHandler(channel_secret)

configuration = Configuration(
access_token=channel_access_token
)
line_bot_api = LineBotClient(channel_access_token=channel_access_token)


@app.route("/callback", methods=['POST'])
Expand All @@ -74,14 +69,12 @@ def callback():

@handler.add(MessageEvent, message=TextMessageContent)
def message_text(event):
with ApiClient(configuration) as api_client:
line_bot_api = MessagingApi(api_client)
line_bot_api.reply_message_with_http_info(
ReplyMessageRequest(
reply_token=event.reply_token,
messages=[TextMessage(text=event.message.text)]
)
line_bot_api.reply_message(
ReplyMessageRequest(
reply_token=event.reply_token,
messages=[TextMessage(text=event.message.text)]
)
)


if __name__ == "__main__":
Expand Down
Loading