python的LINE Messaging API

・6 分钟阅读

  • 源代码名称: line-bot-sdk-python
  • 源代码网址: https://www.github.com/line/line-bot-sdk-python
  • line-bot-sdk-python的文档
  • line-bot-sdk-python的源代码下载
  • Git URL:
    git://www.github.com/line/line-bot-sdk-python.git
  • Git Clone代码到本地:
    git clone https://www.github.com/line/line-bot-sdk-python
  • Subversion代码到本地:
    $ svn co --depth empty https://www.github.com/line/line-bot-sdk-python
                              Checked out revision 1.
                              $ cd repo
                              $ svn up trunk
              
  • line-bot-sdk-python

    Build StatusPyPI versionDocumentation Status

    适用于Python的LINE Messaging API的SDK。

    关于LINE Messaging API

    有关更多信息,请参见官方API文档。

    中文:https://developers.line.me/en/docs/messaging-api/reference/

    日语:https://developers.line.me/ja/docs/messaging-api/reference/

    安装

    $ pip install line-bot-sdk

    概要

    使用方法:

    from flask import Flask, request, abortfrom linebot import (
     LineBotApi, WebhookHandler
    )from linebot.exceptions import (
     InvalidSignatureError
    )from linebot.models import (
     MessageEvent, TextMessage, TextSendMessage,
    )
    app = Flask(__name__)
    line_bot_api = LineBotApi('YOUR_CHANNEL_ACCESS_TOKEN')
    handler = WebhookHandler('YOUR_CHANNEL_SECRET')@app.route("/callback", methods=['POST'])defcallback():
     # 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 bodytry:
     handler.handle(body, signature)
     except InvalidSignatureError:
     abort(400)
     return'OK'@handler.add(MessageEvent, message=TextMessage)defhandle_message(event):
     line_bot_api.reply_message(
     event.reply_token,
     TextSendMessage(text=event.message.text))if__name__=="__main__":
     app.run()

    API

    LineBotApi

    __init__(self ,channel_access_token ,endpoint='',timeout=5 ,http_client=RequestsHttpClient )

    创建新的LineBotApi实例。

    line_bot_api = linebot.LineBotApi('YOUR_CHANNEL_ACCESS_TOKEN')

    你可以重写每个方法的timeout值。

    reply_message(self ,reply_token ,messages ,timeout=None )

    响应来自用户,组和房间的事件,你可以从webhook事件对象中获取reply_令牌。

    https://developers.line.me/en/docs/messaging-api/reference/#send-reply-message

    line_bot_api.reply_message(reply_token, TextSendMessage(text='Hello World!'))
    push_message(self ,to ,messages ,timeout=None )

    随时向用户,组和房间发送消息。

    https://developers.line.me/en/docs/messaging-api/reference/#send-push-message

    line_bot_api.push_message(to, TextSendMessage(text='Hello World!'))
    multicast(self ,to ,messages ,timeout=None )

    在任何时候向多个用户发送消息。

    https://developers.line.me/en/docs/messaging-api/reference/#send-multicast-messages

    line_bot_api.multicast(['to1', 'to2'], TextSendMessage(text='Hello World!'))
    get_profile(self ,user_id ,timeout=None )

    获取用户配置文件信息。

    https://developers.line.me/en/docs/messaging-api/reference/#get-profile

    profile = line_bot_api.get_profile(user_id)print(profile.display_name)print(profile.user_id)print(profile.picture_url)print(profile.status_message)
    get_group_member_profile(self ,group_id ,user_id ,timeout=None )

    获取机器人所在组的成员的用户配置文件。这可以是未将机器人添加为朋友或已阻止机器人的用户的用户ID。

    https://developers.line.me/en/docs/messaging-api/reference/#get-group-member-profile

    profile = line_bot_api.get_group_member_profile(group_id, user_id)print(profile.display_name)print(profile.user_id)print(profile.picture_url)
    get_room_member_profile(self ,room_id ,user_id ,timeout=None )

    获取bot所在房间成员的用户配置文件,这可以是未将机器人添加为朋友或已阻止机器人的用户的用户ID。

    https://developers.line.me/en/docs/messaging-api/reference/#get-room-member-profile

    profile = line_bot_api.get_room_member_profile(room_id, user_id)print(profile.display_name)print(profile.user_id)print(profile.picture_url)
    get_group_member_ids (自定义,group_id,start=None,timeout=None ),

    获取机器人所在组的成员的用户ID。包括没有添加bot的用户的用户ID,或者阻止了bot 。

    https://developers.line.me/en/docs/messaging-api/reference/#get-group-member-user-ids

    member_ids_res = line_bot_api.get_group_member_ids(group_id)print(member_ids_res.member_ids)print(member_ids_res.next)
    get_room_member_ids(self ,room_id ,start=None ,timeout=None )

    获取bot所在房间成员的用户ID ,包括没有添加bot的用户的用户ID,或者阻止了bot 。

    https://developers.line.me/en/docs/messaging-api/reference/#get-room-member-user-ids

    member_ids_res = line_bot_api.get_room_member_ids(room_id)print(member_ids_res.member_ids)print(member_ids_res.next)
    get_message_content(self ,message_id ,timeout=None )

    检索用户发送的图像,视频和音频数据。

    https://developers.line.me/en/docs/messaging-api/reference/#get-content

    message_content = line_bot_api.get_message_content(message_id)withopen(file_path, 'wb') as fd:
     for chunk in message_content.iter_content():
     fd.write(chunk)
    讨论
    Fansisi profile image