Browse Source

Convert and re-enable wikipedia, imgur and guggy services

Signed-off-by: Nikos Filippakis <me@nfil.dev>
pull/322/head
Nikos Filippakis 4 years ago
parent
commit
6eccf69a9b
  1. 1
      go.sum
  2. 7
      goneb.go
  3. 30
      services/guggy/guggy.go
  4. 4
      services/guggy/guggy_test.go
  5. 50
      services/imgur/imgur.go
  6. 4
      services/imgur/imgur_test.go
  7. 28
      services/wikipedia/wikipedia.go
  8. 4
      services/wikipedia/wikipedia_test.go

1
go.sum

@ -89,6 +89,7 @@ github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaO
github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-shellwords v1.0.10 h1:Y7Xqm8piKOO3v10Thp7Z36h4FYFjt5xB//6XvOrs2Gw=
github.com/mattn/go-shellwords v1.0.10/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=

7
goneb.go

@ -26,13 +26,14 @@ import (
//_ "github.com/matrix-org/go-neb/services/github"
//_ "github.com/matrix-org/go-neb/services/google"
//_ "github.com/matrix-org/go-neb/services/guggy"
//_ "github.com/matrix-org/go-neb/services/imgur"
_ "github.com/matrix-org/go-neb/services/guggy"
_ "github.com/matrix-org/go-neb/services/imgur"
//_ "github.com/matrix-org/go-neb/services/jira"
_ "github.com/matrix-org/go-neb/services/rssbot"
//_ "github.com/matrix-org/go-neb/services/slackapi"
//_ "github.com/matrix-org/go-neb/services/travisci"
//_ "github.com/matrix-org/go-neb/services/wikipedia"
_ "github.com/matrix-org/go-neb/services/wikipedia"
"github.com/matrix-org/go-neb/types"
"github.com/matrix-org/util"
_ "github.com/mattn/go-sqlite3"

30
services/guggy/guggy.go

@ -11,8 +11,10 @@ import (
"strings"
"github.com/matrix-org/go-neb/types"
"github.com/matrix-org/gomatrix"
log "github.com/sirupsen/logrus"
"maunium.net/go/mautrix"
mevt "maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
// ServiceType of the Guggy service
@ -49,17 +51,17 @@ type Service struct {
// Commands supported:
// !guggy some search query without quotes
// 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{
types.Command{
{
Path: []string{"guggy"},
Command: func(roomID, userID string, args []string) (interface{}, error) {
Command: func(roomID id.RoomID, userID id.UserID, args []string) (interface{}, error) {
return s.cmdGuggy(client, roomID, userID, args)
},
},
}
}
func (s *Service) cmdGuggy(client *gomatrix.Client, roomID, userID string, args []string) (interface{}, error) {
func (s *Service) cmdGuggy(client *mautrix.Client, roomID id.RoomID, userID id.UserID, args []string) (interface{}, error) {
// only 1 arg which is the text to search for.
querySentence := strings.Join(args, " ")
gifResult, err := s.text2gifGuggy(querySentence)
@ -68,8 +70,8 @@ func (s *Service) cmdGuggy(client *gomatrix.Client, roomID, userID string, args
}
if gifResult.GIF == "" {
return gomatrix.TextMessage{
MsgType: "m.notice",
return mevt.MessageEventContent{
MsgType: mevt.MsgNotice,
Body: "No GIF found!",
}, nil
}
@ -79,14 +81,14 @@ func (s *Service) cmdGuggy(client *gomatrix.Client, roomID, userID string, args
return nil, fmt.Errorf("Failed to upload Guggy image to matrix: %s", err.Error())
}
return gomatrix.ImageMessage{
return mevt.MessageEventContent{
MsgType: "m.image",
Body: querySentence,
URL: resUpload.ContentURI,
Info: gomatrix.ImageInfo{
Height: uint(math.Floor(gifResult.Height)),
Width: uint(math.Floor(gifResult.Width)),
Mimetype: "image/gif",
URL: resUpload.ContentURI.CUString(),
Info: &mevt.FileInfo{
Height: int(math.Floor(gifResult.Height)),
Width: int(math.Floor(gifResult.Width)),
MimeType: "image/gif",
},
}, nil
}
@ -142,7 +144,7 @@ func (s *Service) text2gifGuggy(querySentence string) (*guggyGifResult, error) {
}
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{
DefaultService: types.NewDefaultService(serviceID, serviceUserID, ServiceType),
}

4
services/guggy/guggy_test.go

@ -12,7 +12,7 @@ import (
"github.com/matrix-org/go-neb/database"
"github.com/matrix-org/go-neb/testutils"
"github.com/matrix-org/go-neb/types"
"github.com/matrix-org/gomatrix"
"maunium.net/go/mautrix"
)
// TODO: It would be nice to tabularise this test so we can try failing different combinations of responses to make
@ -86,7 +86,7 @@ func TestCommand(t *testing.T) {
}
return nil, fmt.Errorf("Unknown URL: %s", req.URL.String())
}
matrixCli, _ := gomatrix.NewClient("https://hyrule", "@guggybot:hyrule", "its_a_secret")
matrixCli, _ := mautrix.NewClient("https://hyrule", "@guggybot:hyrule", "its_a_secret")
matrixCli.Client = &http.Client{Transport: matrixTrans}
// Execute the matrix !command

50
services/imgur/imgur.go

@ -11,8 +11,10 @@ import (
"strings"
"github.com/matrix-org/go-neb/types"
"github.com/matrix-org/gomatrix"
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
@ -114,17 +116,17 @@ type Service struct {
// Commands supported:
// !imgur some_search_query_without_quotes
// 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{
types.Command{
{
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
},
},
types.Command{
{
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)
},
},
@ -132,13 +134,15 @@ func (s *Service) Commands(client *gomatrix.Client) []types.Command {
}
// 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
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
if len(args) < 1 {
return usageMessage(), nil
@ -155,8 +159,8 @@ func (s *Service) cmdImgSearch(client *gomatrix.Client, roomID, userID string, a
if searchResultImage != nil {
var imgURL = searchResultImage.Link
if imgURL == "" {
return gomatrix.TextMessage{
MsgType: "m.notice",
return mevt.MessageEventContent{
MsgType: mevt.MsgNotice,
Body: "No image found!",
}, nil
}
@ -168,24 +172,24 @@ func (s *Service) cmdImgSearch(client *gomatrix.Client, roomID, userID string, a
}
// Return image message
return gomatrix.ImageMessage{
return mevt.MessageEventContent{
MsgType: "m.image",
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
} else if searchResultAlbum != nil {
return gomatrix.TextMessage{
MsgType: "m.notice",
return mevt.MessageEventContent{
MsgType: mevt.MsgNotice,
Body: "Search returned an album - Not currently supported",
}, nil
} else {
return gomatrix.TextMessage{
MsgType: "m.notice",
return mevt.MessageEventContent{
MsgType: mevt.MsgNotice,
Body: "No image found!",
}, nil
}
@ -280,7 +284,7 @@ func response2String(res *http.Response) string {
// Initialise the service
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{
DefaultService: types.NewDefaultService(serviceID, serviceUserID, ServiceType),
}

4
services/imgur/imgur_test.go

@ -12,7 +12,7 @@ import (
"github.com/matrix-org/go-neb/database"
"github.com/matrix-org/go-neb/testutils"
"github.com/matrix-org/go-neb/types"
"github.com/matrix-org/gomatrix"
"maunium.net/go/mautrix"
)
func TestCommand(t *testing.T) {
@ -106,7 +106,7 @@ func TestCommand(t *testing.T) {
}
return nil, fmt.Errorf("Unknown URL: %s", req.URL.String())
}
matrixCli, _ := gomatrix.NewClient("https://hyrule", "@imgurbot:hyrule", "its_a_secret")
matrixCli, _ := mautrix.NewClient("https://hyrule", "@imgurbot:hyrule", "its_a_secret")
matrixCli.Client = &http.Client{Transport: matrixTrans}
// Execute the matrix !command

28
services/wikipedia/wikipedia.go

@ -11,8 +11,10 @@ import (
"github.com/jaytaylor/html2text"
"github.com/matrix-org/go-neb/types"
"github.com/matrix-org/gomatrix"
log "github.com/sirupsen/logrus"
"maunium.net/go/mautrix"
mevt "maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
// ServiceType of the Wikipedia service
@ -49,11 +51,11 @@ type Service struct {
// Commands supported:
// !wikipedia some_search_query_without_quotes
// Responds with a suitable article extract and link to the referenced page 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{
types.Command{
{
Path: []string{"wikipedia"},
Command: func(roomID, userID string, args []string) (interface{}, error) {
Command: func(roomID id.RoomID, userID id.UserID, args []string) (interface{}, error) {
return s.cmdWikipediaSearch(client, roomID, userID, args)
},
},
@ -61,12 +63,14 @@ func (s *Service) Commands(client *gomatrix.Client) []types.Command {
}
// usageMessage returns a matrix TextMessage representation of the service usage
func usageMessage() *gomatrix.TextMessage {
return &gomatrix.TextMessage{"m.notice",
`Usage: !wikipedia search_text`}
func usageMessage() *mevt.MessageEventContent {
return &mevt.MessageEventContent{
MsgType: mevt.MsgNotice,
Body: "Usage: !wikipedia search_text",
}
}
func (s *Service) cmdWikipediaSearch(client *gomatrix.Client, roomID, userID string, args []string) (interface{}, error) {
func (s *Service) cmdWikipediaSearch(client *mautrix.Client, roomID id.RoomID, userID id.UserID, args []string) (interface{}, error) {
// Check for query text
if len(args) < 1 {
return usageMessage(), nil
@ -81,7 +85,7 @@ func (s *Service) cmdWikipediaSearch(client *gomatrix.Client, roomID, userID str
// No article extracts
if searchResultPage == nil || searchResultPage.Extract == "" {
return gomatrix.TextMessage{
return mevt.MessageEventContent{
MsgType: "m.notice",
Body: "No results",
}, nil
@ -90,7 +94,7 @@ func (s *Service) cmdWikipediaSearch(client *gomatrix.Client, roomID, userID str
// Convert article HTML to text
extractText, err := html2text.FromString(searchResultPage.Extract)
if err != nil {
return gomatrix.TextMessage{
return mevt.MessageEventContent{
MsgType: "m.notice",
Body: "Failed to convert extract to plain text - " + err.Error(),
}, nil
@ -105,7 +109,7 @@ func (s *Service) cmdWikipediaSearch(client *gomatrix.Client, roomID, userID str
extractText += fmt.Sprintf("\nhttp://en.wikipedia.org/?curid=%d", searchResultPage.PageID)
// Return article extract
return gomatrix.TextMessage{
return mevt.MessageEventContent{
MsgType: "m.notice",
Body: extractText,
}, nil
@ -175,7 +179,7 @@ func response2String(res *http.Response) string {
// Initialise the service
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{
DefaultService: types.NewDefaultService(serviceID, serviceUserID, ServiceType),
}

4
services/wikipedia/wikipedia_test.go

@ -12,7 +12,7 @@ import (
"github.com/matrix-org/go-neb/database"
"github.com/matrix-org/go-neb/testutils"
"github.com/matrix-org/go-neb/types"
"github.com/matrix-org/gomatrix"
"maunium.net/go/mautrix"
)
// TODO: It would be nice to tabularise this test so we can try failing different combinations of responses to make
@ -83,7 +83,7 @@ func TestCommand(t *testing.T) {
matrixTrans.RT = func(req *http.Request) (*http.Response, error) {
return nil, fmt.Errorf("Unknown URL: %s", req.URL.String())
}
matrixCli, _ := gomatrix.NewClient("https://hyrule", "@wikipediabot:hyrule", "its_a_secret")
matrixCli, _ := mautrix.NewClient("https://hyrule", "@wikipediabot:hyrule", "its_a_secret")
matrixCli.Client = &http.Client{Transport: matrixTrans}
// Execute the matrix !command

Loading…
Cancel
Save