-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
124 lines (103 loc) · 2.78 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
117
118
119
120
121
122
123
124
//
// main.go
// main
//
// Created by d-exclaimation on 5:08 PM.
// Copyright © 2020 d-exclaimation. All rights reserved.
//
package main
import (
"fmt"
"log"
"net/http"
"os"
"strings"
. "github.com/d-exclaimation/lineapi/bot-impl"
"github.com/d-exclaimation/lineapi/commands"
arcade "github.com/d-exclaimation/lineapi/games"
"github.com/line/line-bot-sdk-go/linebot"
)
var prefix = "!"
type HubMessage struct {
Author string
Msg string
Key string
}
func main() {
// Get bot with all the env variables
var bot, err = linebot.New(
os.Getenv("CHANNEL_SECRET"),
os.Getenv("CHANNEL_TOKEN"),
)
if err != nil {
log.Fatal(err)
}
var games = make(map[string]*arcade.Sokoban)
// Setup HTTP Server for receiving requests from LINE platform
http.HandleFunc("/callback", func(w http.ResponseWriter, req *http.Request) {
var events, err = bot.ParseRequest(req)
if err != nil {
if err == linebot.ErrInvalidSignature {
w.WriteHeader(400)
} else {
w.WriteHeader(500)
}
return
}
for _, event := range events {
// Only check for EventTypeMessage
if event.Type == linebot.EventTypeMessage {
switch message := event.Message.(type) {
case *linebot.TextMessage:
// Get id of the room, group, or user
var id string
switch event.Source.Type {
case "user":
id = event.Source.UserID
case "group":
id = event.Source.GroupID
case "room":
id = event.Source.RoomID
}
// Let the action function handle the rest
actions(bot, event, message.Text, games, id)
}
}
}
})
if err := http.ListenAndServe(":"+os.Getenv("PORT"), nil); err != nil {
log.Fatal(err)
}
}
// Command function
func actions(bot *linebot.Client, event *linebot.Event, message string, games map[string]*arcade.Sokoban, id string) {
fmt.Println(message) // Logging for debugging
_, ok := games[id] // Check whether the given id has a game in the map
// If the message sent is a movement {w, a, s, d} then try to play the game, if game exist
if isWASD(message) && ok {
arcade.MoveGame(bot, event, message, games, id)
return
}
// A non command is ignored (start with a prefix)
if !strings.HasPrefix(message, prefix) {
return
}
// Special cases
if strings.HasPrefix(message, "!start") {
arcade.NewGame(bot, event, games, id)
return
}
// Get the command itself without any parameter
var name = strings.Split(message, " ")[0]
// Get the appropriate functions hashmaps from command directory
var commandMap = commands.All()
_, ok = commandMap[name] // Check if found
if ok {
// Execute the function given the required parameters
commandMap[name](bot, event, message)
}
}
func isWASD(msg string) bool {
var lower = strings.ToLower(msg)
return lower == "w" || lower == "a" || lower == "s" || lower == "d"
}