-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (49 loc) · 1.07 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
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/aikchun/gotelegrambot"
"github.com/aikchun/totobro-bot/internal/services/db"
"github.com/aws/aws-lambda-go/lambda"
"github.com/joho/godotenv"
)
func handleLambdaEvent(u gotelegrambot.Update) {
d := db.NewDB()
b := newBot(os.Getenv("BOT_USERNAME"), os.Getenv("BOT_TOKEN"), d)
b.handle(&u)
}
func handler(w http.ResponseWriter, r *http.Request) {
var u gotelegrambot.Update
body, err := io.ReadAll(io.LimitReader(r.Body, 1048576))
if err != nil {
log.Fatal(err)
}
if err := r.Body.Close(); err != nil {
log.Fatal(err)
}
if err := json.Unmarshal(body, &u); err != nil {
log.Fatal(err)
}
handleLambdaEvent(u)
}
func main() {
err := godotenv.Load(".env")
if err != nil {
log.Printf("Couldn't find .env")
}
e := os.Getenv("ENVIRONMENT")
if e != "dev" {
lambda.Start(handleLambdaEvent)
}
p := ":8080"
if e == "dev" {
http.HandleFunc("/", handler)
fmt.Printf("starting http server\n")
fmt.Printf("listening: %s\n", p)
log.Fatal(http.ListenAndServe(p, nil))
}
}