|
@ -11,8 +11,10 @@ import ( |
|
|
"strings" |
|
|
"strings" |
|
|
|
|
|
|
|
|
"github.com/matrix-org/go-neb/types" |
|
|
"github.com/matrix-org/go-neb/types" |
|
|
"github.com/matrix-org/gomatrix" |
|
|
|
|
|
log "github.com/sirupsen/logrus" |
|
|
log "github.com/sirupsen/logrus" |
|
|
|
|
|
"maunium.net/go/mautrix" |
|
|
|
|
|
mevt "maunium.net/go/mautrix/event" |
|
|
|
|
|
"maunium.net/go/mautrix/id" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
// ServiceType of the Imgur service
|
|
|
// ServiceType of the Imgur service
|
|
@ -114,17 +116,17 @@ type Service struct { |
|
|
// Commands supported:
|
|
|
// Commands supported:
|
|
|
// !imgur some_search_query_without_quotes
|
|
|
// !imgur some_search_query_without_quotes
|
|
|
// Responds with a suitable image into the same room as the command.
|
|
|
// Responds with a suitable image into the same room as the command.
|
|
|
func (s *Service) Commands(client *gomatrix.Client) []types.Command { |
|
|
|
|
|
|
|
|
func (s *Service) Commands(client *mautrix.Client) []types.Command { |
|
|
return []types.Command{ |
|
|
return []types.Command{ |
|
|
types.Command{ |
|
|
|
|
|
|
|
|
{ |
|
|
Path: []string{"imgur", "help"}, |
|
|
Path: []string{"imgur", "help"}, |
|
|
Command: func(roomID, userID string, args []string) (interface{}, error) { |
|
|
|
|
|
|
|
|
Command: func(roomID id.RoomID, userID id.UserID, args []string) (interface{}, error) { |
|
|
return usageMessage(), nil |
|
|
return usageMessage(), nil |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
types.Command{ |
|
|
|
|
|
|
|
|
{ |
|
|
Path: []string{"imgur"}, |
|
|
Path: []string{"imgur"}, |
|
|
Command: func(roomID, userID string, args []string) (interface{}, error) { |
|
|
|
|
|
|
|
|
Command: func(roomID id.RoomID, userID id.UserID, args []string) (interface{}, error) { |
|
|
return s.cmdImgSearch(client, roomID, userID, args) |
|
|
return s.cmdImgSearch(client, roomID, userID, args) |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}, |
|
@ -132,13 +134,15 @@ func (s *Service) Commands(client *gomatrix.Client) []types.Command { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// usageMessage returns a matrix TextMessage representation of the service usage
|
|
|
// usageMessage returns a matrix TextMessage representation of the service usage
|
|
|
func usageMessage() *gomatrix.TextMessage { |
|
|
|
|
|
return &gomatrix.TextMessage{"m.notice", |
|
|
|
|
|
`Usage: !imgur image_search_text`} |
|
|
|
|
|
|
|
|
func usageMessage() *mevt.MessageEventContent { |
|
|
|
|
|
return &mevt.MessageEventContent{ |
|
|
|
|
|
MsgType: mevt.MsgNotice, |
|
|
|
|
|
Body: "Usage: !imgur image_search_text", |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Search Imgur for a relevant image and upload it to matrix
|
|
|
// Search Imgur for a relevant image and upload it to matrix
|
|
|
func (s *Service) cmdImgSearch(client *gomatrix.Client, roomID, userID string, args []string) (interface{}, error) { |
|
|
|
|
|
|
|
|
func (s *Service) cmdImgSearch(client *mautrix.Client, roomID id.RoomID, userID id.UserID, args []string) (interface{}, error) { |
|
|
// Check for query text
|
|
|
// Check for query text
|
|
|
if len(args) < 1 { |
|
|
if len(args) < 1 { |
|
|
return usageMessage(), nil |
|
|
return usageMessage(), nil |
|
@ -155,8 +159,8 @@ func (s *Service) cmdImgSearch(client *gomatrix.Client, roomID, userID string, a |
|
|
if searchResultImage != nil { |
|
|
if searchResultImage != nil { |
|
|
var imgURL = searchResultImage.Link |
|
|
var imgURL = searchResultImage.Link |
|
|
if imgURL == "" { |
|
|
if imgURL == "" { |
|
|
return gomatrix.TextMessage{ |
|
|
|
|
|
MsgType: "m.notice", |
|
|
|
|
|
|
|
|
return mevt.MessageEventContent{ |
|
|
|
|
|
MsgType: mevt.MsgNotice, |
|
|
Body: "No image found!", |
|
|
Body: "No image found!", |
|
|
}, nil |
|
|
}, nil |
|
|
} |
|
|
} |
|
@ -168,24 +172,24 @@ func (s *Service) cmdImgSearch(client *gomatrix.Client, roomID, userID string, a |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Return image message
|
|
|
// Return image message
|
|
|
return gomatrix.ImageMessage{ |
|
|
|
|
|
|
|
|
return mevt.MessageEventContent{ |
|
|
MsgType: "m.image", |
|
|
MsgType: "m.image", |
|
|
Body: querySentence, |
|
|
Body: querySentence, |
|
|
URL: resUpload.ContentURI, |
|
|
|
|
|
Info: gomatrix.ImageInfo{ |
|
|
|
|
|
Height: uint(searchResultImage.Height), |
|
|
|
|
|
Width: uint(searchResultImage.Width), |
|
|
|
|
|
Mimetype: searchResultImage.Type, |
|
|
|
|
|
|
|
|
URL: resUpload.ContentURI.CUString(), |
|
|
|
|
|
Info: &mevt.FileInfo{ |
|
|
|
|
|
Height: searchResultImage.Height, |
|
|
|
|
|
Width: searchResultImage.Width, |
|
|
|
|
|
MimeType: searchResultImage.Type, |
|
|
}, |
|
|
}, |
|
|
}, nil |
|
|
}, nil |
|
|
} else if searchResultAlbum != nil { |
|
|
} else if searchResultAlbum != nil { |
|
|
return gomatrix.TextMessage{ |
|
|
|
|
|
MsgType: "m.notice", |
|
|
|
|
|
|
|
|
return mevt.MessageEventContent{ |
|
|
|
|
|
MsgType: mevt.MsgNotice, |
|
|
Body: "Search returned an album - Not currently supported", |
|
|
Body: "Search returned an album - Not currently supported", |
|
|
}, nil |
|
|
}, nil |
|
|
} else { |
|
|
} else { |
|
|
return gomatrix.TextMessage{ |
|
|
|
|
|
MsgType: "m.notice", |
|
|
|
|
|
|
|
|
return mevt.MessageEventContent{ |
|
|
|
|
|
MsgType: mevt.MsgNotice, |
|
|
Body: "No image found!", |
|
|
Body: "No image found!", |
|
|
}, nil |
|
|
}, nil |
|
|
} |
|
|
} |
|
@ -280,7 +284,7 @@ func response2String(res *http.Response) string { |
|
|
|
|
|
|
|
|
// Initialise the service
|
|
|
// Initialise the service
|
|
|
func init() { |
|
|
func init() { |
|
|
types.RegisterService(func(serviceID, serviceUserID, webhookEndpointURL string) types.Service { |
|
|
|
|
|
|
|
|
types.RegisterService(func(serviceID string, serviceUserID id.UserID, webhookEndpointURL string) types.Service { |
|
|
return &Service{ |
|
|
return &Service{ |
|
|
DefaultService: types.NewDefaultService(serviceID, serviceUserID, ServiceType), |
|
|
DefaultService: types.NewDefaultService(serviceID, serviceUserID, ServiceType), |
|
|
} |
|
|
} |
|
|