|
@ -10,8 +10,11 @@ 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" |
|
|
|
|
|
"maunium.net/go/mautrix/event" |
|
|
|
|
|
mevt "maunium.net/go/mautrix/event" |
|
|
|
|
|
"maunium.net/go/mautrix/id" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
// ServiceType of the Giphy service.
|
|
|
// ServiceType of the Giphy service.
|
|
@ -58,18 +61,18 @@ type Service struct { |
|
|
// Commands supported:
|
|
|
// Commands supported:
|
|
|
// !giphy some search query without quotes
|
|
|
// !giphy some search query without quotes
|
|
|
// Responds with a suitable GIF into the same room as the command.
|
|
|
// Responds with a suitable GIF 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{ |
|
|
types.Command{ |
|
|
Path: []string{"giphy"}, |
|
|
Path: []string{"giphy"}, |
|
|
Command: func(roomID, userID string, args []string) (interface{}, error) { |
|
|
|
|
|
|
|
|
Command: func(roomID id.RoomID, userID id.UserID, args []string) (interface{}, error) { |
|
|
return s.cmdGiphy(client, roomID, userID, args) |
|
|
return s.cmdGiphy(client, roomID, userID, args) |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
}, |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (s *Service) cmdGiphy(client *gomatrix.Client, roomID, userID string, args []string) (interface{}, error) { |
|
|
|
|
|
|
|
|
func (s *Service) cmdGiphy(client *mautrix.Client, roomID id.RoomID, userID id.UserID, args []string) (interface{}, error) { |
|
|
// only 1 arg which is the text to search for.
|
|
|
// only 1 arg which is the text to search for.
|
|
|
query := strings.Join(args, " ") |
|
|
query := strings.Join(args, " ") |
|
|
gifResult, err := s.searchGiphy(query) |
|
|
gifResult, err := s.searchGiphy(query) |
|
@ -90,14 +93,14 @@ func (s *Service) cmdGiphy(client *gomatrix.Client, roomID, userID string, args |
|
|
return nil, err |
|
|
return nil, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return gomatrix.ImageMessage{ |
|
|
|
|
|
MsgType: "m.image", |
|
|
|
|
|
|
|
|
return mevt.MessageEventContent{ |
|
|
|
|
|
MsgType: event.MsgImage, |
|
|
Body: gifResult.Slug, |
|
|
Body: gifResult.Slug, |
|
|
URL: resUpload.ContentURI, |
|
|
|
|
|
Info: gomatrix.ImageInfo{ |
|
|
|
|
|
|
|
|
URL: resUpload.ContentURI.CUString(), |
|
|
|
|
|
Info: &mevt.FileInfo{ |
|
|
Height: asInt(image.Height), |
|
|
Height: asInt(image.Height), |
|
|
Width: asInt(image.Width), |
|
|
Width: asInt(image.Width), |
|
|
Mimetype: "image/gif", |
|
|
|
|
|
|
|
|
MimeType: "image/gif", |
|
|
Size: asInt(image.Size), |
|
|
Size: asInt(image.Size), |
|
|
}, |
|
|
}, |
|
|
}, nil |
|
|
}, nil |
|
@ -130,16 +133,16 @@ func (s *Service) searchGiphy(query string) (*result, error) { |
|
|
return &search.Data, nil |
|
|
return &search.Data, nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func asInt(strInt string) uint { |
|
|
|
|
|
u64, err := strconv.ParseUint(strInt, 10, 32) |
|
|
|
|
|
|
|
|
func asInt(strInt string) int { |
|
|
|
|
|
i64, err := strconv.ParseInt(strInt, 10, 32) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return 0 // default to 0 since these are all just hints to the client
|
|
|
return 0 // default to 0 since these are all just hints to the client
|
|
|
} |
|
|
} |
|
|
return uint(u64) |
|
|
|
|
|
|
|
|
return int(i64) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
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), |
|
|
} |
|
|
} |
|
|