Browse Source

Pre-commit hook issues, and use gomatrix

pull/116/head
Luke Barnard 8 years ago
parent
commit
0a44908d57
  1. 4
      src/github.com/matrix-org/go-neb/services/slackapi/message.go
  2. 28
      src/github.com/matrix-org/go-neb/services/slackapi/slackapi.go

4
src/github.com/matrix-org/go-neb/services/slackapi/message.go

@ -13,7 +13,7 @@ import (
"time" "time"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/matrix-org/go-neb/matrix"
"github.com/matrix-org/gomatrix"
"github.com/russross/blackfriday" "github.com/russross/blackfriday"
) )
@ -195,7 +195,7 @@ func renderSlackAttachment(attachment *slackAttachment) {
} }
} }
func slackMessageToHTMLMessage(message slackMessage) (html matrix.HTMLMessage, err error) {
func slackMessageToHTMLMessage(message slackMessage) (html gomatrix.HTMLMessage, err error) {
text := linkifyString(message.Text) text := linkifyString(message.Text)
if message.Mrkdwn == nil || *message.Mrkdwn == true { if message.Mrkdwn == nil || *message.Mrkdwn == true {
message.TextRendered = template.HTML(blackfriday.MarkdownBasic([]byte(text))) message.TextRendered = template.HTML(blackfriday.MarkdownBasic([]byte(text)))

28
src/github.com/matrix-org/go-neb/services/slackapi/slackapi.go

@ -5,23 +5,37 @@ import (
"strings" "strings"
log "github.com/Sirupsen/logrus" log "github.com/Sirupsen/logrus"
"github.com/matrix-org/go-neb/matrix"
"github.com/matrix-org/go-neb/types" "github.com/matrix-org/go-neb/types"
"github.com/matrix-org/gomatrix"
) )
// ServiceType of the Slack API service // ServiceType of the Slack API service
const ServiceType = "slackapi" const ServiceType = "slackapi"
// Service contains the Config fields for the Slack API service.
//
// This service will send HTML formatted messages into a room when an outgoing slack webhook
// hits WebhookURL.
//
// Example JSON request:
// {
// "room_id": "!someroomid:some.domain.com",
// "message_type": "m.text"
// }
type Service struct { type Service struct {
types.DefaultService types.DefaultService
webhookEndpointURL string webhookEndpointURL string
// The URL which should be given to an outgoing slack webhook - Populated by Go-NEB after Service registration. // The URL which should be given to an outgoing slack webhook - Populated by Go-NEB after Service registration.
WebhookURL string `json:"webhook_url"`
WebhookURL string `json:"webhook_url"`
RoomID string `json:"room_id"` RoomID string `json:"room_id"`
MessageType string `json:"message_type"` MessageType string `json:"message_type"`
} }
func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli *matrix.Client) {
// OnReceiveWebhook receives requests from a slack outgoing webhook and possibly sends requests
// to Matrix as a result.
//
// This requires that the WebhookURL is given to an outgoing slack webhook (see https://api.slack.com/outgoing-webhooks)
func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli *gomatrix.Client) {
segments := strings.Split(req.URL.Path, "/") segments := strings.Split(req.URL.Path, "/")
if len(segments) < 2 { if len(segments) < 2 {
@ -37,7 +51,7 @@ func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli
slackMessage, err := getSlackMessage(*req) slackMessage, err := getSlackMessage(*req)
if err != nil { if err != nil {
log.WithFields(log.Fields{"slackMessage":slackMessage, log.ErrorKey:err}).Error("Slack message error")
log.WithFields(log.Fields{"slackMessage": slackMessage, log.ErrorKey: err}).Error("Slack message error")
w.WriteHeader(500) w.WriteHeader(500)
return return
} }
@ -56,9 +70,9 @@ func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli
} }
// Register joins the configured room and sets the public WebhookURL // Register joins the configured room and sets the public WebhookURL
func (s *Service) Register(oldService types.Service, client *matrix.Client) error {
func (s *Service) Register(oldService types.Service, client *gomatrix.Client) error {
s.WebhookURL = s.webhookEndpointURL s.WebhookURL = s.webhookEndpointURL
if _, err := client.JoinRoom(s.RoomID, "", ""); err != nil {
if _, err := client.JoinRoom(s.RoomID, "", nil); err != nil {
log.WithFields(log.Fields{ log.WithFields(log.Fields{
log.ErrorKey: err, log.ErrorKey: err,
"room_id": s.RoomID, "room_id": s.RoomID,
@ -71,7 +85,7 @@ func (s *Service) Register(oldService types.Service, client *matrix.Client) erro
func init() { func init() {
types.RegisterService(func(serviceID, serviceUserID, webhookEndpointURL string) types.Service { types.RegisterService(func(serviceID, serviceUserID, webhookEndpointURL string) types.Service {
return &Service{ return &Service{
DefaultService: types.NewDefaultService(serviceID, serviceUserID, ServiceType),
DefaultService: types.NewDefaultService(serviceID, serviceUserID, ServiceType),
webhookEndpointURL: webhookEndpointURL, webhookEndpointURL: webhookEndpointURL,
} }
}) })

Loading…
Cancel
Save