-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
156 lines (133 loc) · 3.82 KB
/
Copy pathmain.go
File metadata and controls
156 lines (133 loc) · 3.82 KB
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
package main
import (
"fmt"
"log"
"net/http"
"github.com/Yuta-Haruna/calDisplayApp/accessKey"
"github.com/line/line-bot-sdk-go/linebot"
)
func main() {
// ハンドラの登録
http.HandleFunc("/", helloHandler)
http.HandleFunc("/callback", lineHandler)
fmt.Println("http://localhost:8080 で起動中...")
// HTTPサーバを起動
log.Fatal(http.ListenAndServe(":8080", nil))
}
func helloHandler(w http.ResponseWriter, r *http.Request) {
msg := "Hello World!!!!"
fmt.Fprintf(w, msg)
}
func lineHandler(w http.ResponseWriter, r *http.Request) {
// BOTを初期化
bot, err := linebot.New(
accessKey.GetSecretKey(),
accessKey.GetAccessKey(),
)
if err != nil {
log.Fatal(err)
}
// リクエストからBOTのイベントを取得
events, err := bot.ParseRequest(r)
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:
replyMessage := message.Text
_, err = bot.ReplyMessage(event.ReplyToken, linebot.NewTextMessage(replyMessage)).Do()
if err != nil {
log.Print(err)
}
// // メッセージが位置情報の場合
// case *linebot.LocationMessage:
// sendRestoInfo(bot, event)
// }
// 他にもスタンプや画像、位置情報など色々受信可能
}
}
}
// func sendRestoInfo(bot *linebot.Client, e *linebot.Event) {
// msg := e.Message.(*linebot.LocationMessage)
// lat := strconv.FormatFloat(msg.Latitude, 'f', 2, 64)
// lng := strconv.FormatFloat(msg.Longitude, 'f', 2, 64)
// replyMsg := getRestoInfo(lat, lng)
// res := linebot.NewTemplateMessage(
// "レストラン一覧",
// linebot.NewCarouselTemplate(replyMsg...).WithImageOptions("rectangle", "cover"),
// )
// if _, err := bot.ReplyMessage(e.ReplyToken, res).Do(); err != nil {
// log.Print(err)
// }
// }
// // response APIレスポンス
// type response struct {
// Results results `json:"results"`
// }
// // results APIレスポンスの内容
// type results struct {
// Shop []shop `json:"shop"`
// }
// // shop レストラン一覧
// type shop struct {
// Name string `json:"name"`
// Address string `json:"address"`
// Photo photo `json:"photo"`
// URLS urls `json:"urls"`
// }
// // photo 写真URL一覧
// type photo struct {
// Mobile mobile `json:"mobile"`
// }
// // mobile モバイル用の写真URL
// type mobile struct {
// L string `json:"l"`
// }
// // urls URL一覧
// type urls struct {
// PC string `json:"pc"`
// }
// func getRestoInfo(lat string, lng string) []*linebot.CarouselColumn {
// apikey := "(自分のAPIKEYを入力)"
// url := fmt.Sprintf(
// "https://webservice.recruit.co.jp/hotpepper/gourmet/v1/?format=json&key=%s&lat=%s&lng=%s",
// apikey, lat, lng)
// // リクエストしてボディを取得
// resp, err := http.Get(url)
// if err != nil {
// log.Fatal(err)
// }
// defer resp.Body.Close()
// body, err := ioutil.ReadAll(resp.Body)
// if err != nil {
// log.Fatal(err)
// }
// var data response
// if err := json.Unmarshal(body, &data); err != nil {
// log.Fatal(err)
// }
// var ccs []*linebot.CarouselColumn
// for _, shop := range data.Results.Shop {
// addr := shop.Address
// if 60 < utf8.RuneCountInString(addr) {
// addr = string([]rune(addr)[:60])
// }
// cc := linebot.NewCarouselColumn(
// shop.Photo.Mobile.L,
// shop.Name,
// addr,
// linebot.NewURIAction("ホットペッパーで開く", shop.URLS.PC),
// ).WithImageOptions("#FFFFFF")
// ccs = append(ccs, cc)
// }
// return ccs
}