Skip to content

osk2/line-bot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

26 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

line-bot

hacs_badge

็น้ซ”ไธญๆ–‡ | English

LINE bot plugin for Home Assistant

This project is modified from yun-s-oh/Homeassistant

โš  Note

This component is for LINE bot

If you plan to integrate LINE Notify, use yun-s-oh's component instead

Installation

via HACS

Install line-bot via HACS is recommended

HACS > Integrations > 3 dots menu (top right) > Custom repositories > URL: osk2/line-bot > Category: Integration

Manually

Copy custom_components/line_bot to custom_components

Configuration

Add following entry in configuration.yaml

notify:
  - name: line_bot
    platform: line_bot
    client_id: 'CLIENT_ID'
    access_token: 'CHANNEL_ACCESS_TOKEN'

See Additional Information for detail of retrieving client_id and access_token

Add more profile via UI

A profile means a set of client_id and access_token.

You can add more notify service by repeating above steps. You can also create profile via UI to simplify the process

  1. Configuration > Integration > Add Integration > LINE Bot
  2. Enter profile name (different to entry name in configuration.yaml), client_id and access_token.

See Change Profile to learn how to use profile

Usage

Passing LINE message object into service

service: notify.line_bot
data:
  message: >-
    {"messages":[{"type": "text", "text": "Hello, world"}]}

Just in case you are too lazy to pass full object, plain text is also supported

service: notify.line_bot
data:
  message: 'Hello, world'

Change profile

service: notify.line_bot
data:
  message: >-
    {"messages":[{"type": "text", "text": "Hello, world"}]}
  data:
    profile: cool_line_bot # Profile name

See Additional Information for detail of LINE Message Object

Additional Information

client_id

client_id is LINE user ID or group ID

Retrieve client_id can be tricky, here's how I get client_id

  1. Create Firebase Cloud Functions
  2. Deploy following script to Cloud Functions
const functions = require('firebase-functions')

exports.helloWorld = functions.https.onRequest((request, response) => {
  const events = request.body.events
  const source = events.length > 0 ? events[0].source : null

  if (source) {
    functions.logger.info(source.groupId || source.userId)
  }
  response.send('Hello from Firebase!')
})
  1. Enable webhook for LINE Messaging API image
  2. Friend bot account or invite bot to your group chat
  3. You should be able to see client_id in Cloud Functions log after sending some nice message to your bot image
  4. Disable webhook again or your log will be flooded

access_token

access_token is channel access token which can be generate from LINE Developer website

Visit https://developers.line.biz/console/channel/<YOUR CHANNEL ID>/messaging-api

The token is listed under Channel access token or your can create one there image

LINE Message Object

This component supports all kinds of message types that are listed in Messaging API reference

Text message example

{
  "messages": [
    {
      "type": "text",
      "text": "Hello, world"
    }
  ]
}

Flex message example

{
  "type": "flex",
  "altText": "this is a flex message",
  "contents": {
    "type": "bubble",
    "body": {
      "type": "box",
      "layout": "vertical",
      "contents": [
        {
          "type": "text",
          "text": "hello"
        },
        {
          "type": "text",
          "text": "world"
        }
      ]
    }
  }
}

See Also

  1. Flex Message Simulator to help you build flex message object
  2. #ๆ•™ๅญธ ๆ‰“้€ ไฝ ็š„ๆ™บๆ…งๅฎถๅบญๅง! ๆŠŠ LINE ๆ้†’่ฎŠๅพ—ๆ›ดๆœ‰ๅž‹ (Thanks Jason Lee ๐Ÿ‘)

License

The project is licensed under MIT License.

See LICENSE for detailed infomation.