-
Notifications
You must be signed in to change notification settings - Fork 0
/
wolframalpha.go
66 lines (54 loc) · 1.53 KB
/
wolframalpha.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
package wolframalpha
import (
"errors"
"strings"
"github.com/Clinet/clinet_convos"
"github.com/Clinet/clinet_features"
"github.com/Clinet/clinet_storage"
"github.com/JoshuaDoes/go-wolfram"
)
var Feature = features.Feature{
Name: "wolframalpha",
Desc: "Wolfram|Alpha is available as a conversation service. You can @Clinet with a question, and Wolfram|Alpha may answer it!",
ServiceConvo: &ClientWolframAlpha{},
}
type ClientWolframAlpha struct {
Client *wolfram.Client
}
func (wa *ClientWolframAlpha) Login() error {
cfg := &storage.Storage{}
if err := cfg.LoadFrom("wolframalpha"); err != nil {
return err
}
appID, err := cfg.ConfigGet("cfg", "appID")
if err != nil {
return err
}
wa.Client = &wolfram.Client{
AppID: appID.(string),
}
return nil
}
func (wa *ClientWolframAlpha) Query(query *convos.ConversationQuery, lastState *convos.ConversationState) (*convos.ConversationResponse, error) {
resp := &convos.ConversationResponse{}
if lastState != nil {
resp = lastState.Response
resp.TextSimple = ""
}
wolframConvo, err := wa.Client.GetConversationalQuery(query.Text, wolfram.Metric, resp.WolframAlpha)
if err != nil {
return nil, err
}
if wolframConvo.ErrorMessage != "" {
return nil, errors.New("wolframalpha: " + wolframConvo.ErrorMessage)
}
if wolframConvo.Result == "" {
return nil, errors.New("wolframalpha: empty result")
}
if !strings.HasSuffix(wolframConvo.Result, ".") {
wolframConvo.Result += "."
}
resp.TextSimple = wolframConvo.Result
resp.WolframAlpha = wolframConvo
return resp, nil
}