Skip to content
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
32 changes: 14 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Use agents instead if you need:

This extension showcases the skillset approach by providing three simple endpoints that generate random development data:
- Random commit messages
- Lorem ipsum text generation
- URL shortener
- Random user data

## Getting Started
Expand Down Expand Up @@ -72,22 +72,19 @@ URL: https://<your ngrok domain>/random-commit-message
Parameters: { "type": "object" }
Return type: String
---
Name: random_lorem_ipsum
Inference description: Generates a random Lorem Ipsum text. Responses should have html tags present.
URL: https://<your ngrok domain>/random-lorem-ipsum
Name: shorten_url
Inference description: Shortens a long URL using the CleanURI service. Send a JSON body with the "url" field and receive the shortened URL in the response.
URL: https://<your ngrok domain>/shorten-url
Parameters:
{
"type": "object",
"properties": {
"number_of_paragraphs": {
"type": "number",
"description": "The number of paragraphs to be generated. Must be between 1 and 10 inclusive"
},
"paragraph_length": {
"type": "string",
"description": "The length of each paragraph. Must be one of \"short\", \"medium\", \"long\", or \"verylong\""
}
}
"type": "object",
"properties": {
"url": {
"type": "string",
"description": "The long URL to be shortened."
}
},
"required": ["url"]
}
Return type: String
---
Expand All @@ -111,16 +108,15 @@ Return type: String
Here's some example things:

* `@skillset-example please create a random commit message`
* `@skillset-example generate a lorem ipsum`
* `@skillset-example generate a short lorem ipsum with 3 paragraphs`
* `@skillset-example please short this url https://github.com`
* `@skillset-example generate random user data`

## Implementation

This bot provides a passthrough to a couple of other APIs:

* For commit messages, https://whatthecommit.com/
* For Lorem Ipsum, https://loripsum.net/
* For shorting URLs, https://cleanuri.com/
* For user data, https://randomuser.me/

## Documentation
Expand Down
116 changes: 0 additions & 116 deletions handlers/loripsum.go

This file was deleted.

60 changes: 60 additions & 0 deletions handlers/shorten_url.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package handlers

import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)

// CleanURI API for shortening URLs
// https://cleanuri.com/docs

// Structure to receive the JSON request with the URL to shorten
type ShortenRequest struct {
URL string `json:"url"`
}

// HTTP handler to shorten URLs using the CleanURI API
func ShortenURL(w http.ResponseWriter, r *http.Request) {
fmt.Println("ShortenURL Called") // Log to know the handler was called

// Decode the request body expecting a JSON with the "url" field
var reqData ShortenRequest
if err := json.NewDecoder(r.Body).Decode(&reqData); err != nil {
http.Error(w, "Invalid JSON", http.StatusBadRequest)
return
}
fmt.Printf("URL to shorten: %s\n", reqData.URL) // Log the received URL

// Prepare the form body for the request to CleanURI
form := []byte("url=" + reqData.URL)
// Create the HTTP POST request to the CleanURI API
req, err := http.NewRequestWithContext(r.Context(), http.MethodPost, "https://cleanuri.com/api/v1/shorten", bytes.NewBuffer(form))
if err != nil {
http.Error(w, "Failed to create request", http.StatusInternalServerError)
return
}
// Set the header to indicate the body is a form
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

// Make the request to CleanURI
resp, err := http.DefaultClient.Do(req)
if err != nil {
http.Error(w, "Failed to contact CleanURI", http.StatusInternalServerError)
return
}
defer resp.Body.Close()

// If CleanURI responds with a code other than 200 OK, return the error
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
http.Error(w, fmt.Sprintf("CleanURI error: %s", string(body)), http.StatusInternalServerError)
return
}

// If everything goes well, copy CleanURI's response to the client
w.Header().Set("Content-Type", "application/json")
io.Copy(w, resp.Body)
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func main() {

func run() error {
http.HandleFunc("/random-commit-message", handlers.CommitMessage)
http.HandleFunc("/random-lorem-ipsum", handlers.Loripsum)
http.HandleFunc("/random-user", handlers.User)
http.HandleFunc("/shorten-url", handlers.ShortenURL)
http.HandleFunc("/_ping", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("OK"))
})
Expand Down