Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify the code using struct #4

Merged
merged 4 commits into from
Aug 22, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 17 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,32 @@

# slack-go-webhook

Go Lang library to send messages to Slack via Incoming Webhooks.
Go Lang library to send messages to Slack via Incoming Webhooks.

## Usage
```go
package main

import "github.com/ashwanthkumar/slack-go-webhook"
import "fmt"

func main() {
attachment1 := slack.Attachment {}
attachment1.
AddField(slack.Field { Title: "Author", Value: "Ashwanth Kumar" }).
AddField(slack.Field { Title: "Status", Value: "Completed" })

payload := slack.Payload("Hello from <https://github.com/ashwanthkumar/slack-go-webhook|slack-go-webhook>, a Go-Lang library to send slack webhook messages.",
"golang-bot",
"",
"golang-test",
[]slack.Attachment { attachment1 })


slack.Send("https://hooks.slack.com/services/foo/bar/baz", "", payload)
token := "foo/bar/baz"

attachment1 := slack.Attachment {}
attachment1.AddField(slack.Field { Title: "Author", Value: "Ashwanth Kumar" }).AddField(slack.Field { Title: "Status", Value: "Completed" })
payload := slack.Payload {
Text: "Hello from <https://github.com/ashwanthkumar/slack-go-webhook|slack-go-webhook>, a Go-Lang library to send slack webhook messages.\n<https://golangschool.com/wp-content/uploads/golang-teach.jpg|golang-img>",
Username: "robot",
Channel: "#general",
IconEmoji: ":monkey_face:",
Attachments: []slack.Attachment{attachment1},
}
err := slack.Send(token, "", payload)
if len(err) > 0 {
fmt.Printf("error: %s\n", err)
}
}

```

## License
Expand Down
47 changes: 14 additions & 33 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package slack

import (
"fmt"
"encoding/json"
"log"
"strings"

"github.com/parnurzeal/gorequest"
)
Expand All @@ -29,50 +27,33 @@ type Attachment struct {
Fields []*Field `json:"fields"`
}

type Payload struct {
Parse string `json:"parse,omitempty"`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please format this with go fmt?

Username string `json:"username,omitempty"`
IconUrl string `json:"icon_url,omitempty"`
IconEmoji string `json:"icon_emoji,omitempty"`
Channel string `json:"channel,omitempty"`
Text string `json:"text,omitempty"`
Attachments []Attachment `json:"attachments,omitempty"`
}

func (attachment *Attachment) AddField(field Field) *Attachment {
attachment.Fields = append(attachment.Fields, &field)
return attachment
}

func Payload(text, username, imageOrIcon, channel string, attachments []Attachment) map[string]interface{} {
payload := make(map[string]interface{})
payload["parse"] = "full"
if username != "" {
payload["username"] = username
}

if strings.HasPrefix("http", imageOrIcon) {
payload["icon_url"] = imageOrIcon
} else if imageOrIcon != "" {
payload["icon_emoji"] = imageOrIcon
}

if channel != "" {
payload["channel"] = channel
}

if text != "" {
payload["text"] = text
}

if len(attachments) > 0 {
payload["attachments"] = attachments
}

return payload
}

func redirectPolicyFunc(req gorequest.Request, via []gorequest.Request) error {
return fmt.Errorf("Incorrect token (redirection)")
}

func Send(webhookUrl string, proxy string, payload map[string]interface{}) []error {
data, _ := json.Marshal(payload)
func Send(token string, proxy string, payload Payload) []error {
webhookUrl := fmt.Sprintf("https://hooks.slack.com/services/%v", token)

request := gorequest.New().Proxy(proxy)
resp, _, err := request.
Post(webhookUrl).
RedirectPolicy(redirectPolicyFunc).
Send(string(data)).
Send(payload).
End()

if err != nil {
Expand Down