示例#1
0
func main() {
	hook := slack.NewWebHook(hookURL)
	err := hook.PostMessage(&slack.WebHookPostPayload{
		Text: "hello!",
		// Channel: "#test-channel",
		Attachments: []*slack.Attachment{
			{Text: "danger", Color: "danger"},
		},
	})
	if err != nil {
		panic(err)
	}
}
示例#2
0
// PostEvent implements MessagingService
func (s *SlackService) PostEvent(event webhook.Event) (string, error) {
	eventID := eventRequestID(event)
	text := Message(s, event)

	// Send the webhook to Logs
	log.Printf("[event:%v] %s", eventID, text)

	// Don't send to Slack
	if s.Token[0] == '-' {
		return "", nil
	}

	slackWebhookURL := fmt.Sprintf("https://hooks.slack.com/services/%s", s.Token)
	log.Printf("[event:%v] Sending event to slack %v\n", eventID, slackWebhookURL)

	webhook := slack.NewWebHook(slackWebhookURL)
	webhookErr := webhook.PostMessage(&slack.WebHookPostPayload{
		Username: "******",
		IconUrl:  "http://cl.ly/2t0u2Q380N3y/trusty.png",
		Attachments: []*slack.Attachment{
			&slack.Attachment{
				Fallback: text,
				Color:    "good",
				Fields: []*slack.AttachmentField{
					&slack.AttachmentField{
						Title: event.EventName(),
						Value: text,
					},
				},
			},
		},
	})
	if webhookErr != nil {
		log.Printf("[event:%v] Error sending to slack: %v\n", eventID, webhookErr)
	}

	return text, webhookErr
}