Skip to content

Commit

Permalink
add glip attachment support
Browse files Browse the repository at this point in the history
  • Loading branch information
grokify committed Aug 2, 2017
1 parent baa51d5 commit a9a9c3a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
24 changes: 13 additions & 11 deletions bin/send_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,18 @@ func (sender *Sender) SendCcMessage(ccMsg cc.Message, err error) {
}

func main() {
log.SetLevel(log.DebugLevel)

guidPointer := flag.String("guid", "", "Glip webhook GUID or URL")
examplePointer := flag.String("example", "", "Example message type")
adapterType := flag.String("adapter", "", "Adapter")

flag.Parse()
guid := strings.TrimSpace(*guidPointer)
webhookURLOrUID := strings.TrimSpace(*guidPointer)
example := strings.ToLower(strings.TrimSpace(*examplePointer))

fmt.Printf("LENGUID[%v]\n", len(guid))
fmt.Printf("GUID [%v]\n", guid)
fmt.Printf("LENGUID[%v]\n", len(webhookURLOrUID))
fmt.Printf("GUID [%v]\n", webhookURLOrUID)
fmt.Printf("EXAMPLE [%v]\n", example)

if len(example) < 1 {
Expand All @@ -87,21 +89,21 @@ func main() {

sender := Sender{}
if *adapterType == "glip" {
if len(guid) < 1 {
guid = os.Getenv(GLIP_WEBHOOK_ENV)
fmt.Printf("GLIP_GUID_ENV [%v]\n", guid)
if len(webhookURLOrUID) < 1 {
webhookURLOrUID = os.Getenv(GLIP_WEBHOOK_ENV)
fmt.Printf("GLIP_GUID_ENV [%v]\n", webhookURLOrUID)
}
adapter, err := adapters.NewGlipAdapter(guid)
adapter, err := adapters.NewGlipAdapter(webhookURLOrUID)
if err != nil {
panic("Incorrect Webhook GUID or URL")
}
sender.Adapter = &adapter
} else if *adapterType == "slack" {
if len(guid) < 1 {
guid = os.Getenv(SLACK_WEBHOOK_ENV)
fmt.Printf("SLACK_GUID_ENV [%v]\n", guid)
if len(webhookURLOrUID) < 1 {
webhookURLOrUID = os.Getenv(SLACK_WEBHOOK_ENV)
fmt.Printf("SLACK_GUID_ENV [%v]\n", webhookURLOrUID)
}
adapter, err := adapters.NewSlackAdapter(guid)
adapter, err := adapters.NewSlackAdapter(webhookURLOrUID)
if err != nil {
panic("Incorrect Webhook GUID or URL")
}
Expand Down
2 changes: 2 additions & 0 deletions src/adapters/glip.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
var (
AdaptersGlipActivityIncludeIntegrationName = false
AdaptersGlipMarkdownQuote = false
AdaptersGlipUseAttachments = false
AdaptersGlipUseShortFields = false
AdatpersGlipUseFieldExtraSpacing = true
EmojiURLFormat = ""
Expand All @@ -29,6 +30,7 @@ type GlipAdapter struct {
func NewGlipAdapter(webhookURLOrUID string) (GlipAdapter, error) {
glip, err := glipwebhook.NewGlipWebhookClient(webhookURLOrUID)
converter := ccglip.NewGlipMessageConverter()
converter.UseAttachments = AdaptersGlipUseAttachments
converter.UseShortFields = AdaptersGlipUseShortFields
converter.UseFieldExtraSpacing = AdatpersGlipUseFieldExtraSpacing
return GlipAdapter{
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func NewSlackAdapter(webhookURLOrUID string) (SlackAdapter, error) {
}

func (adapter *SlackAdapter) SendWebhook(urlOrUid string, message commonchat.Message) (*fasthttp.Request, *fasthttp.Response, error) {
return adapter.SlackClient.PostWebhookGUIDFast(urlOrUid, slack.ConvertCommonMessage(message))
return adapter.SlackClient.PostWebhookFast(urlOrUid, slack.ConvertCommonMessage(message))
}

func (adapter *SlackAdapter) SendMessage(message commonchat.Message) (*fasthttp.Request, *fasthttp.Response, error) {
Expand Down
26 changes: 17 additions & 9 deletions src/adapters/slack_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import (
"regexp"
"strings"

log "github.com/Sirupsen/logrus"
"github.com/commonchat/commonchat-go/slack"
"github.com/grokify/gotilla/net/httputil"
"github.com/valyala/fasthttp"
)

const (
ContentTypeHeader = "Content-Type"
ContentTypeValue = "application/json"
HTTPMethod = "POST"
HTTPMethod = "POST"
)

var (
Expand All @@ -29,7 +28,11 @@ type SlackWebhookClient struct {
}

func NewSlackWebhookClient(urlOrUid string, clientType string) (SlackWebhookClient, error) {
client := SlackWebhookClient{UrlPrefix: regexp.MustCompile(`^https?:`)}
log.WithFields(log.Fields{
"lib": "slack_client.go",
"request_url_client_init": urlOrUid}).Debug("")

client := SlackWebhookClient{UrlPrefix: regexp.MustCompile(`^https:`)}
client.WebhookUrl = client.BuildWebhookURL(urlOrUid)
if clientType == "fast" {
client.FastClient = fasthttp.Client{}
Expand All @@ -40,8 +43,12 @@ func NewSlackWebhookClient(urlOrUid string, clientType string) (SlackWebhookClie
}

func (client *SlackWebhookClient) BuildWebhookURL(urlOrUid string) string {
rs := client.UrlPrefix.FindString(urlOrUid)
rx := regexp.MustCompile(`^https:`)
rs := rx.FindString(urlOrUid)
if len(rs) > 0 {
log.WithFields(log.Fields{
"lib": "slack_client.go",
"request_url_http_match": urlOrUid}).Debug("")
return urlOrUid
}
return strings.Join([]string{WebhookBaseURL, urlOrUid}, "")
Expand All @@ -58,13 +65,14 @@ func (client *SlackWebhookClient) PostWebhookFast(url string, message slack.Mess
req.SetBody(bytes)

req.Header.SetMethod(HTTPMethod)
req.Header.SetRequestURI(client.BuildWebhookURL(url))
req.Header.Set(ContentTypeHeader, ContentTypeValue)
req.Header.SetRequestURI(url)

req.Header.Set(httputil.ContentTypeHeader, httputil.ContentTypeValueJSONUTF8)

err = client.FastClient.Do(req, resp)
return req, resp, err
}

func (client *SlackWebhookClient) PostWebhookGUIDFast(guid string, message slack.Message) (*fasthttp.Request, *fasthttp.Response, error) {
return client.PostWebhookFast(strings.Join([]string{WebhookBaseURL, guid}, ""), message)
func (client *SlackWebhookClient) PostWebhookGUIDFast(urlOrUid string, message slack.Message) (*fasthttp.Request, *fasthttp.Response, error) {
return client.PostWebhookFast(client.BuildWebhookURL(urlOrUid), message)
}

0 comments on commit a9a9c3a

Please sign in to comment.