-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (65 loc) · 2.16 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
package main
import (
"context"
"fmt"
"log"
"os"
"regexp"
"restaurant-scrapper/scrapper"
"github.com/enescakir/emoji"
"github.com/fatih/color"
"github.com/shomali11/slacker"
"github.com/slack-go/slack"
)
func main() {
bot := slacker.NewClient(os.Getenv("SLACK_BOT_TOKEN"), os.Getenv("SLACK_APP_TOKEN"))
go printCommandEvents(bot.CommandEvents())
bot.Command("menu-spok", &slacker.CommandDefinition{
Description: "Spok menu descriptor",
Example: "menu-spok",
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
dishes := scrapper.Scrapper("boutique.wysifood.fr", "https://boutique.wysifood.fr/5bde4fa022f9e550534f93537e604529/take-away/menu/15809")
italic := color.New(color.Italic).SprintFunc()
vege := regexp.MustCompile(`\(V\)|veggie|\(\ V\)|\(V\ \)|\(\ V\ \)`)
r := "---------Plats principaux (généralement 11.90eu)---------\n"
for _, d := range dishes {
if vege.MatchString(d.DishName) {
r = r + fmt.Sprintf("%v\n%s\n\n", d.DishName+" "+string(emoji.LeafyGreen), italic(d.Description))
} else {
r = r + fmt.Sprintf("%s\n%s\n\n", d.DishName, italic(d.Description))
}
}
response.Reply(r)
},
})
bot.Command("get-menu-file", &slacker.CommandDefinition{
Description: "Upload on chat the csv file containing all past menus",
Example: "get-menu-file",
Handler: func(botCtx slacker.BotContext, request slacker.Request, response slacker.ResponseWriter) {
ev := botCtx.Event()
client := botCtx.Client()
if ev.Channel != "" {
_, err := client.UploadFile(slack.FileUploadParameters{File: "menu.csv", Channels: []string{ev.Channel}})
if err != nil {
fmt.Printf("Error encountered when uploading file: %+v\n", err)
}
}
},
})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
err := bot.Listen(ctx)
if err != nil {
log.Fatal(err)
}
}
func printCommandEvents(analyticChannel <-chan *slacker.CommandEvent) {
for event := range analyticChannel {
fmt.Println("Command Events")
fmt.Println(event.Timestamp)
fmt.Println(event.Command)
fmt.Println(event.Parameters)
fmt.Println(event.Event)
fmt.Println()
}
}