2018-04-26 09:15
浏览 16


I want to send a Slack notification with an attached file. This is my current code:

package Message

import (

func Message(message string, cannalul string, attash bool) {
    f, err := os.Open(filename)
    if err != nil {
      return false
    defer f.Close()

    _  = f

    fullName := "myServer"
    webhookUrl := "https://hooks.slack.com/services/......."

    attachment1 := slack.Attachment {}
    //attachment1.AddField(slack.Field { Title: "easySmtp", Value: "EasySmtp" }).AddField(slack.Field { Title: "Status", Value: "Completed" })
    if attash {
        attachment1.AddField(slack.Field { Title: "easySmtp", Value: fullName})
    payload := slack.Payload {
      Text: message,
      Username: "worker",
      Channel: cannalul,
      IconEmoji: ":grin:",
      Attachments: []slack.Attachment{attachment1},
    err := slack.Send(webhookUrl, "", payload)
    if len(err) > 0 {
      fmt.Printf("error: %s
", err)

My code works, but I don't know how I can add an attached file in my current code. How I can do this?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dtrovwl75780 2018-04-26 12:18

    You can not attach a file to an attachment through a webhook in Slack. That functionality does not exist in Slack.

    If its just text you can add the content as part of the message or another attachments (up to a limit of currently 500,000 characters, which will soon be reduced to 40,000 - see here for reference).

    Or you can directly upload a file to a channel with the API method files.upload.

    解决 无用
    打赏 举报

相关推荐 更多相似问题