【python】Slack APIのchat.postMessageでattachmentsを送る方法

python

attachmentsで送ると「invalid_attachments」

Block Kit Builderでattachmentsを作成し、chat.postMessageで送信したら上記のエラーが発生。
textやblocksの時は送れたのに、なんでだー。

Got an error: invalid_attachments

いろいろ調べた結果「json.dumps」を使うことで解決。

Block Kit Builderでデフォルトで表示される以下の画面から「Attachment Preview」をリストから選択した後、右上の「Copy Payload」でコードを貼り付けて送信できた。

import asyncio
import os
import json
from slack_sdk.web.async_client import AsyncWebClient
from slack_sdk.errors import SlackApiError

# SLACK_BOT_TOKEN,POST_CHANNELは別途設定してね

# slack通知処理
client = AsyncWebClient(token=os.environ['SLACK_BOT_TOKEN'])

async def post_message():
    try:
        response = await client.chat_postMessage(
            channel=os.environ['POST_CHANNEL'], 
            attachments = json.dumps([
                {
                    "color": "#f2c744",
                    "blocks": [
                        {
                            "type": "section",
                            "text": {
                                "type": "mrkdwn",
                                "text": "Hello, Assistant to the Regional Manager Dwight! *Michael Scott* wants to know where you'd like to take the Paper Company investors to dinner tonight.\n\n *Please select a restaurant:*"
                            }
                        },
                        {
                            "type": "divider"
                        },
                        {
                            "type": "section",
                            "text": {
                                "type": "mrkdwn",
                                "text": "*Farmhouse Thai Cuisine*\n:star::star::star::star: 1528 reviews\n They do have some vegan options, like the roti and curry, plus they have a ton of salad stuff and noodles can be ordered without meat!! They have something for everyone here"
                            },
                            "accessory": {
                                "type": "image",
                                "image_url": "https://s3-media3.fl.yelpcdn.com/bphoto/c7ed05m9lC2EmA3Aruue7A/o.jpg",
                                "alt_text": "alt text for image"
                            }
                        },
                        {
                            "type": "section",
                            "text": {
                                "type": "mrkdwn",
                                "text": "*Kin Khao*\n:star::star::star::star: 1638 reviews\n The sticky rice also goes wonderfully with the caramelized pork belly, which is absolutely melt-in-your-mouth and so soft."
                            },
                            "accessory": {
                                "type": "image",
                                "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/korel-1YjNtFtJlMTaC26A/o.jpg",
                                "alt_text": "alt text for image"
                            }
                        },
                        {
                            "type": "section",
                            "text": {
                                "type": "mrkdwn",
                                "text": "*Ler Ros*\n:star::star::star::star: 2082 reviews\n I would really recommend the  Yum Koh Moo Yang - Spicy lime dressing and roasted quick marinated pork shoulder, basil leaves, chili & rice powder."
                            },
                            "accessory": {
                                "type": "image",
                                "image_url": "https://s3-media2.fl.yelpcdn.com/bphoto/DawwNigKJ2ckPeDeDM7jAg/o.jpg",
                                "alt_text": "alt text for image"
                            }
                        },
                        {
                            "type": "divider"
                        },
                        {
                            "type": "actions",
                            "elements": [
                                {
                                    "type": "button",
                                    "text": {
                                        "type": "plain_text",
                                        "text": "Farmhouse",
                                        "emoji": True
                                    },
                                    "value": "click_me_123"
                                },
                                {
                                    "type": "button",
                                    "text": {
                                        "type": "plain_text",
                                        "text": "Kin Khao",
                                        "emoji": True
                                    },
                                    "value": "click_me_123",
                                    "url": "https://google.com"
                                },
                                {
                                    "type": "button",
                                    "text": {
                                        "type": "plain_text",
                                        "text": "Ler Ros",
                                        "emoji": True
                                    },
                                    "value": "click_me_123",
                                    "url": "https://google.com"
                                }
                            ]
                        }
                    ]
                }
            ])
        
        )
    except SlackApiError as e:
        assert e.response["ok"] is False
        assert e.response["error"]  # str like 'invalid_auth', 'channel_not_found'
        print(f"Got an error: {e.response['error']}")

asyncio.run(post_message())
slack通知結果

コメント

タイトルとURLをコピーしました