|
|
@ -5,13 +5,23 @@ import ( |
|
|
|
"strings" |
|
|
|
|
|
|
|
log "github.com/Sirupsen/logrus" |
|
|
|
"github.com/matrix-org/go-neb/matrix" |
|
|
|
"github.com/matrix-org/go-neb/types" |
|
|
|
"github.com/matrix-org/gomatrix" |
|
|
|
) |
|
|
|
|
|
|
|
// ServiceType of the Slack API service
|
|
|
|
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 { |
|
|
|
types.DefaultService |
|
|
|
webhookEndpointURL string |
|
|
@ -21,7 +31,11 @@ type Service struct { |
|
|
|
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, "/") |
|
|
|
|
|
|
|
if len(segments) < 2 { |
|
|
@ -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
|
|
|
|
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 |
|
|
|
if _, err := client.JoinRoom(s.RoomID, "", ""); err != nil { |
|
|
|
if _, err := client.JoinRoom(s.RoomID, "", nil); err != nil { |
|
|
|
log.WithFields(log.Fields{ |
|
|
|
log.ErrorKey: err, |
|
|
|
"room_id": s.RoomID, |
|
|
|