-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
234 lines (208 loc) · 5.79 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
package main
import (
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"github.com/line/line-bot-sdk-go/linebot"
"github.com/Homarechan/datas"
"githuc.bom/Homarechan/prefcodes"
)
var helpMessage = `コマンド一覧
路線一覧:[都道府県]
駅一覧:[駅番号]
駅情報:[駅番号]
所属路線一覧:[駅番号]
隣接駅:[路線番号]
使い方
[コマンド]:[引数]
例:
路線一覧: 大阪府
駅情報:1130224`
func main() {
port := os.Getenv("PORT")
if port == "" {
log.Fatal("$PORT must be set")
}
bot, err := linebot.New(
os.Getenv("CHANNEL_SECRET"),
os.Getenv("CHANNEL_TOKEN"),
)
if err != nil {
log.Fatal(err)
}
// Setup HTTP Server for receiving requests from LINE platform
http.HandleFunc("/callback", func(w http.ResponseWriter, req *http.Request) {
events, err := bot.ParseRequest(req)
if err != nil {
if err == linebot.ErrInvalidSignature {
w.WriteHeader(400)
} else {
w.WriteHeader(500)
}
return
}
for _, event := range events {
if event.Type == linebot.EventTypeMessage {
switch message := event.Message.(type) {
case *linebot.TextMessage:
if event.ReplyToken == "00000000000000000000000000000000" {
return
}
if _, err = bot.ReplyMessage(event.ReplyToken, linebot.NewTextMessage(parse(message.Text))).Do(); err != nil {
log.Print(err)
}
}
}
}
})
// This is just sample code.
// For actual use, you must support HTTPS by using `ListenAndServeTLS`, a reverse proxy or something else.
if err := http.ListenAndServe(":"+os.Getenv("PORT"), nil); err != nil {
log.Fatal(err)
}
}
func parse(message string) string {
if startsWith(message, "路線一覧:") {
return fetchText(message[13:len(message)])
} else if startsWith(message, "駅一覧:") {
code, err := strconv.Atoi(message[10:len(message)])
if err != nil {
return "エラー:\n路線一覧で取得した路線番号を入力してください"
}
return getLineData(code)
} else if startsWith(message, "駅情報:") {
code, err := strconv.Atoi(message[10:len(message)])
if err != nil {
return "エラー:\n駅一覧で取得した駅番号を入力してください"
}
return getStationData(code)
} else if startsWith(message, "所属路線一覧:") {
code, err := strconv.Atoi(message[19:len(message)])
if err != nil {
return "エラー:\n駅一覧で取得した駅番号を入力してください"
}
return getGroupData(code)
} else if startsWith(message, "隣接駅:") {
code, err := strconv.Atoi(message[10:len(message)])
if err != nil {
return "エラー:\n駅一覧で取得した駅番号を入力してください"
}
return getJoinData(code)
}
return helpMessage
}
func startsWith(str string, text string) bool {
return strings.HasPrefix(str, text)
}
func fetchText(message string) string {
text := message
if len(text)/3 == 2 {
if text == "大阪" || text == "京都" {
text += "府"
} else if text == "東京" {
text = "東京都"
} else {
text += "県"
}
} else if len(text)/3 == 3 {
if text == "神奈川" || text == "和歌山" {
text += "県"
}
}
_, ok := prefcodes.NameToCode[text]
if !(ok) {
return "都道府県名が無効です"
}
return getPrefData(text)
}
func getPrefData(pref string) string {
linesInterface, err := datas.GetPrefData(prefcodes.NameToCode[pref])
if err != nil {
return err.Error()
}
lines := linesInterface.Lines
result := "[" + pref + "]\n"
for _, line := range lines {
result += "\n"
result += strconv.Itoa(line.Code)
result += ": "
result += line.Name
}
return result
}
func getLineData(linecode int) string {
stations, err := datas.GetLineData(linecode)
if err != nil {
return "エラー:\n路線一覧で取得した路線番号を入力してください"
}
result := "[" + stations.Line.Name + "]\n"
for _, station := range stations.Stations {
result += "\n"
result += strconv.Itoa(station.Code)
result += ": "
result += station.Name
}
return result
}
func getStationData(stationcode int) string {
station, err := datas.GetStationData(stationcode)
if err != nil {
return "エラー:\n駅一覧で取得した駅番号を入力してください"
}
result := "[" + station.Station.Name + "]\n\n"
result += "駅コード: " + strconv.Itoa(station.Station.Code)
result += "\n"
result += "駅グループコード: " + strconv.Itoa(station.Station.GroupCode)
result += "\n"
result += "路線: " + station.Station.LineName + "(" + strconv.Itoa(station.Station.LineCode) + ")"
result += "\n"
result += "都道府県: " + prefcodes.CodeToName[station.Station.PrefCode]
result += "\n"
result += "緯度: " + fmt.Sprintf("%f", station.Station.Latitude)
result += "\n"
result += "経度: " + fmt.Sprintf("%f", station.Station.Longtitude)
return result
}
func getGroupData(stationcode int) string {
group, err := datas.GetGroupData(stationcode)
if err != nil {
return "エラー:\n駅一覧で取得した駅番号を入力してください"
}
result := "[" + group.Station.Name + "]\n"
for _, line := range group.GroupStations {
result += "\n"
result += strconv.Itoa(line.LineCode)
result += ": "
result += line.LineName
result += "\n名称: "
result += line.Name
}
return result
}
func getJoinData(stationcode int) string {
joins, err := datas.GetJoinData(stationcode)
if err != nil {
return "エラー:\n路線一覧で取得した路線番号を入力してください"
}
line, err := datas.GetLineData(stationcode)
if err != nil {
return err.Error()
}
result := "[" + line.Line.Name + "]\n\n"
for _, join := range joins.StationJoins {
result += "隣接駅1:"
result += join.Name1
result += "("
result += strconv.Itoa(join.Code1)
result += ")\n"
result += "隣接駅2:"
result += join.Name2
result += "("
result += strconv.Itoa(join.Code2)
result += ")\n\n"
}
return result
}