0
0

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 3 years have passed since last update.

SlackAPIでのメッセージ送信でのメッセージ上限を調べてみた

Last updated at Posted at 2020-07-03

前提条件

Blocks UIを使った場合なので、その場合以外は検証はしてません。詳しくは一番下の検証ソースのcreate_send_messageのとこ見てください。

経緯

AWSのLambdaでSlack通知をしたいなと思った時に、400: Bad Requestなエラーが出た。送る文字数を少ない時は送信できてたので調査してみる。どうでもいいけどSlackはもうちょっと親切なエラーメッセージ返して欲しい。


Response:
{
  "errorMessage": "HTTP Error 400: Bad Request",
  "errorType": "HTTPError",
  "stackTrace": [
    "  File \"/var/task/lambda_function.py\", line 57, in lambda_handler\n    post_slack(create_send_message(test_message))\n",
    "  File \"/var/task/lambda_function.py\", line 236, in post_slack\n    with urllib.request.urlopen(request) as response:\n",
    "  File \"/var/lang/lib/python3.8/urllib/request.py\", line 222, in urlopen\n    return opener.open(url, data, timeout)\n",
    "  File \"/var/lang/lib/python3.8/urllib/request.py\", line 531, in open\n    response = meth(req, response)\n",
    "  File \"/var/lang/lib/python3.8/urllib/request.py\", line 640, in http_response\n    response = self.parent.error(\n",
    "  File \"/var/lang/lib/python3.8/urllib/request.py\", line 569, in error\n    return self._call_chain(*args)\n",
    "  File \"/var/lang/lib/python3.8/urllib/request.py\", line 502, in _call_chain\n    result = func(*args)\n",
    "  File \"/var/lang/lib/python3.8/urllib/request.py\", line 649, in http_error_default\n    raise HTTPError(req.full_url, code, msg, hdrs, fp)\n"
  ]
}

結論

メッセージ上限は3000文字だ!

参考

検証ソースです。AWS Lambda上で実施したものを適当に抜粋したので動かないかもしれません。


import json
import urllib
import os

TEXT_LEN = 3000 # ここを適当に変える

def lambda_handler(event, context):
    test_message= 'A' * TEXT_LEN
    post_slack(create_send_message(test_message))

    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

def create_send_message(message):
    return {
        "blocks": [
            {
                "type": "section",
                "text": {
                    "type": "mrkdwn",
                    "text": f"{message}"
                }
            }
        ]
    }

def post_slack(send_data):
    request = urllib.request.Request(
        os.environ['SLACK_WEBHOOK_URL_DEV'], 
        data=json.dumps(send_data).encode("utf-8"),
        method="POST"
    )
    with urllib.request.urlopen(request) as response:
        response_body = response.read().decode("utf-8")
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?