Là một dự án tự động hoá, line/line-bot-sdk-python đã đạt 2.1k sao trên GitHub, ngôn ngữ Python. LINE Messaging API SDK for Python
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
from flask import Flask, request, abort
from linebot.v3 import (
WebhookHandler
)
from linebot.v3.exceptions import (
InvalidSignatureError
)
from linebot.v3.messaging import (
Configuration,
ApiClient,
MessagingApi,
ReplyMessageRequest,
TextMessage
)
from linebot.v3.webhooks import (
MessageEvent,
TextMessageContent
)
app = Flask(__name__)
configuration = Configuration(access_token='YOUR_CHANNEL_ACCESS_TOKEN')
handler = WebhookHandler('YOUR_CHANNEL_SECRET')
@app.route("/callback", methods=['POST'])
def callback():
# get X-Line-Signature header value
signature = request.headers['X-Line-Signature']
# get request body as text
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
# handle webhook body
try:
handler.handle(body, signature)
except InvalidSignatureError:
app.logger.info("Invalid signature. Please check your channel access token/channel secret.")
abort(400)
return 'OK'
@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)]
)
)
if __name__ == "__main__":
app.run()
API
See linebot/v3/messaging/docs <linebot/v3/messaging/docs/MessagingApi.md>__ . Other docs are in linebot/v3/<feature>/docs/*.md.
Webhook
WebhookParser
※ You can use WebhookParser
\_\_init\_\_(self, channel\_secret, skip\_signature\_verification=lambda: False)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.. code:: python
parser = linebot.v3.WebhookParser('YOUR_CHANNEL_SECRET')
# or with skip_signature_verification option
parser = linebot.v3.WebhookParser(
'YOUR_CHANNEL_SECRET',
skip_signature_verification=lambda: False
parse(self, body, signature, as_payload=False)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Parses the webhook body, and returns a list of Event objects or a WebhookPayload object (depending on as_payload).
If the signature does NOT match, ``InvalidSignatureError`` is raised.
.. code:: python
events = parser.parse(body, signature)
for event in events:
do_something(event)
.. code:: python
payload = parser.parse(body, signature, as_payload=True)
for event in payload.events:
do_something(payload.event, payload.destination)
WebhookHandler
Handles webhooks with handlers added
by the decorators add <#add-self-event-message-none>__ and default <#default-self>__.
If the signature does NOT match, InvalidSignatureError is raised.
`aiohttp-echo <examples/aiohttp-echo>`__
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Sample echo-bot with asynchronous processings.
`fastapi-echo <examples/fastapi-echo>`__
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Sample echo-bot using `FastAPI <https://fastapi.tiangolo.com/>`__
`flask-echo <examples/flask-echo>`__
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Sample echo-bot using `Flask <http://flask.pocoo.org/>`__
`flask-kitchensink <examples/flask-kitchensink>`__
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Sample bot using `Flask <http://flask.pocoo.org/>`__
`rich-menu <examples/rich-menu>`__
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Switching richmenu script
`simple-server-echo <examples/simple-server-echo>`__
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Sample echo-bot using
`wsgiref.simple\_server <https://docs.python.org/3/library/wsgiref.html>`__
How to deserializes JSON to FlexMessage or RichMenu
line-bot-python-sdk provides from_json method for each model.
It deserializes the JSON into the specified model.
Thus, you can send a JSON designed with Flex Message Simulator <https://developers.line.biz/console/fx/>__.
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 ``~_with_http_info`` 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.
.. code:: python
response = line_bot_api.reply_message_with_http_info(
ReplyMessageRequest(
reply_token=event.reply_token,
messages=[TextMessage(text='see application log')]
)
)
app.logger.info("Got response with http status code: " + str(response.status_code))
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.
.. code:: python
from linebot.v3.messaging import ApiException, ErrorResponse
try:
line_bot_api.reply_message_with_http_info(
ReplyMessageRequest(
reply_token='invalid-reply-token',
messages=[TextMessage(text='see application log')]
)
)
except ApiException as e:
app.logger.info("Got response with http status code: " + str(e.status))
app.logger.info("Got x-line-request-id: " + e.headers['x-line-request-id'])
app.logger.info("Got response with http body: " + str(ErrorResponse.from_json(e.body)))
When you need to get ``x-line-accepted-request-id`` header from error response, you can get it: ``e.headers['x-line-accepted-request-id']``.
Help and media
--------------
FAQ: https://developers.line.biz/en/faq/
News: https://developers.line.biz/en/news/
Versioning
----------
This project respects semantic versioning
- See `semver.org <https://semver.org/>`_
However, if a feature that was publicly released is discontinued for business reasons and becomes completely unusable, we will release changes as a patch release.
Version 3.x
-----------
LINE's SDK developer team decided to generate SDK code based on OpenAPI spec. https://github.com/line/line-openapi
As a result, LINE bot sdk 3.x is not compatible with 2.x. It can follow the future API changes very quickly.
We will be maintaining only ``linebot.v3`` going forward.
To utilize the latest features, we recommend you gradually transition to ``linebot.v3`` modules in your application, although you can still continue to use the 2.x ``linebot`` modules.
While we won't update ``linebot`` modules anymore, users can still continue to use the version 2.x ``linebot`` modules.
We also welcome pull requests for the version ``2.x`` and ``3.x`` modules.
How to suppress deprecation warnings
------------------------------------
If you keep using old line-bot-sdk library (``version < 3.x``) but use ``3.x``, you'll get
::
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.
If it's noisy, you can suppress this warning as follows.
.. code:: python
import warnings
from linebot import LineBotSdkDeprecatedIn30
## your code here
...
if __name__ == '__main__':
warnings.filterwarnings("ignore", category=LineBotSdkDeprecatedIn30)
Contributing
------------
Please check `CONTRIBUTING <CONTRIBUTING.md>`__ before making a contribution.
.. |PyPI version| image:: https://badge.fury.io/py/line-bot-sdk.svg
:target: https://badge.fury.io/py/line-bot-sdk
License
--------
::
Copyright (C) 2016 LINE Corp.
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
http://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.
line/line-bot-sdk-python thuộc nhóm Automation trên TopGit, cùng 5 topic GitHub. Trang Trending và Topics liệt kê các repo cùng số sao và cùng ngôn ngữ để so sánh.
Đọc thêm về line/line-bot-sdk-python ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/line/line-bot-sdk-python là nguồn chính thức.
line/line-bot-sdk-python có bao nhiêu sao?
line/line-bot-sdk-python có 2.1k sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/line/line-bot-sdk-python. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
line/line-bot-sdk-python có những chủ đề gì?
GitHub topics của line/line-bot-sdk-python: "bot", "line", "linebot", "python", "sdk". TopGit xếp repo vào nhóm Automation.
line/line-bot-sdk-python có phải mã nguồn mở không?
Có — line/line-bot-sdk-python phát hành theo license Apache-2.0, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/line/line-bot-sdk-python.
line/line-bot-sdk-python có trang demo không?
Dự án có trang chủ ở https://pypi.python.org/pypi/line-bot-sdk. Tab "Readme" ở trang này thường có ảnh chụp và hướng dẫn bắt đầu nhanh.
line/line-bot-sdk-python còn đang phát triển không?
Commit gần nhất trên line/line-bot-sdk-python là 3 tháng trước (theo timestamp GitHub). Repo có 1.0k fork — một chỉ báo về mức độ quan tâm của cộng đồng.
Đọc đầy đủ README ở tab phía trên.
Chưa chắc line-bot-sdk-python có hợp với bạn?
Để ChatGPT, Claude hoặc Perplexity tìm hiểu giúp — bấm bên dưới và xem AI nói gì về line-bot-sdk-python.