/
queryservice-googleassistant.go
61 lines (48 loc) · 1.66 KB
/
queryservice-googleassistant.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
package main
import (
"errors"
"strings"
"time"
"github.com/bwmarrin/discordgo"
)
//QueryServiceGoogleAssistant holds a Google Assistant query service
type QueryServiceGoogleAssistant struct {
}
//GetName returns "Google Assistant"
func (*QueryServiceGoogleAssistant) GetName() string {
return "Google Assistant"
}
//GetColor returns 0x0F9D58
func (*QueryServiceGoogleAssistant) GetColor() int {
return 0x0F9D58
}
//GetIconURL returns "https://files.joshuadoes.com/googleassistant_transparent.png"
func (*QueryServiceGoogleAssistant) GetIconURL() string {
return "https://files.joshuadoes.com/googleassistant_transparent.png"
}
//Query returns a Google Assistant response to a given query
func (*QueryServiceGoogleAssistant) Query(query string, env *QueryEnvironment) (*discordgo.MessageEmbed, error) {
Debug.Println("[Google Assistant] Spawning a conversation...")
conversation, err := botData.BotClients.GoogleAssistant.NewConversation(time.Second * 240)
if err != nil {
return nil, err
}
defer conversation.Close()
Debug.Println("[Google Assistant] Requesting text transport...")
textTransport := conversation.RequestTransportText()
Debug.Printf("[Google Assistant] Getting result for [%s]...", query)
result, err := textTransport.Query(query)
if err != nil {
return nil, err
}
if result == "" {
Debug.Println("[Google Assistant] Error getting allowed result from response")
return nil, errors.New("error getting allowed result from response")
}
for old, new := range botData.BotOptions.QueryResponseReplacements {
result = strings.ReplaceAll(result, old, new)
}
gaEmbed := NewEmbed().
AddField(query, result).MessageEmbed
return gaEmbed, nil
}