4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Line Notifyでメッセージサイズが上限(1000文字)を超えていたら分割して送信する(python3)

Posted at

背景

LINE Notify通知が送信文字数上限(1000文字)超えでエラーになっていたので分割送信するようにしました。

実装

AWS Lambda(Python3.6)を利用していますが、どこでも動作すると思います。

import requests

MESSAGE_SIZE_LIMIT = 1000
LINE_NOTIFY_URL = "https://notify-api.line.me/api/notify"
LINE_NOTIFY_TOKEN = "your-token"

def line_notify(long_message):
    headers = {"Authorization" : "Bearer "+ LINE_NOTIFY_TOKEN}
    message_length = len(long_message)
    i = 0
    while message_length > 0:
        if i == 0:
            buf = long_message[:MESSAGE_SIZE_LIMIT]
        else:
            buf = long_message[MESSAGE_SIZE_LIMIT * i:MESSAGE_SIZE_LIMIT * (i+1)]
        payload = {"message" :  buf}
        i += 1
        message_length -= MESSAGE_SIZE_LIMIT

        r = requests.post(LINE_NOTIFY_URL, headers = headers, params = payload)

        if r.status_code != 200 :
              pass # エラー処理

関連URL

公式ドキュメント

4
3
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
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?