/
main.go
116 lines (97 loc) · 3.13 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"strconv"
"github.com/joho/godotenv"
"github.com/krognol/go-wolfram"
"github.com/shomali11/slacker"
"github.com/slack-go/slack"
"github.com/tidwall/gjson"
witai "github.com/wit-ai/wit-go/v2"
)
func printCommandEvents(analyticsChannel <-chan *slacker.CommandEvent) {
for event := range analyticsChannel {
fmt.Println(event.Command)
fmt.Println(event.Parameters)
fmt.Println(event.Timestamp)
fmt.Println(event.Event)
}
}
func main() {
err := godotenv.Load()
if err != nil {
fmt.Errorf(err.Error())
}
bot := slacker.NewClient(os.Getenv("SLACK_BOT_TOKEN"), os.Getenv("SLACK_APP_TOKEN"))
witClient := witai.NewClient(os.Getenv("WIT_AI_TOKEN"))
wolframClient := &wolfram.Client{AppID: os.Getenv("WOLFRAM_APP_ID")}
// go printCommandEvents(bot.CommandEvents())
bot.Command("query for my bot - <query>", &slacker.CommandDefinition{
Description: "ai chat bot",
Examples: []string{"what is the capital of India"},
Handler: func(ctx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
query := request.Param("query")
// call wit with the user input to construct query which wolfram can understand
res, err := witClient.Parse(&witai.MessageRequest{
Query: query,
})
if err != nil {
fmt.Println(err.Error())
return
}
// convert response to understandable json format to fetch required value for processing
data, err := json.Marshal(res)
if err != nil {
fmt.Println(err.Error())
return
}
rough := string(data[:])
// use gjson to deconstruct the response (gjson.ResponseToUnderstandableJson(res, "$wolframquer...") which is very nested json
val := gjson.Get(rough, "entities.wit$wolfram_search_query:wolfram_search_query.0.value")
stringVal := val.String()
// call wolfram with the query result decoded from the wit response
receivedAnswer := "NA"
receivedAnswer, err = wolframClient.GetSpokentAnswerQuery(stringVal, wolfram.Metric, 1000)
if err != nil {
log.Fatal(err)
}
response.Reply(receivedAnswer, slacker.WithThreadReply(true))
},
})
bot.Command("my yob is <year>", &slacker.CommandDefinition{
Description: "age calculator",
Examples: []string{"my yob is 2002", "my yob is 1967"},
Handler: func(ctx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
param := request.Param("year")
var yob int
if yob, err = strconv.Atoi(param); err != nil {
log.Print(err.Error())
}
age := 2023 - yob
r := fmt.Sprintf("your age is %v", age)
response.Reply(r)
},
})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
log.Fatal(bot.Listen(ctx))
// File upload via go server to slack channel
fileBot := slack.New(os.Getenv("SLACK_FILE_BOT_TOKEN"))
channelArr := []string{os.Getenv("CHANNEL_ID")}
files := []string{"sample.csv"}
for i := range files {
params := slack.FileUploadParameters{
Channels: channelArr,
File: files[i],
}
file, err := fileBot.UploadFile(params)
if err != nil {
log.Print(err.Error())
}
log.Printf("URL: %v; Name: %v", file.URLPrivate, file.Name)
}
}