-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a266abd
commit 6dd3ac1
Showing
4 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package fun | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type ImageResponse struct { | ||
Error string `json:"error"` | ||
Success bool `json:"success"` | ||
ImageURL string `json:"image"` | ||
} | ||
|
||
func GetImageURL(category string) (string, error) { | ||
resp, err := http.Get(fmt.Sprintf("https://botimages.realpha.ru/?category=%v", category)) | ||
if err != nil && resp.StatusCode == http.StatusOK { | ||
fmt.Printf("Getting image url error: %v", err) | ||
return "", errors.New("getting image url error") | ||
} | ||
|
||
var result ImageResponse | ||
|
||
err = json.NewDecoder(resp.Body).Decode(&result) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if result.Success { | ||
return fmt.Sprintf("https://botimages.realpha.ru/%v", result.ImageURL), nil | ||
} | ||
return "", errors.New("wrong data") | ||
} | ||
|
||
func GetImage(category string) (*bytes.Buffer, error) { | ||
resp, err := http.Get(fmt.Sprintf("https://botimages.realpha.ru/?category=%v", category)) | ||
if err != nil { | ||
fmt.Printf("Getting image url error: %v", err) | ||
return nil, errors.New("getting image url error") | ||
} | ||
|
||
buf := new(bytes.Buffer) | ||
|
||
_, err = io.Copy(buf, resp.Body) //png.Encode(buf, resp.Body) | ||
if err != nil { | ||
fmt.Printf("Map image: %v", err.Error()) | ||
} | ||
return buf, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"github.com/FlameInTheDark/dtbot/api/fun" | ||
"github.com/FlameInTheDark/dtbot/bot" | ||
) | ||
|
||
// SlapCommand returns slap image | ||
func SlapCommand(ctx bot.Context) { | ||
if len(ctx.Args) > 0 { | ||
url, err := fun.GetImageURL("slap") | ||
if err != nil { | ||
ctx.ReplyEmbedAttachmentImageURL(fmt.Sprintf("%v slaping %v", ctx.User.Mention(), ctx.Args[0]), url) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters