Integrating LINE Message and Zapier
Tags: python, zapier, line, gmail
Published: 2020-06-16 06:30:48

The most common text messaging service used in Japan is by far [LINE](https://line.me/en-US/). I have been using LINE for some time now with [Zapier](https://zapier.com) to get notifications directly on my phone without needing to install other services.

This will be a brief guide aimed at those that are already familiar with Zapier and LINE and would like to bring the two together.

For those that are less familiar. I would direct your attention to this [link](https://gmailzapierlinenotify.readthedocs.io/) where I have some additional details.

Prerequisites:
  - Gmail
  - Zapier Account
  - LINE Messenger

Gmail:

First setup a filter in Gmail which adds a label to any message that you would like to have trigger a LINE message using Zapier.
There are several good guides on doing this.
Google provides a guide [here](https://support.google.com/mail/answer/6579?hl=en)

For the purposes of this guide I will assume you are using 'ZapierLine' as the label.

Next we will setup LINE

Step 1:

Log into the site [here](https://notify-bot.line.me/en/). This supports English, Japanese and a few other languages. Note: You will need to have setup your email address and password in advance using the mobile app.

Step 2:

Click on the 'My page'

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2mabaeo87tcjc7493mak.png)

Step 3:
Click on 'Generate token'

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gx6ks2vwxlpt4jo3yb25.png)

Step 4:
Provide a name for token. In this case I have provided 'ZapierDemo'. This will appear on your phone as the sender of the notification.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/1d6vqa3qcsylg7lc4j6i.png)

Step 5:
Choose if you want to send notifications only to yourself using '1-on-1 chat with LINE Notify' or choose an existing chat group you have created within the LINE App on your device.

Here I have chosen '1-on-1'

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gr6qw6an7osuy4ed40b2.png)

Step 6:
Copy the generated token and set save it in a safe place as you will see this once.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/iosnauqhgz42xp22qaos.png)

Zapier:

From here we will create a Zap which will be triggered when an email arrives in your account and has been labeled 'ZapierLine' by the filter you previously created.

Step 1:
Log in to Zapier and click 'MAKE A ZAP' or 'Create Zap'

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/3941ya5eufj907aqi7pb.png)

Step 2:
Give your Zap a name. I have used 'ZapierDemo'
If you don't see Gmail in the app list, enter it in the search as I have done.
Click on 'Gmail'

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/h1a6z2go3zq34myohjz1.png)

Step 3:
Choose your 'Trigger Event'
You should choose 'New Thread'

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/i2l6wcb2t09ioinspd3d.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jea821mt33u1auolb629.png)

Step 4:
Click 'Continue'

Step 5:
Select your connected Gmail account. This will tell Zapier which gmail account to monitor.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/42lzl8gfj8htygb55jtp.png)
Click 'Continue'

Step 6:
Select the correct Gmail Label. In this case we are using 'ZapierLine'

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ywzo0mta0get25fwrtf2.png)
Click 'Continue'

Step 7:
Next we need to 'Test your Trigger'. So click this button.

This will now look for any messages that contain the label we have provided. 'ZapierLine'

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5oi6vx5ou8o5ahbdch4w.png)
If you are happy with the result. Click 'Continue'

Step 8:
Enter 'Code by Zapier' and click on 'Code by Zapier'

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/ndlfnmo7qq5tltb3lo5r.png)
Step 9:
For 'Choose Action Event' select 'Run Python'

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/llr6kxhc0jwiwb0m8qiw.png)

Click 'Continue'

Step 10:

At this point we will start selecting what information we want to extract from the email.
This is of course is optional. If you wish you could simply have a notification sent to your line account letting you know to check your email.

However in this case we will collect some data to add to the notification.

I will extract the following details. from, subject and body. These names are provided manually.

I will then select from the drop down the actual items I am looking for.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tsoisk3jap4lhdmpl0qd.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/o3zxy8wawzwtjp007q5k.png)

Next we need to enter our code in the code window.

This code simply takes the plain text body of the email and sends it in the notification.

Sample 1
```py
   import requests
   API_URI = "https://notify-api.line.me/api/notify"

   text_message = input.get('body')
   TOKEN = "YOUR_GENERATED_TOKEN_GOES_HERE"
   HEADER = {"Authorization": "Bearer " + TOKEN}
   PARAMS = {"message": text_message}

   response = requests.post(API_URI, headers=HEADER, params=PARAMS)
   response.raise_for_status()
   return {'rawHtml': response.text}
```

Sample 2:
This code sends a different text message depending on the from address of the sender.

```py
  import requests
   API_URI = "https://notify-api.line.me/api/notify"

   if input.get('from') == 'AddressA':
      text_message = "This is the message from sender A"
   if input.get('from') == 'AddressB':
      text_message = "This is the message from sender B with email body included:\n\n{}".format(input.get('body'))

   TOKEN = "YOUR_TOKEN_GOES_HERE"
   HEADER = {"Authorization": "Bearer " + TOKEN}
   PARAMS = {"message": text_message}

   response = requests.post(API_URI, headers=HEADER, params=PARAMS)
   response.raise_for_status()
   return {'rawHtml': response.text}
```

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6l2l2orihqbrkxbx113w.png)

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/elmmm7fj4mhi0dpa7c0c.png)

NOTE: If you get an error message that execution took more than 1 second. This can be ignored.
Simply Click the 'Skip Test' in the upper right corner.

Step 11:
Activate your Zap
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/oa6l2uc4cs3t33usr120.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/fgxn4y0t7llirx2o7icj.png)

known Issues:
*Zapier now only runs every 15 minutes for new free accounts.
*There is a 1 second runtime limit. You may get a warning if your code takes longer to execute.


These limits can be removed by upgrading your service.