Workshop chatbot with LINE - up1/workshop-n8n GitHub Wiki

Workshop chatbot with LINE

If node :: to check type of event = event

{{ $json.body.events[0].type }}

Result

[
    {
    "text": "Hello",
    "replyToken": "d6a242a2b80f48fb9ac66adedf35f5d7"
    }
]

Edit field node

[
    {
        "chatInput": "Hi",
        "replyToken": "a3169e5ae5a1449aaea4e0aedc58afae"
    }
]

Code Node :: Parse response from LINE Message API

let requests
let message = {}
for (const item of $input.all()) {
  requests = item.json.body
}
if ('events' in requests) {
  if ('message' in requests['events'][0]) {
    if (requests['events'][0]['message']['type'] == 'text' &&
        'text' in requests['events'][0]['message']) {
      message = {
        'text': requests['events'][0]['message']['text'],
        'replyToken': requests['events'][0]['replyToken']
      }
    }
  }
}
return message

Reply to LINE with Http request node

HTTP Header

  • Content-Type=application/json
  • Authorization=BEARER

HTTP Body

{
  "replyToken": "{{$node["Code"].json["replyToken"]}}",
  "messages": [
    {
      "type": "text",
      "text": "{{$node["Code"].json["text"]}}"
    }
  ]
}
⚠️ **GitHub.com Fallback** ⚠️