-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
89 lines (78 loc) · 2.08 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
package main
import (
"context"
"log"
"net/http"
"os"
"time"
"cloud.google.com/go/logging"
"github.com/otiai10/amesh-bot/bot"
"github.com/otiai10/amesh-bot/commands"
"github.com/otiai10/amesh-bot/controllers"
"github.com/otiai10/amesh-bot/service"
"github.com/otiai10/goapis/google"
"github.com/otiai10/marmoset"
)
var (
timezone *time.Location
)
func init() {
tokyo, err := time.LoadLocation("Asia/Tokyo")
if err != nil {
log.Fatalf("failed to load location: %v", err)
}
timezone = tokyo
}
func main() {
r := marmoset.NewRouter()
g := &google.Client{
APIKey: os.Getenv("GOOGLE_CUSTOMSEARCH_API_KEY"),
CustomSearchEngineID: os.Getenv("GOOGLE_CUSTOMSEARCH_ENGINE_ID"),
}
lg, err := logging.NewClient(context.Background(), os.Getenv("GOOGLE_PROJECT_ID"))
if err != nil {
panic(err)
}
defer lg.Close()
b := &bot.Bot{
Commands: []bot.Command{
commands.ImageCommand{Search: g},
commands.ForecastCommand{
SourceURL: "https://www.jma.go.jp/bosai/forecast", Timezone: timezone,
},
commands.TyphoonCommand{},
commands.GoogleCommand{Search: g},
commands.LGTMCommand{Service: service.LGTM{}},
commands.EchoCommand{},
},
Default: commands.AmeshCommand{
Storage: &service.Cloudstorage{BaseURL: "https://storage.googleapis.com"},
Timezone: timezone,
},
NotFound: commands.AICompletion{
APIKey: os.Getenv("OPENAI_APIKEY"),
BaseURL: "https://api.openai.com/v1",
}, // commands.NotFound{},
Logger: lg.Logger("bot"),
}
c := controllers.Controller{
Bot: b,
Slack: &service.SlackOAuthClient{},
Datastore: service.NewDatastore(os.Getenv("GOOGLE_PROJECT_ID")),
Storage: &service.Cloudstorage{BaseURL: "https://storage.googleapis.com"},
}
r.POST("/slack/webhook", c.Webhook)
r.GET("/slack/oauth", c.OAuth)
// 画像フィルタリング
r.GET("/image", c.Image)
http.Handle("/", r)
port := os.Getenv("PORT")
if port == "" {
port = "8080"
log.Printf("Defaulting to port %s", port)
}
log.Printf("Listening on port %s", port)
if err := http.ListenAndServe(":"+port, nil); err != nil {
log.Fatal(err)
}
}