Nerd For Tech
Published in

Nerd For Tech

Echo messages from LINE chat to Slack

In our case, we have a Line group and a Slack channel. Both of them are discussing similar things but with different people. In order to reduce efforts on sending the same messages on both sides, we plan to utilize LINE’s messaging API, connecting it with Slack’s incoming webhook.

The overall process can be broken down to the following steps:

  1. Create an Incoming Webhook on Slack
  2. Create a LINE echo bot
  3. Pass messages to Slack channel whenever the bot receives a message

Create an Incoming Webhook on Slack

There are many nice tutorials on Youtube guiding us on setting up incoming webhooks. The main goal here is to derive a Webhook Url.

We can later open the terminal and use curl to see whether the url works:

$ curl -X POST -H 'Content-type: application/json' --data '{"text":"It is rainy today"}'  https://hooks.slack.com/your-webhook-url
Screenshot from slack

Create a LINE echo bot

In order to use LINE’s messaging API, it is required to register ourselves as LINE developers and create a provider. Here is the step by step instructions. In the end, we can add the bot by QR code:

After having a bot, please edit the settings and invite the bot to the chatroom where we want it to ❌ spy ✅ forward messages.

Pass messages to Slack channel whenever the bot receives a message

Now, we will use Node.js to write the message passing part. Create a new folder and download index.js, package-lock.json, and package.json from the following github link:

To setup the configuration easily, we will create a file named .env in the same folder. We then declare our channel_secret, channel_access_token, and intended port.

We also need to install the some packages. We can type the following commands in terminal:

npm install              # download line-sdk listed in package.json
npm install dotenv # for enabling the file .env
npm install node-fetch # for posting api

Great 👊 Let’s rewrite codes in index.js for posting messages to slack whenever the LINE bot receives a message:

Last step is to run this Node.js server in a place where we can get a public url (It must use https. LINE’s webhook does not accept http 😧) You choose between:

  • export your localhost ip using ngrok.
  • use firebase function, which is highly recommended since it is easy and fast to setup for production.

In my case, I run it on Google Cloud Platform, where I already setup an apache server to host another website. (Yes, running node.js server and apache server on the same machine was such an arduous work 🙀 I’ve shared my experience in this post 👉 Run Node.js application on Apache server)

In either method, you may start your node.js server by typing this command in the terminal:

node .

Then verify your webhook in LINE’s developer console with your public url (Mine is the GCP machine):

TADA! That’s all 🎊🎊🎊 Now all the messages received by your LINE bot, either texts you directly send to it or those it eavesdrops in the group’s chatroom, will be sent to our slack channel 🙌

--

--

--

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Recommended from Medium

A Step Towards Simplified Querying in NodeJS

Node.js | Inspecting an Apollo.js Server

Abstract: Out of all the JavaScript libraries, ReactJS has proven to be the best front end library.

Phase 0: Project

CodeceptJS — part II — Automation Framework

Beginner’s Guide to Redux vs React Context

JavaScript: Reverse Linked List

Multiple environments in Netlify functions

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Wendee

Wendee

💗 cooking, coding, and sharing (^o^)

More from Medium

How to create a static website using HUGO on Heroku

Google less secure app access is going to stop — not an issue

How to Integrate Razorpay Payment Gateway with WordPress Website

Heroku/Git pipeline Deployment Fails (504, Internal Server Error)