-
Notifications
You must be signed in to change notification settings - Fork 0
/
duckduckgo.go
69 lines (56 loc) · 1.57 KB
/
duckduckgo.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
package duckduckgo
import (
"errors"
"github.com/Clinet/clinet_convos"
"github.com/Clinet/clinet_features"
"github.com/Clinet/clinet_storage"
duckduckgo "github.com/JoshuaDoes/duckduckgolang"
)
var Feature = features.Feature{
Name: "duckduckgo",
Desc: "DuckDuckGo is available as a conversation service. You can @Clinet with a question, and DuckDuckGo may answer it!",
ServiceConvo: &ClientDuckDuckGo{},
}
type ClientDuckDuckGo struct {
Client *duckduckgo.Client
}
func (ddg *ClientDuckDuckGo) Login() error {
cfg := &storage.Storage{}
if err := cfg.LoadFrom("duckduckgo"); err != nil {
return err
}
appName := "Clinet"
rawAppName, err := cfg.ConfigGet("cfg", "appName")
if err != nil {
cfg.ConfigSet("cfg", "appName", appName)
} else {
appName = rawAppName.(string)
}
ddg.Client = &duckduckgo.Client{
AppName: appName,
}
return nil
}
func (ddg *ClientDuckDuckGo) Query(query *convos.ConversationQuery, lastState *convos.ConversationState) (*convos.ConversationResponse, error) {
resp := &convos.ConversationResponse{}
queryResult, err := ddg.Client.GetQueryResult(query.Text)
if err != nil {
return nil, err
}
result := ""
if queryResult.Definition != "" {
result = queryResult.Definition
} else if queryResult.Answer != "" {
result = queryResult.Answer
} else if queryResult.AbstractText != "" {
result = queryResult.AbstractText
}
if result == "" {
return nil, errors.New("duckduckgo: empty result")
}
resp.TextSimple = result
if queryResult.Image != "" {
resp.ImageURL = "https://duckduckgo.com" + queryResult.Image
}
return resp, nil
}