Browse Source

Improve command path parsing

pull/155/head
Richard Lewis 8 years ago
parent
commit
333161d2e9
  1. 20
      src/github.com/matrix-org/go-neb/services/google/google.go
  2. 2
      src/github.com/matrix-org/go-neb/services/google/google_test.go

20
src/github.com/matrix-org/go-neb/services/google/google.go

@ -69,9 +69,15 @@ type Service struct {
func (s *Service) Commands(client *gomatrix.Client) []types.Command { func (s *Service) Commands(client *gomatrix.Client) []types.Command {
return []types.Command{ return []types.Command{
types.Command{ types.Command{
Path: []string{"google"},
Path: []string{"google", "image"},
Command: func(roomID, userID string, args []string) (interface{}, error) { Command: func(roomID, userID string, args []string) (interface{}, error) {
return s.cmdGoogle(client, roomID, userID, args)
return s.cmdGoogleImgSearch(client, roomID, userID, args)
},
},
types.Command{
Path: []string{"google", "help"},
Command: func(roomID, userID string, args []string) (interface{}, error) {
return usageMessage(), nil
}, },
}, },
} }
@ -83,13 +89,11 @@ func usageMessage() *gomatrix.TextMessage {
`Usage: !google image image_search_text`} `Usage: !google image image_search_text`}
} }
func (s *Service) cmdGoogle(client *gomatrix.Client, roomID, userID string, args []string) (interface{}, error) {
func (s *Service) cmdGoogleImgSearch(client *gomatrix.Client, roomID, userID string, args []string) (interface{}, error) {
if len(args) < 2 || args[0] != "image" {
if len(args) < 1 {
return usageMessage(), nil return usageMessage(), nil
} }
// Drop the search type (should currently always be "image")
args = args[1:]
// Get the query text to search for. // Get the query text to search for.
querySentence := strings.Join(args, " ") querySentence := strings.Join(args, " ")
@ -145,9 +149,7 @@ func (s *Service) text2imgGoogle(query string) (*googleSearchResult, error) {
// q.set("fileType, "") // Any file format // q.set("fileType, "") // Any file format
var key = s.APIKey var key = s.APIKey
if key == "" {
key = "AIzaSyA4FD39m9pN-hiYf2NRU9x9cOv5tekRDvM" // FIXME -- Should be instantiated from service config
}
q.Set("key", key) // Set the API key for the request q.Set("key", key) // Set the API key for the request
q.Set("cx", "003141582324323361145:f5zyrk9_8_m") // Set the custom search engine ID q.Set("cx", "003141582324323361145:f5zyrk9_8_m") // Set the custom search engine ID

2
src/github.com/matrix-org/go-neb/services/google/google_test.go

@ -100,7 +100,7 @@ func TestCommand(t *testing.T) {
// Execute the matrix !command // Execute the matrix !command
cmds := google.Commands(matrixCli) cmds := google.Commands(matrixCli)
if len(cmds) != 1 {
if len(cmds) != 2 {
t.Fatalf("Unexpected number of commands: %d", len(cmds)) t.Fatalf("Unexpected number of commands: %d", len(cmds))
} }
// cmd := cmds[0] // cmd := cmds[0]

Loading…
Cancel
Save