LoginSignup
1
0

More than 3 years have passed since last update.

Slack API でテキストの auto parse を escape する方法

Last updated at Posted at 2020-05-23

書いてあること

  • Slack API でメッセージを投稿するときの話
  • @hoge みたいなものはメンション(通知)として auto parse される
    • 他にチャンネル名やリンクなどいろいろある
  • ↑の処理を escape して平文テキストとして投稿するようにする方法です

方法

いろいろ調べてハマったけど公式ドキュメントにちゃんと書いてあった :tada:
Formatting text for app surfaces: Automatic parsing

  • For text in layout blocks set a verbatim attribute of your text objects to false. This is actually the default method of processing these text objects.
  • For the top-level text field, or text in secondary message attachments, you need to include a link_names argument with value of 1 when publishing the message - see the reference docs for the relevant publishing method for more information on how to include this.

解決方法は書いてある通り2つあります。

Text object のパラメータ追加

  • Text object に auto parse を無効にする "verbatim": true フィールドを追加する
  • verbatim は "一言一句違わず、言葉通りに" みたいな意味らしい
{
  "type": "mrkdwn",
  "text": "@hoge",
  "verbatim": true
}

リンクフォーマットの一部を使う

  • Slack の link format <http://example.com|example link>
  • この左側だけ使えばよい
    • リンク名になる右側が空欄だと左側の実際のURLがそのまま表示される仕様で、それを利用したものだと思われる
    • 力技感がすごい :muscle: :innocent:
  • verbatim は全部 escape しちゃうので局所的にしたいならこちらを使ったほうがいい
<@hoge|>
1
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
1
0