add ask docs command

2023additions
Dallas Winger 2023-04-13 00:20:52 -04:00
parent a41bb07c4b
commit 86dc52c53c
No known key found for this signature in database
GPG Key ID: 59CE387CBF3FCA01
3 changed files with 93 additions and 0 deletions

View File

@ -51,6 +51,7 @@ func (c *Commands) RegisterHandlers() {
c.RegisterCommand("cmtoin", "Convert Centimeters to Inches", false, c.handleCentimeterToInch)
c.RegisterCommand("intocm", "Convert Inches to Centimeters", false, c.handleInchToCentimeter)
c.RegisterCommand("google4u", "Displays a letmegooglethat link", false, c.handleGoogle4U)
c.RegisterCommand("askdocs", "Ask docs.hak5.org a question", false, c.handleDocs)
// Admin Commands
c.RegisterCommand("ping", "Ping Scuzzy", true, c.handlePing)

View File

@ -1,10 +1,13 @@
package commands
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"reflect"
@ -715,3 +718,73 @@ func (c *Commands) handleGoogle4U(s *discordgo.Session, m *discordgo.MessageCrea
}
return nil
}
func (c *Commands) handleDocs(s *discordgo.Session, m *discordgo.MessageCreate) error {
args := strings.Split(m.Content, " ")
if len(args) < 2 {
return errors.New("missing question")
}
input := m.Content[strings.Index(m.Content, " "):len(m.Content)]
apiurl := "https://api.gitbook.com/v1/spaces/"
endpoint := "/search/ask"
spaceId := ""
docsURL := "https://docs.hak5.org"
switch m.ChannelID {
case "522275837651714048":
//ducky
spaceId = "-MiIkRK_o3RBhZzUkrzr"
docsURL = "https://docs.hak5.org/hak5-usb-rubber-ducky/"
case "1006233482957164634":
//payloadstudio
spaceId = "RgTCQkzfO7AUWTT3gFAq"
docsURL = "https://docs.hak5.org/payload-studio/"
default:
// Default to ducky for testing in bot-testing channel
spaceId = "-MiIkRK_o3RBhZzUkrzr"
docsURL = "https://docs.hak5.org/hak5-usb-rubber-ducky/"
// return errors.New("lens not supported yet")
}
askEndpoint := apiurl + spaceId + endpoint
req := models.AISearchRequest{Query: url.QueryEscape(input)}
j, err := json.Marshal(req)
if err != nil {
return errors.New("error marshalling input")
}
r, err := http.NewRequest("POST", askEndpoint, bytes.NewBuffer(j))
if err != nil {
return errors.New("error creating request")
}
r.Header.Add("Content-Type", "application/json")
client := &http.Client{}
res, err := client.Do(r)
if err != nil {
return errors.New("error creating request client")
}
defer res.Body.Close()
resp := &models.AISearchResult{}
derr := json.NewDecoder(res.Body).Decode(resp)
if derr != nil {
return errors.New("error getting response from docs")
}
if res.StatusCode != http.StatusOK {
return errors.New("error getting response from docs: " + res.Status)
}
_, err = s.ChannelMessageSend(m.ChannelID, docsURL+"\n"+resp.Answer.Text)
if err != nil {
log.Print("[!] Error writing response: " + err.Error())
return err
}
return nil
}

19
models/gitbook.go Normal file
View File

@ -0,0 +1,19 @@
package models
type AISearchRequest struct {
Query string `json:"query"`
PreviousQueries []string `json:"previousQueries"`
}
type AISearchResult struct {
Answer struct {
Text string `json:"text"`
FollowupQuestions []string `json:"followupQuestions"`
Pages []struct {
Page string `json:"page"`
Revision string `json:"revision"`
Space string `json:"space"`
Sections []string `json:"sections"`
} `json:"pages"`
} `json:"answer"`
}