Skip to content

Documentation

AWeirdDev edited this page Jan 23, 2023 · 29 revisions

Table Of Contents


linelib.Client

class linelib.Client(
    self,
    channel_secret: str,
    channel_access_token: str,
    *args, **options: Any
)

Represents a LINE Official Account (Client).

Client - Parameters

parameter type default description
channel_secret str required Channel secret.
channel_access_token str required Channel access token.
*args, **options Any (), {} No usage provided.

Client - Example

import logging
from linelib import Client

client = Client('channel secret', 'channel access token')

Client - Attributes

  • CS : str - Channel secret.
  • CAT : str - Channel access token.
  • app : Flask - (Running) Flask application. Note that it has been used CORS(app)
  • headers : dict - Headers for requests.
  • loop : Loop- Asyncio event loop.
  • _EVENTS* : dict- Saved event handlers.
  • _VALID_EVENTS* : list - Current valid events.

* - These attributes should not be overwritten or deleted by the user.

Client - Methods

Client.createEvents

def createEvents(
    self,
    name: str,
    obj: type
) -> None

Save the provided content to Client._EVENTS.

Client.createEvents - Parameters

parameter type default description
name str required Event name.
obj type required An event object.

Client.createEvents - Example

from linelib import Client

client = Client(...)

class ControlledEventObject:
    def __init__(self, json: dict):
        self.type = json['type'] # event type, REQUIRED
        self.options = json['options'] # additional options, REQUIRED
    
    async def emit(self, ctx):
        await ctx.reply('Hello, World!')

obj = ControlledEventObject({
    'type': 'text', # text message event
    'options': () # Empty
})

client.createEvents('text', obj) # registers the event handler

Client.emitEvents

def emitEvents(
    self,
    name: str,
    *args: Any,
    **kwargs: Any
) -> None

Runs the events inside Client._EVENTS.

Client.emitEvents - Parameters

parameter type default description
name str required The event name.
*args, **kwargs Any (), {} Any arguments to add for the event handlers.

Client.emitEvents - Example

from linelib import Client

client = Client(...)

@client.event('ready')
async def execute_me():
    print("I just got executed!")

client.emitEvents('ready') # no arguments required

Client.event

@Client.event(
    self,
    listener: str,
    *options
) -> EventObject

Registers an event handler. See Client._VALID_EVENTS to see a list of valid events.

Client.event - Parameters

Decorator Parameters

parameter type default description
listener Union[str, Callable] FUNCTION_NAME_OR_LISTENER Listener name or leave blank.

FUNCTION_NAME_OR_LISTENER - You can pass in a valid function (Client._VALID_EVENTS), or you can leave this blank and let linelib detect your function name. (Prefix: on_ + Event name)

Handler Parameters

Handlers must be async (coroutine) functions.

parameter type description
ctx? None? Depends The specification depends on the event type.

Depends: ctx? None?

event name argument(s) type description
ready No arguments should be passed.
text ctx TextMessageEvent The text message context.
postback ctx PostbackEvent The postback context.

Client.event - Example

from linelib import Client

client = Client(...)

# method 1
@client.event('ready')
async def ready(): # any name you want
    print("WOW!")

# method 2
@client.event()
async def on_ready(): # on_{EVENT_NAME}
    print("Wow, again -- wow!!")

client.run(...)

Client.request_then

@modifiable(req=dict)
def request_then(self, req: dict) -> Any

This function is overwritable (modifiable). This will be executed once LINELIB receives the webhook event from LINE.

Client.payload_then

@modifiable(ctx=type)
def payload_then(self, ctx) -> Any

This function is overwritable (modifiable). This will be executed once Linelib has got the context.

Note: ready event is NOT included!

Client.run

def Client.run(
    self,
    *args,
    **options: Any
)

Runs the LINE bot.

Client.run - Parameters

parameter type default description
*args Any () Meaningless. This does not do anything here.
**options Any {} Configure run options. See Client Options →

⚙️ Client Options

option type description example
log_level level Sets the logger level of Flask. log_level=logging.ERROR
show_logs bool Show linelib logs? show_logs=False
* Any Unnamed for linelib, only for app.run options. threaded=True

Client.run - Example

from linelib import Client

client = Client(...)

client.run(log_level=logging.ERROR, show_logs=False, threaded=True)

Hello! I am a cute sidebar that makes you to read this whole text with no doubt.

I was born in 1969 and collaborated with Gustavo Fring for my entire life, and my motto is: "Life is a career."

Actually, I have a more important announcement to make besides my profile.

My name is Sustavo Fring, but you can call me Sus.

I am the secondary owner of Los Pollos Hermanos that almost nobody knows.

Gustavo and I created an empire.

I AM THE DANGER.

My name is Walter Hartwell White. I live at-

Clone this wiki locally