mirror of https://github.com/matrix-org/go-neb.git
Browse Source
Guggy Integration (#75)
Guggy Integration (#75)
* WIP: Initial Guggy * Finish Guggy integration * typo & send No GIF Found when GIF string empty * unused function * Unused import, HTTPS API and GIFs * APIKey -> api_key * Use api_key on the wire, APIKey in structspull/70/merge
Luke Barnard
8 years ago
committed by
GitHub
3 changed files with 136 additions and 1 deletions
-
1src/github.com/matrix-org/go-neb/goneb.go
-
2src/github.com/matrix-org/go-neb/services/giphy/giphy.go
-
134src/github.com/matrix-org/go-neb/services/guggy/guggy.go
@ -0,0 +1,134 @@ |
|||
package services |
|||
|
|||
import ( |
|||
"encoding/json" |
|||
"bytes" |
|||
"math" |
|||
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" |
|||
"strings" |
|||
) |
|||
type guggyQuery struct { |
|||
// "mp4" or "gif"
|
|||
Format string `json:"format"` |
|||
// Query sentence
|
|||
Sentence string `json:"sentence"` |
|||
} |
|||
|
|||
type guggyGifResult struct { |
|||
ReqID string `json:"reqId"` |
|||
GIF string `json:"gif"` |
|||
Width float64 `json:"width"` |
|||
Height float64 `json:"height"` |
|||
} |
|||
|
|||
type guggyService struct { |
|||
id string |
|||
serviceUserID string |
|||
APIKey string `json:"api_key"` |
|||
} |
|||
|
|||
func (s *guggyService) ServiceUserID() string { return s.serviceUserID } |
|||
func (s *guggyService) ServiceID() string { return s.id } |
|||
func (s *guggyService) ServiceType() string { return "guggy" } |
|||
func (s *guggyService) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli *matrix.Client) { |
|||
} |
|||
func (s *guggyService) Register(oldService types.Service, client *matrix.Client) error { return nil } |
|||
func (s *guggyService) PostRegister(oldService types.Service) {} |
|||
|
|||
func (s *guggyService) Plugin(client *matrix.Client, roomID string) plugin.Plugin { |
|||
return plugin.Plugin{ |
|||
Commands: []plugin.Command{ |
|||
plugin.Command{ |
|||
Path: []string{"guggy"}, |
|||
Command: func(roomID, userID string, args []string) (interface{}, error) { |
|||
return s.cmdGuggy(client, roomID, userID, args) |
|||
}, |
|||
}, |
|||
}, |
|||
} |
|||
} |
|||
func (s *guggyService) cmdGuggy(client *matrix.Client, roomID, userID string, args []string) (interface{}, error) { |
|||
// only 1 arg which is the text to search for.
|
|||
querySentence := strings.Join(args, " ") |
|||
gifResult, err := s.text2gifGuggy(querySentence) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
if gifResult.GIF == "" { |
|||
return matrix.TextMessage{ |
|||
MsgType: "m.text.notice", |
|||
Body: "No GIF found!", |
|||
}, nil |
|||
} |
|||
|
|||
mxc, err := client.UploadLink(gifResult.GIF) |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
return matrix.ImageMessage{ |
|||
MsgType: "m.image", |
|||
Body: gifResult.ReqID, |
|||
URL: mxc, |
|||
Info: matrix.ImageInfo{ |
|||
Height: uint(math.Floor(gifResult.Height)), |
|||
Width: uint(math.Floor(gifResult.Width)), |
|||
Mimetype: "image/gif", |
|||
}, |
|||
}, nil |
|||
} |
|||
|
|||
// text2gifGuggy returns info about a gif
|
|||
func (s *guggyService) text2gifGuggy(querySentence string) (*guggyGifResult, error) { |
|||
log.Info("Transforming to GIF query ", querySentence) |
|||
|
|||
client := &http.Client{ } |
|||
|
|||
var query guggyQuery |
|||
query.Format = "gif" |
|||
query.Sentence = querySentence |
|||
|
|||
reqBody, err := json.Marshal(query); |
|||
if err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
reader := bytes.NewReader(reqBody) |
|||
|
|||
req, err := http.NewRequest("POST", "https://text2gif.guggy.com/guggify", reader) |
|||
if err != nil { |
|||
log.Error(err) |
|||
return nil, err |
|||
} |
|||
req.Header.Add("Content-Type", "application/json") |
|||
req.Header.Add("apiKey", s.APIKey) |
|||
|
|||
res, err := client.Do(req) |
|||
if res != nil { |
|||
defer res.Body.Close() |
|||
} |
|||
if err != nil { |
|||
log.Error(err) |
|||
return nil, err |
|||
} |
|||
var result guggyGifResult |
|||
if err := json.NewDecoder(res.Body).Decode(&result); err != nil { |
|||
return nil, err |
|||
} |
|||
|
|||
return &result, nil |
|||
} |
|||
|
|||
func init() { |
|||
types.RegisterService(func(serviceID, serviceUserID, webhookEndpointURL string) types.Service { |
|||
return &guggyService{ |
|||
id: serviceID, |
|||
serviceUserID: serviceUserID, |
|||
} |
|||
}) |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue