質問編集履歴
2
書式改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,9 +14,23 @@
|
|
14
14
|
|
15
15
|
```
|
16
16
|
|
17
|
-
エラーメッセージ
|
17
|
+
エラーメッセージI, [2019-10-03T00:56:21.711041 #4] INFO -- : [eb3f8fb8-27c3-413a-93cf-c2b167063968] Completed 500 Internal Server Error in 922ms
|
18
|
+
|
18
|
-
|
19
|
+
2019-10-03T00:56:21.711691+00:00 app[web.1]: F, [2019-10-03T00:56:21.711621 #4] FATAL -- : [eb3f8fb8-27c3-413a-93cf-c2b167063968]
|
20
|
+
|
19
|
-
NoMethodError (undefined method `sample' for nil:NilClass):
|
21
|
+
2019-10-03T00:56:21.711762+00:00 app[web.1]: F, [2019-10-03T00:56:21.711705 #4] FATAL -- : [eb3f8fb8-27c3-413a-93cf-c2b167063968] NoMethodError (undefined method `sample' for nil:NilClass):
|
22
|
+
|
23
|
+
2019-10-03T00:56:21.711817+00:00 app[web.1]: F, [2019-10-03T00:56:21.711765 #4] FATAL -- : [eb3f8fb8-27c3-413a-93cf-c2b167063968]
|
24
|
+
|
25
|
+
2019-10-03T00:56:21.711880+00:00 app[web.1]: F, [2019-10-03T00:56:21.711829 #4] FATAL -- : [eb3f8fb8-27c3-413a-93cf-c2b167063968] app/controllers/linebot_controller.rb:48:in `block in callback'
|
26
|
+
|
27
|
+
2019-10-03T00:56:21.711881+00:00 app[web.1]: [eb3f8fb8-27c3-413a-93cf-c2b167063968] app/controllers/linebot_controller.rb:28:in `each'
|
28
|
+
|
29
|
+
2019-10-03T00:56:21.711883+00:00 app[web.1]: [eb3f8fb8-27c3-413a-93cf-c2b167063968] app/controllers/linebot_controller.rb:28:in `callback'
|
30
|
+
|
31
|
+
sasaotookinoAir:ra-mennapp sasaotoyoki$
|
32
|
+
|
33
|
+
|
20
34
|
|
21
35
|
|
22
36
|
|
@@ -32,7 +46,7 @@
|
|
32
46
|
|
33
47
|
place = event.message['text'] #ここでLINEで送った文章を取得
|
34
48
|
|
35
|
-
result = `curl -X GET https://api.gnavi.co.jp/RestSearchAPI/v3/?keyid=
|
49
|
+
result = `curl -X GET https://api.gnavi.co.jp/RestSearchAPI/v3/?keyid=78d2d49f3aa8747a6cc03da01cf41bdd&category_s=RSFST08008&category_s=RSFST08009&#{place}`#ここでぐるなびAPIを叩く
|
36
50
|
|
37
51
|
else
|
38
52
|
|
@@ -42,7 +56,7 @@
|
|
42
56
|
|
43
57
|
|
44
58
|
|
45
|
-
result = `curl -X GET https://api.gnavi.co.jp/RestSearchAPI/v3/?keyid=
|
59
|
+
result = `curl -X GET https://api.gnavi.co.jp/RestSearchAPI/v3/?keyid=78d2d49f3aa8747a6cc03da01cf41bdd&category_s=RSFST08008category_s=RSFST08009&latitude=#{latitude}longitude=#{longitude}` #ここでぐるなびAPIを叩く
|
46
60
|
|
47
61
|
end
|
48
62
|
|
@@ -56,7 +70,167 @@
|
|
56
70
|
|
57
71
|
```
|
58
72
|
|
59
|
-
|
73
|
+
### 全体ソースコード
|
74
|
+
|
75
|
+
```
|
76
|
+
|
77
|
+
class LinebotController < ApplicationController
|
78
|
+
|
79
|
+
require 'line/bot' # gem 'line-bot-api'
|
80
|
+
|
81
|
+
require "json"
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
# callbackアクションのCSRFトークン認証を無効
|
88
|
+
|
89
|
+
protect_from_forgery :except => [:callback]
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
def client
|
94
|
+
|
95
|
+
@client ||= Line::Bot::Client.new { |config|
|
96
|
+
|
97
|
+
config.channel_secret = ENV["LINE_CHANNEL_SECRET"]
|
98
|
+
|
99
|
+
config.channel_token = ENV["LINE_CHANNEL_TOKEN"]
|
100
|
+
|
101
|
+
}
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
def callback
|
108
|
+
|
109
|
+
body = request.body.read
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
signature = request.env['HTTP_X_LINE_SIGNATURE']
|
114
|
+
|
115
|
+
unless client.validate_signature(body, signature)
|
116
|
+
|
117
|
+
error 400 do 'Bad Request' end
|
118
|
+
|
119
|
+
end
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
events = client.parse_events_from(body)
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
#ここでlineに送られたイベントを検出している
|
128
|
+
|
129
|
+
# messageのtext: に指定すると、返信する文字を決定することができる
|
130
|
+
|
131
|
+
#event.message['text']で送られたメッセージを取得することができる
|
132
|
+
|
133
|
+
events.each { |event|
|
134
|
+
|
135
|
+
if event.message['text'] != nil
|
136
|
+
|
137
|
+
place = event.message['text'] #ここでLINEで送った文章を取得
|
138
|
+
|
139
|
+
result = `curl -X GET https://api.gnavi.co.jp/RestSearchAPI/v3/?keyid=78d2d49f3aa8747a6cc03da01cf41bdd&category_s=RSFST08008&category_s=RSFST08009&#{place}`#ここでぐるなびAPIを叩く#{place}
|
140
|
+
|
141
|
+
else
|
142
|
+
|
143
|
+
latitude = event.message['latitude']
|
144
|
+
|
145
|
+
longitude = event.message['longitude']
|
146
|
+
|
147
|
+
puts event.message['latitude']
|
148
|
+
|
149
|
+
puts event.message['longitude']
|
150
|
+
|
151
|
+
puts event.message['latitude'].class
|
152
|
+
|
153
|
+
result = `curl -X GET https://api.gnavi.co.jp/RestSearchAPI/v3/?keyid=78d2d49f3aa8747a6cc03da01cf41bdd&category_s=RSFST08008&category_s=RSFST08009&latitude=#{latitude}&longitude=#{longitude}`#ここでぐるなびAPIを叩くlatitude=#{latitude}longitude=#{longitude}
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
hash_result = JSON.parse(result)#レスポンスが文字列なのでhashにパースする
|
158
|
+
|
159
|
+
shops = hash_result["rest"] #ここでお店情報が入った配列となる
|
160
|
+
|
161
|
+
shop = shops.sample #任意のものを一個選ぶ
|
162
|
+
|
163
|
+
puts shop
|
164
|
+
|
165
|
+
|
166
|
+
|
167
|
+
店の情報
|
168
|
+
|
169
|
+
url = shop["url_mobile"] #サイトのURLを送る
|
170
|
+
|
171
|
+
shop_name = shop["name"] #店の名前
|
172
|
+
|
173
|
+
category = shop["category"] #カテゴリー
|
174
|
+
|
175
|
+
open_time = shop["opentime"] #空いている時間
|
176
|
+
|
177
|
+
holiday = shop["holiday"] #定休日
|
178
|
+
|
179
|
+
|
180
|
+
|
181
|
+
if open_time.class != String #空いている時間と定休日の二つは空白の時にHashで返ってくるので、文字列に直そうとするとエラーになる。そのため、クラスによる場合分け。
|
182
|
+
|
183
|
+
open_time = ""
|
184
|
+
|
185
|
+
end
|
186
|
+
|
187
|
+
if holiday.class != String
|
188
|
+
|
189
|
+
holiday = ""
|
190
|
+
|
191
|
+
end
|
192
|
+
|
193
|
+
|
194
|
+
|
195
|
+
response = "【店名】" + shop_name + "\n" + "【カテゴリー】" + category + "\n" + "【営業時間と定休日】" + open_time + "\n" + holiday + "\n" + url
|
196
|
+
|
197
|
+
case event #case文 caseの値がwhenと一致する時にwhenの中の文章が実行される(switch文みたいなもの)
|
198
|
+
|
199
|
+
when Line::Bot::Event::Message
|
200
|
+
|
201
|
+
case event.type
|
202
|
+
|
203
|
+
when Line::Bot::Event::MessageType::Text,Line::Bot::Event::MessageType::Location
|
204
|
+
|
205
|
+
message = {
|
206
|
+
|
207
|
+
type: 'text',
|
208
|
+
|
209
|
+
text: response
|
210
|
+
|
211
|
+
}
|
212
|
+
|
213
|
+
client.reply_message(event['replyToken'], message)
|
214
|
+
|
215
|
+
end
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
end
|
220
|
+
|
221
|
+
}
|
222
|
+
|
223
|
+
|
224
|
+
|
225
|
+
head :ok
|
226
|
+
|
227
|
+
end
|
228
|
+
|
229
|
+
end
|
230
|
+
|
231
|
+
```
|
232
|
+
|
233
|
+
### 試したこと
|
60
234
|
|
61
235
|
|
62
236
|
|
1
書式改善
test
CHANGED
File without changes
|
test
CHANGED
@@ -23,6 +23,8 @@
|
|
23
23
|
```
|
24
24
|
|
25
25
|
### 該当のソースコード
|
26
|
+
|
27
|
+
```
|
26
28
|
|
27
29
|
events.each { |event|
|
28
30
|
|
@@ -52,7 +54,7 @@
|
|
52
54
|
|
53
55
|
shop = shops.sample #任意のものを一個選ぶ
|
54
56
|
|
55
|
-
|
57
|
+
```
|
56
58
|
|
57
59
|
### 試したこと
|
58
60
|
|