-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
76 lines (58 loc) · 1.7 KB
/
main.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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"github.com/Krognol/go-wolfram"
"github.com/joho/godotenv"
"github.com/shomali11/slacker"
"github.com/tidwall/gjson"
witai "github.com/wit-ai/wit-go/v2"
)
func printCommandEvents(ch <-chan *slacker.CommandEvent) {
for event:=range ch{
fmt.Println("Command Events")
fmt.Println(event.Timestamp)
fmt.Println(event.Command)
fmt.Println(event.Parameters)
fmt.Println(event.Event)
fmt.Println("\n")
}
}
func main(){
godotenv.Load(".env")
bot:=slacker.NewClient(os.Getenv("SLACK_BOT_TOKEN"),os.Getenv("SLACK_APP_TOKEN"))
client:=witai.NewClient(os.Getenv("WIT_AI_TOKEN"))
wolframClient:=&wolfram.Client{AppID: os.Getenv("WOLFRAM_APP_ID")}
go printCommandEvents(bot.CommandEvents())
bot.Command("query for bot - <message>",&slacker.CommandDefinition{
Description:"send any question to wolfram",
Examples: []string{
"wolfram who is the president of India",
"wolfram what is the capital of France",
},
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
query := request.Param("message")
msg,_:=client.Parse(&witai.MessageRequest{
Query: query,
})
data,_:=json.MarshalIndent(msg,""," ")
rough:=string(data[:])
value := gjson.Get(rough, "entities.wit$wolfram_search_query:wolfram_search_query.0.value")
answer:=value.String()
result,err:=wolframClient.GetSpokentAnswerQuery(answer,wolfram.Metric,1000)
if err!=nil{
fmt.Println("there is an error")
}
response.Reply(result)
},
})
ctx,cancel:=context.WithCancel(context.Background())
defer cancel()
err:=bot.Listen(ctx)
if err!=nil{
log.Fatal(err)
}
}