Send a message to LINE with Python (LINE Notify)

Introduction

A bot memorandum that uses LINE Notify in Python and sends messages to LINE.

The procedure is as follows.

  1. Create a LINE account and register for LINE Notify services
  2. Send a message

environment

To use LINE Notify

What is LINE Notify?

The LINE Notify official website has the following explanation.

When you link with the web service, you will receive a notification from the official account "LINE Notify" provided by LINE. It is possible to link with multiple services and receive notifications even in groups.

By linking with LINE Notify, LINE users will be able to easily receive service notifications.

Create a LINE account and register for a LINE Notify service

Create a LINE account

I will omit it.

LINE Notify service registration

  1. After creating an account, go to the LINE Notify Official Site and log in with your LINE account. At that time, you will be logged in with the email address registered in your LINE account, so you need to register in advance.
  2. From the LINE Notify Registration Service Management Page, enter the necessary information such as the service name and service outline to register the service.
  3. From LINE Notify My Page, specify the token name and the talk room to send the notification, and get the access token information. After getting the access token, LINE Notify will be registered as a friend of the talk room.

Please refer to the following site for e-mail address registration.

Send a message using LINE Notify

import requests

url = "https://notify-api.line.me/api/notify"
access_token = 'xxxxxxxxxxxxxxxx'


class LineNotify:
    
    def __init__(self, url, access_token):
        self.url = url
        self.headers = {'Authorization': 'Bearer ' + access_token}

    def send_message(self, message, image=None, sticker_package_id=None, sticker_id=None):
        
        payload = {
                'message': message, 
                'stickerPackageId': sticker_package_id, 
                'stickerId': sticker_id
                }
        
        files = {}
        
        if image != None:
            files = {'imageFile': open(image, 'rb')}
            
        r = requests.post(
                self.url, 
                headers=self.headers, 
                data=payload, 
                files=files
                )


##Main
if __name__ == '__main__':
    
    bot = LineNotify(url, access_token)

    bot.send_message('This message is a test.')

Transmission result

image.png

Recommended Posts

Send a message to LINE with Python (LINE Notify)
How to install NPI + send a message to line with python
How to send a message to LINE with curl
Send a message from Python to Slack
I want to send a message from Python to LINE Bot
[Python] How to draw a line graph with Matplotlib
Post a message to Google Hangouts Chat with a thread (Python)
Create a message corresponding to localization with python translation string
HTML email with image to send with python
How to send a request to the DMM (FANZA) API with python
[Yahoo! Weather Replacement Version] How to get weather information with LINE Notify + Python
Python beginners decided to make a LINE bot with Flask (Flask rough commentary)
Send an email to Spushi's address with python
Create a LINE BOT with Minette for Python
Send an email to a specific email address with python without SMTP settings
Try to draw a life curve with python
I want to make a game with Python
Send mail with mailx to a dummy SMTP server set up with python.
Try to make a "cryptanalysis" cipher with Python
Decide to assign a laboratory with Python (fiction)
How to draw a vertical line on a heatmap drawn with Python seaborn
Sample to send slack notification with python lambda
Steps to create a Twitter bot with python
How to notify a Discord channel in Python
Try to make a dihedral group with Python
Send email with Python
Read line by line from a file with Python
I want to write to a file with Python
[Python] Throw a message to the slack channel
A layman wants to get started with Python
[LINE Messaging API] I want to send a message from the program to everyone's LINE
Create a setting in terraform to send a message from AWS Lambda Python3.8 to Slack
I tried to automatically send the literature of the new coronavirus to LINE with Python
How to convert / restore a string with [] in python
A memo connected to HiveServer2 of EMR with python
Try to make a command standby tool with python
Python Ver. To introduce WebPay with a little code.
I tried to draw a route map with Python
I want to work with a robot in python.
From buying a computer to running a program with python
I made a LINE BOT with Python and Heroku
I tried to automatically generate a password with Python3
[Python] A memo to write CSV vertically with Pandas
A program to write Lattice Hinge with Rhinoceros with Python
[Python] How to create a 2D histogram with Matplotlib
[Super easy] Let's make a LINE BOT with Python.
I want to run a quantum computer with Python
[Python] How to draw a scatter plot with Matplotlib
[Python] Road to a snake charmer (5) Play with Matplotlib
Connect to BigQuery with Python
Send Japanese email with Python3
A road to intermediate Python
Connect to Wikipedia with Python
Post to slack with Python 3
Send a signal to subprocess
Try LINE Notify in Python
Make a fortune with Python
Switch python to 2.7 with alternatives
Write to csv with Python
Create a directory with python
With skype, notify with skype from python!