背景
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 # エラー処理