linebot package

linebot.api module

linebot.api module.

class linebot.api.LineBotApi(channel_access_token, endpoint=u'https://api.line.me', timeout=5, http_client=<class 'linebot.http_client.RequestsHttpClient'>)

Bases: object

LineBotApi provides interface for LINE messaging API.

__init__ method.

Parameters:
  • channel_access_token (str) – Your channel access token
  • endpoint (str) – (optional) Default is https://api.line.me
  • timeout (float | tuple(float, float)) – (optional) How long to wait for the server to send data before giving up, as a float, or a (connect timeout, readtimeout) float tuple. Default is linebot.http_client.HttpClient.DEFAULT_TIMEOUT
  • http_client (T <= linebot.http_client.HttpClient) – (optional) Default is linebot.http_client.RequestsHttpClient
DEFAULT_API_ENDPOINT = u'https://api.line.me'
get_message_content(message_id, timeout=None)

Call get content API.

https://devdocs.line.me/en/#get-content

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

Parameters:
  • message_id (str) – Message ID
  • timeout (float | tuple(float, float)) – (optional) How long to wait for the server to send data before giving up, as a float, or a (connect timeout, readtimeout) float tuple. Default is self.http_client.timeout
Return type:

linebot.models.responses.MessageContent

Returns:

MessageContent instance

get_profile(user_id, timeout=None)

Call get profile API.

https://devdocs.line.me/en/#bot-api-get-profile

Get user profile information.

Parameters:
  • user_id (str) – User ID
  • timeout (float | tuple(float, float)) – (optional) How long to wait for the server to send data before giving up, as a float, or a (connect timeout, readtimeout) float tuple. Default is self.http_client.timeout
Return type:

linebot.models.responses.Profile

Returns:

Profile instance

leave_group(group_id, timeout=None)

Call leave group API.

https://devdocs.line.me/en/#leave

Leave a group.

Parameters:
  • group_id (str) – Group ID
  • timeout (float | tuple(float, float)) – (optional) How long to wait for the server to send data before giving up, as a float, or a (connect timeout, readtimeout) float tuple. Default is self.http_client.timeout
leave_room(room_id, timeout=None)

Call leave room API.

https://devdocs.line.me/en/#leave

Leave a room.

Parameters:
  • room_id (str) – Room ID
  • timeout (float | tuple(float, float)) – (optional) How long to wait for the server to send data before giving up, as a float, or a (connect timeout, readtimeout) float tuple. Default is self.http_client.timeout
multicast(to, messages, timeout=None)

Call multicast API.

https://devdocs.line.me/en/#multicast

Send messages to multiple users at any time.

Parameters:
  • to (list[str]) – IDs of the receivers Max: 150 users
  • messages (T <= linebot.models.send_messages.SendMessage | list[T <= linebot.models.send_messages.SendMessage]) – Messages. Max: 5
  • timeout (float | tuple(float, float)) – (optional) How long to wait for the server to send data before giving up, as a float, or a (connect timeout, readtimeout) float tuple. Default is self.http_client.timeout
push_message(to, messages, timeout=None)

Call push message API.

https://devdocs.line.me/en/#push-message

Send messages to users, groups, and rooms at any time.

Parameters:
reply_message(reply_token, messages, timeout=None)

Call reply message API.

https://devdocs.line.me/en/#reply-message

Respond to events from users, groups, and rooms.

Webhooks are used to notify you when an event occurs. For events that you can respond to, a replyToken is issued for replying to messages.

Because the replyToken becomes invalid after a certain period of time, responses should be sent as soon as a message is received.

Reply tokens can only be used once.

Parameters:
  • reply_token (str) – replyToken received via webhook
  • messages (T <= linebot.models.send_messages.SendMessage | list[T <= linebot.models.send_messages.SendMessage]) – Messages. Max: 5
  • timeout (float | tuple(float, float)) – (optional) How long to wait for the server to send data before giving up, as a float, or a (connect timeout, readtimeout) float tuple. Default is self.http_client.timeout

linebot.webhook module

linebot.http_client webhook.

class linebot.webhook.SignatureValidator(channel_secret)

Bases: object

Signature validator.

https://devdocs.line.me/en/#webhook-authentication

__init__ method.

Parameters:channel_secret (str) – Channel secret (as text)
validate(body, signature)

Check signature.

https://devdocs.line.me/en/#webhook-authentication

Parameters:
  • body (str) – Request body (as text)
  • signature (str) – X-Line-Signature value (as text)
Return type:

bool

Returns:

result

class linebot.webhook.WebhookHandler(channel_secret)

Bases: object

Webhook Handler.

__init__ method.

Parameters:channel_secret (str) – Channel secret (as text)
add(event, message=None)

[Decorator] Add handler method.

Parameters:
  • event (T <= linebot.models.events.Event class) – Specify a kind of Event which you want to handle
  • message – (optional) If event is MessageEvent, specify kind of Messages which you want to handle
Type:

message: T <= linebot.models.messages.Message class

Return type:

func

Returns:

decorator

default()

[Decorator] Set default handler method.

Return type:func
Returns:
handle(body, signature)

Handle webhook.

Parameters:
  • body (str) – Webhook request body (as text)
  • signature (str) – X-Line-Signature value (as text)
class linebot.webhook.WebhookParser(channel_secret)

Bases: object

Webhook Parser.

__init__ method.

Parameters:channel_secret (str) – Channel secret (as text)
parse(body, signature)

Parse webhook request body as text.

Parameters:
  • body (str) – Webhook request body (as text)
  • signature (str) – X-Line-Signature value (as text)
Return type:

list[T <= linebot.models.events.Event]

Returns:

linebot.webhook.compare_digest(val1, val2)

compare_digest function.

If hmac module has compare_digest function, use it. Or not, use linebot.utils.safe_compare_digest.

Parameters:
  • val1 (str | bytes) – string or bytes for compare
  • val2 (str | bytes) – string or bytes for compare
Return type:

bool

Returns:

result

linebot.exceptions module

linebot.exceptions module.

exception linebot.exceptions.BaseError(message=u'-')

Bases: exceptions.Exception

Base Exception class.

__init__ method.

Parameters:message (str) – Human readable message
exception linebot.exceptions.InvalidSignatureError(message=u'-')

Bases: linebot.exceptions.BaseError

When Webhook signature does NOT match, this error will be raised.

__init__ method.

Parameters:message (str) – Human readable message
exception linebot.exceptions.LineBotApiError(status_code, error=None)

Bases: linebot.exceptions.BaseError

When LINE Messaging API response error, this error will be raised.

__init__ method.

Parameters:

linebot.http_client module

linebot.http_client module.

class linebot.http_client.HttpClient(timeout=5)

Bases: object

Abstract Base Classes of HttpClient.

__init__ method.

Parameters:timeout (float | tuple(float, float)) – (optional) How long to wait for the server to send data before giving up, as a float, or a (connect timeout, readtimeout) float tuple. Default is DEFAULT_TIMEOUT
Return type:T <= HttpResponse
Returns:HttpResponse instance
DEFAULT_TIMEOUT = 5
get(url, headers=None, params=None, stream=False, timeout=None)

GET request.

Parameters:
  • url (str) – Request url
  • headers (dict) – (optional) Request headers
  • params (dict) – (optional) Request query parameter
  • stream (bool) – (optional) get content as stream
  • timeout (float | tuple(float, float)) – (optional), How long to wait for the server to send data before giving up, as a float, or a (connect timeout, readtimeout) float tuple. Default is self.timeout
Return type:

T <= HttpResponse

Returns:

HttpResponse instance

post(url, headers=None, data=None, timeout=None)

POST request.

Parameters:
  • url (str) – Request url
  • headers (dict) – (optional) Request headers
  • data – (optional) Dictionary, bytes, or file-like object to send in the body
  • timeout (float | tuple(float, float)) – (optional), How long to wait for the server to send data before giving up, as a float, or a (connect timeout, readtimeout) float tuple. Default is self.timeout
Return type:

T <= HttpResponse

Returns:

HttpResponse instance

class linebot.http_client.HttpResponse

Bases: object

HttpResponse.

content

Get request body as binary.

headers

Get headers.

iter_content(chunk_size=1024, decode_unicode=False)

Get request body as iterator content (stream).

Parameters:
  • chunk_size (int) –
  • decode_unicode (bool) –
json

Get request body as json-decoded.

status_code

Get status code.

text

Get request body as text-decoded.

class linebot.http_client.RequestsHttpClient(timeout=5)

Bases: linebot.http_client.HttpClient

HttpClient implemented by requests.

__init__ method.

Parameters:timeout (float | tuple(float, float)) – (optional) How long to wait for the server to send data before giving up, as a float, or a (connect timeout, readtimeout) float tuple. Default is DEFAULT_TIMEOUT
get(url, headers=None, params=None, stream=False, timeout=None)

GET request.

Parameters:
  • url (str) – Request url
  • headers (dict) – (optional) Request headers
  • params (dict) – (optional) Request query parameter
  • stream (bool) – (optional) get content as stream
  • timeout (float | tuple(float, float)) – (optional), How long to wait for the server to send data before giving up, as a float, or a (connect timeout, readtimeout) float tuple. Default is self.timeout
Return type:

RequestsHttpResponse

Returns:

RequestsHttpResponse instance

post(url, headers=None, data=None, timeout=None)

POST request.

Parameters:
  • url (str) – Request url
  • headers (dict) – (optional) Request headers
  • data – (optional) Dictionary, bytes, or file-like object to send in the body
  • timeout (float | tuple(float, float)) – (optional), How long to wait for the server to send data before giving up, as a float, or a (connect timeout, readtimeout) float tuple. Default is self.timeout
Return type:

RequestsHttpResponse

Returns:

RequestsHttpResponse instance

class linebot.http_client.RequestsHttpResponse(response)

Bases: linebot.http_client.HttpResponse

HttpResponse implemented by requests lib’s response.

__init__ method.

Parameters:response – requests lib’s response
content

Get request body as binary.

headers

Get headers.

iter_content(chunk_size=1024, decode_unicode=False)

Get request body as iterator content (stream).

Parameters:
  • chunk_size (int) –
  • decode_unicode (bool) –
json

Get request body as json-decoded.

status_code

Get status code.

text

Get request body as text-decoded.

linebot.utils module

linebot.http_client module.

linebot.utils.safe_compare_digest(val1, val2)

safe_compare_digest method.

Parameters:
  • val1 (str | bytes) – string or bytes for compare
  • val2 (str | bytes) – string or bytes for compare
linebot.utils.to_camel_case(text)

Convert to camel case.

Parameters:text (str) –
Return type:str
Returns:
linebot.utils.to_snake_case(text)

Convert to snake case.

Parameters:text (str) –
Return type:str
Returns: