Hi, my name is Hasebe. I’m in charge of the development of LINE Notify.
In a previous post, we introduced how you can use LINE Notify to send messages to LINE from the command-line. Today, I’m going to introduce two features newly implemented in the LINE Notify API. One is sending stickers and the other is uploading images.
The use case demonstrated in the previous post was sending a build result from Jenkins to LINE by using LINE Notify. In that use case, an image of Moon laughing was used to show how it can notify us of a build failure. It was during that time that we came to wonder, “why not make LINE Notify send stickers as well as images?”
We thought that sending stickers would make LINE Notify more “LINE-ish.” That’s why we decided to develop this feature.
Sending stickers from the command-line
Let’s try sending a sticker by using the curl command.
$ curl -X POST https://notify-api.line.me/api/notify -H 'Authorization: Bearer YOUR_PERSONAL_ACCESS_TOKEN' -F 'message=test' -F 'stickerPackageId=1' -F 'stickerId=113'
You can now send stickers in this way.
The old way of sending images with LINE Notify was quite inconvenient. You had to upload the image to a public server and specify the URL of the image.
Since the top priority of LINE Notify is providing greater ease of use for users, we’ve added a feature that can help users upload images more easily. This feature lets users send images directly from servers on a private network!
Please check the API documentation for more detailed information such as which image formats are supported or how many uploads are allowed.
Uploading images from the command-line
Let’s try sending an image using the curl command. Images are sent in a multipart/form-data format.
$ curl -X POST https://notify-api.line.me/api/notify -H 'Authorization: Bearer YOUR_PERSONAL_ACCESS_TOKEN' \ -F 'message=test' \ -F 'imageFile=@/PATH/TO/IMAGE/cony.jpg'
You can now upload and send images using this method. In the next section, let’s explore a few more use cases which you may find useful.
Setting up a motion detection camera bot using Raspberry Pi
I will show you how you can easily set up a bot for a motion detection camera, using an open-source software called Motion and Raspberry PI installed on a home network.
- Raspberry Pi 3
- Raspberry Pi camera module v2
- Raspbian Jessie Lite 2016-09-23
You may use an easy-to-get, small Raspberry Pi server and its official camera module. I used the Raspbian OS version which was the latest version as of October 25.
You may configure common settings like a network setting depending on your environment. To use the camera module with Motion, activate the camera and load the kernel module.
# Enable the camera module to use a camera $ sudo raspi-config # Load the kernel module to run the camera module with V4L2 $ sudo modprobe bcm2835-v4l2 $ echo "bcm2835-v4l2" | sudo tee -a /etc/modules
Install the Motion software using the apt-get command.
$ sudo apt-get install motion # Change the owner to enable writes when Motion is running as a daemon $ chown motion:motion /var/lib/motion
Once the Motion installation is complete, edit motion.conf to allow sending images to LINE Notify when the camera detects any motion.
# The image specifications of LINE Notify is maximum 1024x1024 pixels. width 1024 height 768 # A series of motion detection events that occur within N seconds are considered as a single event. Set the value to around 5 seconds so as not to be alerted too late. event_gap 5 # Save the picture which was taken at the nearest midpoint within a motion detection event. output_pictures center # Movies are not saved. ffmpeg_output_movies off # Send the image to LINE Notify when the image is saved. on_picture_save curl https://notify-api.line.me/api/notify -X POST -H 'Authorization: Bearer YOUR_PERSONAL_ACCESS_TOKEN' -F 'message=!' -F 'imageFile=@%f'
Edit /etc/defaults/motion to run Motion as a daemon.
start_motion_daemon=yes # Change "no" to "yes"
Do as follows to start or stop Motion.
$ sudo service motion start # Start Motion $ sudo service motion stop # Stop Motion $ sudo update-rc.d motion defaults # Autorun Motion when Raspberry Pi is run
I set up the camera on the door of my refrigerator as I couldn’t find a better target to keep my eye on. The camera detects when the refrigerator door is open, takes a picture of the inside of the refrigerator and sends the picture to LINE.
Below is the picture that LINE Notify sent to me a few seconds later I opened the door.
It took me only about 10 minutes to set the configuration file and start running the camera bot. (I spent an entire hour cleaning my refrigerator for the photo shoot though.)
Protecting your Mac computer from unauthorized use by taking a picture
Let’s assume that someone logged on to your Mac computer without your permission. If your computer can take a picture of the person and send to LINE, it will help catch the person.
Unlike the use case of Raspberry Pi that I explained earlier, you can make that using only your Mac computer.
First, you need to set up a login hook with a Mac computer. A login hook triggers the execution of a pre-defined script when someone logs in.
sudo defaults write com.apple.loginwindow LoginHook /path/to/login_hook.sh
Write a script as follows in login_hook.sh.
#!/bin/sh at -f "/path/to/notify.sh" now+10sec
This will make the at command execute the notify.sh script after 10 seconds. The reason for doing this is that the camera shooting described below takes a few seconds to work. Also, the at command may not work in some environments. In that case, use the following command to activate it.
sudo launchctl load -F /System/Library/LaunchDaemons/com.apple.atrun.plist
Write as follows in notify.sh.
#!/bin/sh ACCESS_TOKEN="YOUR_PERSONAL_ACCESS_TOKEN" PICTURE_PATH="/PATH/TO/PICTURE/login_camera.jpg" # Take pictures using ImageSnap. /usr/local/bin/imagesnap -w 2 -q $PICTURE_PATH curl -X POST https://notify-api.line.me/api/notify -H "Authorization: Bearer $ACCESS_TOKEN" -F "message=login" -F "imageFile=@$PICTURE_PATH" > /dev/null 2>&1
Install ImageSnap if it has not been installed.
brew install imagesnap
When someone logs on to your Mac computer, a picture is taken and sent to you as shown below. (The picture is pixelated to be posted on the blog.)
I tested the bot for about a week. Well, as you can probably guess, all I received was the photos of myself so I had no choice but to turn it off.
In this post, I introduced the new features of LINE Notify – sending stickers and uploading images. Please see the API documentation for more detailed specifications. What’s even better is that the API documentation which used to be in a PDF file is now available in an HTML format. I encourage all of you to give it a try!