Skip to content
This repository has been archived by the owner on Jun 28, 2023. It is now read-only.

Add support to GIPHY bot for downsampled images #276

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ services:
UserID: "@goneb:localhost" # requires a Syncing client
Config:
api_key: "qwg4672vsuyfsfe"
use_downsized: false
image_type: 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer string enums to integers as strings are more descriptive without knowing the mapping.


- ID: "guggy_service"
Type: "guggy"
Expand Down
26 changes: 17 additions & 9 deletions src/github.com/matrix-org/go-neb/services/giphy/giphy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ type image struct {
type result struct {
Slug string `json:"slug"`
Images struct {
Downsized image `json:"downsized"`
Original image `json:"original"`
Original image `json:"original"`
DownsampledFixedHeight image `json:"fixed_height_downsampled"`
DownsampledFixedWidth image `json:"fixed_width_downsampled"`
Downsized image `json:"downsized"`
} `json:"images"`
}

Expand All @@ -42,17 +44,19 @@ type giphySearch struct {
// Example request:
// {
// "api_key": "dc6zaTOxFJmzC",
// "use_downsized": false
// "image_type": 0
// }
type Service struct {
types.DefaultService
// The Giphy API key to use when making HTTP requests to Giphy.
// The public beta API key is "dc6zaTOxFJmzC".
APIKey string `json:"api_key"`
// Whether to use the downsized image from Giphy.
// Uses the original image when set to false.
// Defaults to false.
UseDownsized bool `json:"use_downsized"`

// Specifies whether to use downsampled/downsized image from Giphy.
// 0 indicates original image (defaults to this)
// 1 for downsized images
// 2 for fixed height downsampled, 3 for fixed width downsampled images
ImageType int `json:"image_type"`
}

// Commands supported:
Expand All @@ -78,9 +82,13 @@ func (s *Service) cmdGiphy(client *gomatrix.Client, roomID, userID string, args
}

image := gifResult.Images.Original
if s.UseDownsized {
switch s.ImageType {
case 1:
image = gifResult.Images.DownsampledFixedHeight
case 2:
image = gifResult.Images.DownsampledFixedWidth
case 3:
image = gifResult.Images.Downsized
}

if image.URL == "" {
return nil, fmt.Errorf("No results")
Expand Down