LoginSignup
4
2

More than 5 years have passed since last update.

LINE Messaging APIのPush Messageを使ってみる

Posted at

LINEのAPIでボットをつくるためにReplyMessageを使う例をよく見かけるのですが、PushMessageを使った例があまり見つからなかったので、メモとして残しておきます。

使用言語 : Python3.5

app.py
import os
import requests
import json

LINE_CHANNEL_SECRET = os.getenv('LINE_CHANNEL_SECRET')
LINE_CHANNEL_ACCESS_TOKEN = os.getenv('LINE_CHANNEL_ACCESS_TOKEN')

def push_message():
    endpoint = 'https://api.line.me/v2/bot/message/push'

    header = {
        'Content-Type': 'application/json',
        'Authorization': 'Bearer {' + LINE_CHANNEL_ACCESS_TOKEN + '}'
    }

    payload = {
        # toには32桁のuserIdをいれる。
        'to': 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
        'messages': [
            {
                'type': 'text',
                'text': 'Hello LINE'
            }
        ]
    }
    payload = json.dumps(payload)

    r = requests.post(url=endpoint, headers=header, data=payload)
    print(r.status_code)

if __name__ == '__main__':
    push_message()

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2