mirror of https://github.com/matrix-org/go-neb.git
Browse Source
Merge pull request #48 from matrix-org/kegan/giphy
Merge pull request #48 from matrix-org/kegan/giphy
Add Giphy integrationkegan/configure-client-name
Kegsay
8 years ago
committed by
GitHub
10 changed files with 217 additions and 6 deletions
-
15README.md
-
2src/github.com/matrix-org/go-neb/clients/clients.go
-
1src/github.com/matrix-org/go-neb/goneb.go
-
55src/github.com/matrix-org/go-neb/matrix/matrix.go
-
16src/github.com/matrix-org/go-neb/matrix/types.go
-
2src/github.com/matrix-org/go-neb/services/echo/echo.go
-
126src/github.com/matrix-org/go-neb/services/giphy/giphy.go
-
2src/github.com/matrix-org/go-neb/services/github/github.go
-
2src/github.com/matrix-org/go-neb/services/jira/jira.go
-
2src/github.com/matrix-org/go-neb/types/types.go
@ -0,0 +1,126 @@ |
|||
package services |
|||
|
|||
import ( |
|||
"encoding/json" |
|||
"errors" |
|||
log "github.com/Sirupsen/logrus" |
|||
"github.com/matrix-org/go-neb/matrix" |
|||
"github.com/matrix-org/go-neb/plugin" |
|||
"github.com/matrix-org/go-neb/types" |
|||
"net/http" |
|||
"net/url" |
|||
"strconv" |
|||
"strings" |
|||
) |
|||
|
|||
type result struct { |
|||
Slug string `json:"slug"` |
|||
Images struct { |
|||
Original struct { |
|||
URL string `json:"url"` |
|||
// Giphy returns ints as strings..
|
|||
Width string `json:"width"` |
|||
Height string `json:"height"` |
|||
Size string `json:"size"` |
|||
} `json:"original"` |
|||
} `json:"images"` |
|||
} |
|||
|
|||
type giphySearch struct { |
|||
Data []result |
|||
} |
|||
|
|||
type giphyService struct { |
|||
id string |
|||
serviceUserID string |
|||
APIKey string // beta key is dc6zaTOxFJmzC
|
|||
} |
|||
|
|||
func (s *giphyService) ServiceUserID() string { return s.serviceUserID } |
|||
func (s *giphyService) ServiceID() string { return s.id } |
|||
func (s *giphyService) ServiceType() string { return "giphy" } |
|||
func (s *giphyService) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli *matrix.Client) { |
|||
} |
|||
func (s *giphyService) Register(oldService types.Service, client *matrix.Client) error { return nil } |
|||
|
|||
func (s *giphyService) Plugin(client *matrix.Client, roomID string) plugin.Plugin { |
|||
return plugin.Plugin{ |
|||
Commands: []plugin.Command{ |
|||
plugin.Command{ |
|||
Path: []string{"giphy"}, |
|||
Command: func(roomID, userID string, args []string) (interface{}, error) { |
|||
return s.cmdGiphy(client, roomID, userID, args) |
|||
}, |
|||
}, |
|||
}, |
|||
} |
|||
} |
|||
func (s *giphyService) cmdGiphy(client *matrix.Client, roomID, userID string, args []string) (interface{}, error) { |
|||
// only 1 arg which is the text to search for.
|
|||
query := strings.Join(args, " ") |
|||
gifResult, err := s.searchGiphy(query) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
mxc, err := client.UploadLink(gifResult.Images.Original.URL) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
return matrix.ImageMessage{ |
|||
MsgType: "m.image", |
|||
Body: gifResult.Slug, |
|||
URL: mxc, |
|||
Info: matrix.ImageInfo{ |
|||
Height: asInt(gifResult.Images.Original.Height), |
|||
Width: asInt(gifResult.Images.Original.Width), |
|||
Mimetype: "image/gif", |
|||
Size: asInt(gifResult.Images.Original.Size), |
|||
}, |
|||
}, nil |
|||
} |
|||
|
|||
// searchGiphy returns info about a gif
|
|||
func (s *giphyService) searchGiphy(query string) (*result, error) { |
|||
log.Info("Searching giphy for ", query) |
|||
u, err := url.Parse("http://api.giphy.com/v1/gifs/search") |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
q := u.Query() |
|||
q.Set("q", query) |
|||
q.Set("api_key", s.APIKey) |
|||
u.RawQuery = q.Encode() |
|||
res, err := http.Get(u.String()) |
|||
if res != nil { |
|||
defer res.Body.Close() |
|||
} |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
var search giphySearch |
|||
if err := json.NewDecoder(res.Body).Decode(&search); err != nil { |
|||
return nil, err |
|||
} |
|||
if len(search.Data) == 0 { |
|||
return nil, errors.New("No results") |
|||
} |
|||
return &search.Data[0], nil |
|||
} |
|||
|
|||
func asInt(strInt string) uint { |
|||
u64, err := strconv.ParseUint(strInt, 10, 32) |
|||
if err != nil { |
|||
return 0 // default to 0 since these are all just hints to the client
|
|||
} |
|||
return uint(u64) |
|||
} |
|||
|
|||
func init() { |
|||
types.RegisterService(func(serviceID, serviceUserID, webhookEndpointURL string) types.Service { |
|||
return &giphyService{ |
|||
id: serviceID, |
|||
serviceUserID: serviceUserID, |
|||
} |
|||
}) |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue