-
Notifications
You must be signed in to change notification settings - Fork 5
/
bard.go
90 lines (71 loc) · 1.58 KB
/
bard.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strings"
"github.com/yunginnanet/girc-atomic"
)
type (
BardRequest struct {
SessionId string `json:"session_id"`
Message string `json:"message"`
}
BardResponse struct {
Content string `json:"content"`
}
)
func bard(c *girc.Client, e girc.Event, message string) {
saveToPastEe := false
if strings.Contains(message, "--save") {
message = strings.Replace(message, "--save", "", -1)
saveToPastEe = true
}
sendToIrc(c, e, "Processing Google Bard: "+message+"...")
url := config.Bard.Host + "/ask"
method := "POST"
request := BardRequest{
SessionId: config.Bard.SessionId,
Message: message,
}
payload, err := json.Marshal(request)
if err != nil {
handleApiError(c, e, err)
return
}
log.Println(string(payload))
client := &http.Client{}
req, err := http.NewRequest(method, url, strings.NewReader(string(payload)))
if err != nil {
handleApiError(c, e, err)
return
}
req.Header.Add("Content-Type", "application/json")
res, err := client.Do(req)
if err != nil {
handleApiError(c, e, err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
handleApiError(c, e, err)
return
}
post := &BardResponse{}
err = json.Unmarshal(body, post)
if err != nil {
handleApiError(c, e, err)
return
}
if post.Content != "" {
response := post.Content
response = strings.TrimSpace(response)
pasteEeLink := ""
if saveToPastEe {
pasteEeLink = "Saved to: " + pasteEe(response, message)
}
sendToIrc(c, e, response+"\n"+pasteEeLink)
}
}