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?

Slack 埋め込みURL 作成方法

Posted at

このドキュメントは、「Markdownリンク」→「Slack埋め込みリンク」への変換方法・プログラミング実装例をまとめたものです。


1. Slackの埋め込みリンク形式

Slackでは [テキスト](URL) のMarkdown構文は使えません。
代わりに、以下のような形式で記述します:

<URL|テキスト>

この形式で記述することで、「テキスト」がリンクとして表示されます。


2. Pythonによる自動変換例

import re

def markdown_to_slack_link(text):
    """
    [テキスト](URL) → <URL|テキスト> に変換
    """
    return re.sub(r"\[([^\]]+)\]\(([^)]+)\)", lambda m: f"<{m.group(2).strip('<>')}|{m.group(1)}>", text)

# 例:
original = '[Document 0](https://www.notion.so/xxxx)'
converted = markdown_to_slack_link(original)
print(converted)  # <https://www.notion.so/xxxx|Document 0>

3. Slack APIでの利用例

Slack BoltやWeb APIを使ってメッセージを送信する際、
text にそのまま <URL|テキスト> 形式を使えばOKです。

client.chat_postMessage(
    channel=user_id,
    text=converted_text
)

また、Block Kitの section ブロック内で mrkdwn を使う場合も同様に利用できます。


4. まとめ

  • Markdownの [text](url) はSlackでは使えない
  • <url|text> 形式に正規表現で変換すればOK
  • プレーンテキストでも mrkdwn でも有効

Slackでリンクテキストを使いたい場合は、この変換を活用しましょう!


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?