As of October 1, 2023, LINE has been rebranded as LY Corporation. Visit the new blog of LY Corporation here: LY Corporation Tech Blog

Blog


Using LINE Notify to send messages to LINE from the command-line

Foreword

Hi, my name is Watanabe and I'm in charge of the development of LINE Notify. In this post, I'd like to share how developers can use LINE Notify to send messages to LINE straight from the command-line.

Up until now sending system messages to LINE either required a Bot API Trial or Business Connect account. While both are improved by Messaging API and provide many great features, they require a high level of implementation.

LINE Notify is an API that has a limited set of features, streamlining the whole process of sending messages to LINE.

Sending messages using cURL

By generating your own "personal access token" through LINE Notify, you can send messages by sending an HTTP POST request to the API endpoint. Any method can be used as long as it uses an HTTP request. In this post, let's go over how you can use the HTTP client cURL.

Generating personal access tokens

You can generate your own personal tokens by navigating to My page (LINE account required).

When you click the "Generate token" button, you will see a screen where you can name your token and select a target that you will send messages to when the token is used.

Use an easily recognizable name for your token. When selecting message targets, selecting "1-on-1 chat with LINE Notify" will make the LINE Notify official account send a message to you. Selecting a group will make the LINE Notify official account send a message to the selected group.

You can change message targets on each token. To send messages to separate chats, you can generate individual tokens for each chat.

Your generated access token will not be shown to you again. You will need to keep the token written down, or else you will need to delete the token and generate a new one.

Sending notifications

Let's try sending a message to LINE by using the personal access token you acquired in the previous step. You can send a message by calling a cURL command as you can see below. Replace [access_token] with your personal access token.

curl -X POST -H 'Authorization: Bearer [access_token]' -F 'message=foobar' https://  
notify-api.line.me/api/notify
  • -X is for assigning request methods
  • -H is for assigning request headers
  • -F is for sending form data

The URL is an endpoint for LINE Notify notifications.

Overall, simple and easy.

Use cases

The previous sections showed you how you can use cURL to easily send messages. Below are some examples.

Example 1: Sending build results to Jenkins

Here's an example of sending build results from Jenkins to LINE. Let's try using Jenkinsfile, which was added in Jenkins 2.0. We'll be using the specs below.

1. Let's send the Jenkins build results as seen below.

Build {branch}, result is {result}.
{buildUrl}

2. If the build fails, let's send an image of Moon laughing and pointing too (Please don't try this at home. This is only meant as a fun example.).

The Jenkinsfile may look really simple, but it's good enough to serve our purposes (The example used is a Java project, so Gradle was used for building and testing).

#!groovy
  
node {
    try {
        stage 'Checkout'
            checkout scm
      
        stage 'Build and test'
            sh './gradlew clean check'
            currentBuild.result = 'SUCCESS'
    } catch (err) {
        currentBuild.result = 'FAILURE'
    }
  
    stage 'Notify'
        notifyLINE('YOUR_LINE_NOTIFY_TOKEN', currentBuild.result)
}
  
def notifyLINE(token, result) {
    def isFailure = result == 'FAILURE'
      
    def url = 'https://notify-api.line.me/api/notify'
    def message = "Build ${env.BRANCH_NAME}, result is ${result}. 
${env.BUILD_URL}"
    def imageThumbnail = isFailure ? 'FAILED_IMAGE_THUMBNAIL' : ''
    def imageFullsize = isFailure ? 'FAILED_IMAGE_FULLSIZE' : ''
      
    sh "curl ${url} -H 'Authorization: Bearer ${token}' -F 'message=${message}'   
    -F 'imageThumbnail=${imageThumbnail}' -F 'imageFullsize=${imageFullsize}'"
}

With this, you will see the following message when your build result is a success.

If your build result is a failure, you will receive the following message...

Simple, right?

Example 2: Using the sl command

An sl command is a command that is triggered when you mistype "ls" as "sl." The command stops all tasks until you fix the typo. True to its name, once the command is triggered you will see a SL (steam locomotive) pass by on your terminal.

Let's try making a SL run across the screen in LINE using LINE Notify. By saving the shell script below in a directory named "sl", you can make an SL pass across the screen in LINE using LINE Notify (You must have your access token and the URL of the image set up properly. The example below also assumes you will use zsh, thus using .zsh_history).

#!/bin/sh
 
ACCESS_TOKEN=...
 
COUNT=$(echo $(perl -lan -e 's/^.+;| .+//g and /^sl$/ and print' ~/.zsh_history | wc -l))
 
for n in 1   4   8; do
    echo "Choo choo"
    curl -s -X POST -H "Authorization: Bearer $ACCESS_TOKEN" -F "message=SL coming 
    through (Total: $COUNT)" -F "imageThumbnail=https://url-to-sl/$n.jpg" -F   
    "imageFullsize=https://url-to-sl/$n.jpg" https://notify-api.line.me/api/notify >  
    /dev/null
    sleep 1
done  

When executed, you will see the screen below.

That easy.

Registering services

Among those of you reading this post, there are probably some developers in charge of nifty web services. LINE Notify has an API that uses OAuth intended for web service providers. You can easily implement a service that can send messages to selected users on LINE by using this API.

You can read more about the details in the development document (Requires login with LINE account).

Closing words

After reading this post, you should be able to use LINE Notify to send messages to LINE. I recommend you try it yourself.