From 2980e473d266836c25ad3cc321262793f48a90ac Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Fri, 24 Feb 2017 00:22:23 +0000 Subject: [PATCH] First pass at adding google web search --- .../go-neb/services/google/google.go | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/github.com/matrix-org/go-neb/services/google/google.go b/src/github.com/matrix-org/go-neb/services/google/google.go index 347011b..90841e0 100644 --- a/src/github.com/matrix-org/go-neb/services/google/google.go +++ b/src/github.com/matrix-org/go-neb/services/google/google.go @@ -85,7 +85,7 @@ func (s *Service) Commands(client *gomatrix.Client) []types.Command { types.Command{ Path: []string{"google"}, Command: func(roomID, userID string, args []string) (interface{}, error) { - return usageMessage(), nil + return s.cmdGoogleWebSearch(client, roomID, userID, args), nil }, }, } @@ -97,6 +97,35 @@ func usageMessage() *gomatrix.TextMessage { `Usage: !google image image_search_text`} } +// Search google for web results +func (s *Service) cmdGoogleWebSearch(client *gomatrix.Client, roomID, userID string, args []string) (interface{}, error) { + + if len(args) < 1 { + return usageMessage(), nil + } + + // Get the query text to search for. + querySentence := strings.Join(args, " ") + + searchResult, err := s.textSearchGoogle(querySentence) + + if err != nil { + return nil, err + } + + return gomatrix.HTMLMessage{ + MsgType: "m.notice", + Body: querySentence, + URL: resUpload.ContentURI, + Info: gomatrix.ImageInfo{ + Height: uint(math.Floor(searchResult.Image.Height)), + Width: uint(math.Floor(searchResult.Image.Width)), + Mimetype: searchResult.Mime, + }, + }, nil +} + +// Search google for a specified image func (s *Service) cmdGoogleImgSearch(client *gomatrix.Client, roomID, userID string, args []string) (interface{}, error) { if len(args) < 1 {