Skip to main content

LINE Official User API wrapper library for Python

Project description

LINELib

LINELib は、LINE公式アカウントのWebチャット操作を行うための非公式Pythonラッパーです。

認証済みCookieの再利用、チャット・メッセージAPI、ファイル送信、SSEによるメッセージ受信、ローカルでのレート制限や再接続処理を主な機能としています。

※ このライブラリはLINEのWebエンドポイントとSeleniumによるログイン処理を利用します。 利用規約や運用上のリスクを理解した環境でのみ使用してください。


主な機能

  • Cookieを利用したセッション復元
  • Seleniumによる初回ログイン
  • Botアカウント・チャット一覧取得
  • テキスト送信、メンション送信、ファイル送信
  • 同期・非同期送信向けローカルレート制限
  • SSE(Server-Sent Events)によるイベント受信
  • lastEventId を利用した自動再接続
  • listen(block=False) と stop() による安全な制御
  • リスナー・レート制限・SSE関連の補助機能

インストール

pip install lineoa

開発用インストール:

pip install -e .

クイックスタート

from LINELib.linebot import LineBot

bot = LineBot(
    cookie_path="lineoa-storage.json",
    ping_secs=20,
    rate_limit=18,
    rate_limit_window=60,
)

BOT_ID = "Uxxxxxxxx"
CHAT_ID = "xxxxxxxx"

bot.sendMessage(
    bot_id=BOT_ID,
    chat_id=CHAT_ID,
    text="hello"
)

初回実行時はSeleniumがブラウザを起動し、 ログインしてCookieを保存する場合があります。

2回目以降は保存済みCookieが利用されます。


レート制限

ライブラリは送信履歴を保存し、 設定された制限を超える送信を防ぎます。

bot = LineBot(
    cookie_path="lineoa-storage.json",
    rate_limit=18,
    rate_limit_window=60,
    rate_limit_enabled=True,
)

status = bot.getRateLimitStatus()
print(status)

bot.resetRateLimit()

送信がブロックされた場合:

{
    "ratelimit": True,
    "ratelimit_after": 1710000000.0,
    "limit": 18,
    "window": 60.0,
}

※ この制限はローカル側の安全機能であり、 LINEサーバー側での送信成功を保証するものではありません。


イベント受信

from LINELib.linebot import LineBot

bot = LineBot(
    cookie_path="lineoa-storage.json",
    ping_secs=20,
    reconnect_interval=5,
    max_reconnects=None,
)

@bot.event
def on_message(event):
    payload = event.get("payload", {})
    chat_payload = payload.get("payload", {})
    message = chat_payload.get("message", {})
    text = message.get("text", "")

    if message.get("type") == "text" and text == "ping":
        bot.sendMessage(
            bot_id=payload.get("botId"),
            chat_id=payload.get("chatId"),
            text="pong",
        )

bot.listen(botid="Uxxxxxxxx")

再接続機能

listen() は以下の場合に自動再接続します。

  • SSE接続が切断された場合
  • 例外が発生した場合

最後に受信したイベントIDを記録し、 再接続時に引き継ぎます。


非同期リスニング

thread = bot.listen(
    botid="Uxxxxxxxx",
    block=False
)

他の処理

bot.stop()
thread.join()

stop() を呼び出すことで安全に終了できます。


ファイル送信

bot.sendFile(
    bot_id="Uxxxxxxxx",
    chat_id="xxxxxxxx",
    file_path="sample.png",
)

非同期送信

await bot._lib.async_send_message(
    user_id="xxxxxxxx",
    context="hello async",
    bot_id="Uxxxxxxxx",
)

主なクラス

LineBot 高レベルBotクラス

LINELib 認証セッション管理

ChatService LINEチャットAPI通信

AuthService Seleniumログイン管理

RateLimitConfig レート制限設定

ListenConfig リスナー設定

SSEParser SSE解析

SSEEvent SSEイベントオブジェクト


セキュリティ注意事項

  • lineoa-storage.json には認証Cookieが保存されます
  • GitHub等へ公開しないでください
  • デフォルト設定は保守的です(60秒間に18回送信)
  • コールバックエラーは他の処理へ影響しません
  • ネットワークエラー時は自動再接続されます
  • LINE側の仕様変更により動作しなくなる可能性があります

テスト実行

python -m unittest discover -s tests

テスト内容:

  • レート制限の検証
  • SSEパーサーの検証

※ LINEサーバーへの接続は行いません


ライセンス

MITライセンス

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lineoa-6.5.7.tar.gz (20.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lineoa-6.5.7-py3-none-any.whl (23.7 kB view details)

Uploaded Python 3

File details

Details for the file lineoa-6.5.7.tar.gz.

File metadata

  • Download URL: lineoa-6.5.7.tar.gz
  • Upload date:
  • Size: 20.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for lineoa-6.5.7.tar.gz
Algorithm Hash digest
SHA256 b117e764a6cf2ef99d01a6083235d727afcfa166ba4d646e1822790d1e2e594b
MD5 793df12b9df40385f3610562a2178e3f
BLAKE2b-256 3afca0fde69457289d04c314ab0959aa5a7bf8f52f3b512fabf80581c941a62b

See more details on using hashes here.

Provenance

The following attestation bundles were made for lineoa-6.5.7.tar.gz:

Publisher: python-publish.yml on Madoa5561/LINELib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file lineoa-6.5.7-py3-none-any.whl.

File metadata

  • Download URL: lineoa-6.5.7-py3-none-any.whl
  • Upload date:
  • Size: 23.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for lineoa-6.5.7-py3-none-any.whl
Algorithm Hash digest
SHA256 b719a89d5cdb005b34b6db486c45ea6d4361bbccd9ea8785a5b1f30539c79ef9
MD5 9c842005d097547db39494a7dd2be267
BLAKE2b-256 104e8b17c1f5f0ae5437b57a48026c197ccae1241dd41979ba2cd6da68812ab1

See more details on using hashes here.

Provenance

The following attestation bundles were made for lineoa-6.5.7-py3-none-any.whl:

Publisher: python-publish.yml on Madoa5561/LINELib

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page