Skip to main content

linebot-sdk-python wrapper.

Project description

pylinebot

linebot-sdk-python wrapper.

Install

pip install pylinebot

Import, Instance

from pylinebot import LINE, Tracer

bot = LINE(
    channel_access_token='XXXXXXXXXXXXXXXXXXX',
    channel_secret='XXXXXXXXX'
)

Example (Echo-bot)

import os
from typing import Any

from dotenv import load_dotenv
from flask import Flask, request

from pylinebot import LINE, Tracer
from pylinebot.types.event import Event, TracerEvent
from pylinebot.types.message import ContentType, Message


def receive_message(bot: LINE, event: Event.MESSAGE) -> None:
    message: Message = event.message
    message_type = message.type

    if message_type == ContentType.TEXT:
        bot.reply_text_message(message.text)


load_dotenv(verbose=True)
load_dotenv(".env")

CHANNEL_ACCESS_TOKEN: str = os.environ["CHANNEL_ACCESS_TOKEN"]
CHANNEL_SECRET: str = os.environ["CHANNEL_SECRET"]

app = Flask(__name__)

bot = LINE(channel_access_token=CHANNEL_ACCESS_TOKEN, channel_secret=CHANNEL_SECRET)

tracer = Tracer(bot, debug=True)
tracer.add_event(TracerEvent.MESSAGE, receive_message)


@app.route("/", methods=["POST"])
def hello() -> Any:
    signature = request.headers["X-Line-Signature"]
    body = request.get_data(as_text=True)
    tracer.trace(body, signature)
    return "OK"


if __name__ == "__main__":
    app.run(host="0.0.0.0", port=3000, debug=True)

Reply Message

Refer to this: sample_op.py

  • text
text = 'test'
bot.reply_text_message(text)
# > test
bot.reply_text_message(text, text, text)
# > test
# > test
# > test
  • image
img_url = 'https://xxx.xxxx/xxxx.jpg'
bot.reply_image_message(img_url)
# > img
  • video
video_message = VideoMessage(
    content_url='https://xxx.xxxx/xxxx.mp4',
    preview_url='https://xxx.xxxx/xxxx.jpg'
)
bot.reply_message([video_message])
  • audio
audio_message = AudioMessage(
    content_url="https://xxx.xxxx/xxxx.mp3",
    duration=1000,
)
bot.reply_message([audio_message])

Quick reply

action_list = [
    {
        'type': 'message',
        'label': 'aaaa',
        'text': 'これ送信'
    },
    {
        'type': 'camera',
        'label': 'かめら',
    },
    {
        'type': 'cameraRoll',
        'label': 'かめらろーる',
    },
    {
        'type': 'location',
        'label': 'ろけーしょん',
    },
    {
        'type': 'postback',
        'label': 'ぽすとばっく',
        'data': 'test_postback'
    },
    {
        'type': 'datetimepicker',
        'label': 'でーとぴっく',
        'data': 'test_datepick',
        'mode': 'date',
        'initial': '2020-05-15',
        'max': '2020-05-31',
        'min': '2020-05-01'
    }
]
bot.set_quick_reply(action_list)
bot.reply_text_message('quick_reply')

Send multiple messages

The max count of messages that can be sent at one time is 5.

# 2 text message
bot.reply_text_message('text', 'text')
# 5 text message
bot.reply_text_message('text', 'text', 'text', 'text', 'text')
# Error
bot.reply_text_message('text', 'text', 'text', 'text', 'text', 'text')

Send various messages at once.

video_message = VideoMessage(
    content_url="https://xxx.xxxx/xxxx.mp4",
    preview_url="https://xxx.xxxx/xxxx.png",
)
audio_message = AudioMessage(
    content_url="https://xxx.xxxx/xxxx.mp3",
    duration=1000,
)
image_message = ImageMessage(
    preview_url="https://xxx.xxxx/xxxx.png",
    content_url="https://xxx.xxxx/xxxx.png",
)
text_message = TextMessage("test")
# メッセージを詰める
messages: List[SEND_MESSAGE] = []
messages.append(video_message)
messages.append(audio_message)
messages.append(image_message)
messages.append(text_message)
bot.reply_message(messages)

Save message content

Save image, video, and audio data sent by users.

bot.save_content_from_message_id(message_id, file_name)

How to use

from pylinebot.types.event import Event
from pylinebot.types.message import ContentType

def receive_message(bot: LINE, event: Event.MESSAGE) -> None:
    message = event.message
    message_type = message.type

    if message_type == ContentType.IMAGE:
        bot.save_content_from_message_id(message_id, f"{message_id}.jpg")
        bot.reply_text_message("その画像", "保存したよ。")

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

pylinebot-2.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file pylinebot-2.0-py3-none-any.whl.

File metadata

  • Download URL: pylinebot-2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.51.0 CPython/3.7.3

File hashes

Hashes for pylinebot-2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f7d7f10159db25391e080ca228244f487899149cba47dce681f9cfd62cbaec79
MD5 81a5a7985986caad96e6e66d1366bc3f
BLAKE2b-256 944ab3a564cc6f849de829b9b1279270047c27c0319dd9ab5f2a93581ba3a8ed

See more details on using hashes here.

Supported by

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