Slack Slash Command - 图像不松动

时间:2016-02-29 15:07:04

标签: slack-api

我编写了一个自定义Slash命令,该命令从用户接收查询并返回图像。

接收Slash命令的服务器端从用户检索查询,并形成图像URL http://example.com/file1.png,并将响应发送回<http://example.com/file1.png>。这在响应中显示为链接,并未展开。可能是什么问题?

我甚至尝试过以下方法: 1)我发回了一个JSON有效载荷,如下所示:

{
     "text":"http://example.com/file1.gif","unfurl_media":true
}

但是再次展示了这个链接并没有展开它。

2)我试过

{
     "text":"<http://example.com/file1.gif>","unfurl_media":true
}

但结果相同。

可能是什么问题?我是否绝对需要传入的webhook集成并在那里发送消息?

3 个答案:

答案 0 :(得分:2)

第一次是否有效,或者以前是否展开了网址?

Slack只会在给定频道中每小时自动展开一次网址。如果用户手动发布URL并且由于这个限制而没有展开,他们将从SlackBot获得关于它的短暂消息,但是对于斜杠命令或webhooks,展开只是默默地失败。我在测试之前已经打过这个,并且必须确保更改URL或频道以验证事情是否有效。

您不应该在响应中使用手动附件或webhook甚至是unfurl_media标记(默认情况下,对于通过webhooks / slash命令发布的消息,它都会打开)。

答案 1 :(得分:1)

我遇到类似的问题,斜杠命令返回我的图片网址文字。

您需要更改的是为图像使用image_url的对象,并将其放在attachments数组中。

下面是一个示例,它将图像的链接作为文本和图像本身返回。

        {
            "parse": "full",
            "response_type": "in_channel",
            "text": "http://example.com/file1.png",
            "attachments":[
                {
                    "image_url": "http://example.com/file1.png"
                }
            ],
            "unfurl_media":true,
            "unfurl_links":true
        }

答案 2 :(得分:0)

# Sample input objects that emulate file-info objects
# as output by Get-ChildItem
$files =
    @{ Name = 'ABC_1232.txt' },
    @{ Name = 'abC_4321.TxT' },
    @{ Name = 'qwerty_1232.cSv' },
    @{ Name = 'QwErTY_4321.CsV' },
    @{ Name = 'Unrelated.CsV' }

# The globs (wildcard patterns) to match against.
$globs = 'QWERTY_*.csv', 'abc_*.TXT'

# Translate the globs into regexes, with the non-literal parts enclosed in
# capture groups; note the addition of anchors ^ and $, given that globs
# match the entire input string.
# E.g., 'QWERTY_*.csv' -> '^QWERTY_(.*)\.csv$'
$regexes = foreach($glob in $globs) {
  '^' +
    ([regex]::Escape($glob) -replace '\\\*', '(.*)' -replace  # *
                                     '\\\?', '(.)' -replace   # ?
                                     '\\(\[.+?\])', '($1)') + # [...]
  '$'
}

# Construct string templates from the globs that can be used with the -f
# operator to fill in the variable parts from each filename match.
# Each variable part is replaced with a {<n>} placeholder, starting with 0.
# E.g., 'QWERTY_*.csv' -> 'QWERTY_{0}.csv'
$templates = foreach($glob in $globs) {
  $iRef = [ref] 0
  [regex]::Replace(
    ($glob -replace '[{}]', '$&$&'), # escape literal '{' and '}' as '{{' and '}}' first
    '\*|\?|\[.+?\]', # wildcard metachars. / constructs
    { param($match) '{' + ($iRef.Value++) + '}' } # replace with {<n>} placeholders
  )
}

# Loop over all files.
# IRL, use Get-ChildItem in lieu of $files.
$files | ForEach-Object {

  # Loop over all wildcard patterns
  $i = -1
  foreach ($regex in $regexes) {
    ++$i
    # See if the filename matches
    if (($matchInfo = [regex]::Match($_.Name, $regex, 'IgnoreCase')).Success) {
      # Instantiate the template string with the capture-group values.
      # E.g., 'QWERTY_{0}.csv' -f '4321' 
      $newName = $templates[$i] -f ($matchInfo.Groups.Value | Select-Object -Skip 1)

      # This is where your Rename-Item call would go.
      #   $_ | Rename-Item -WhatIf -NewName $newName
      # Note that if the filename already happens to be case-exact, 
      # Rename-Item is a quiet no-op.
      # For this demo, we simply output the new name.
      $newName    
    }
  }
}