質問するログイン新規登録

質問編集履歴

2

書式改善

2019/10/03 01:05

投稿

sekimori0802
sekimori0802

スコア15

title CHANGED
File without changes
body CHANGED
@@ -6,29 +6,116 @@
6
6
  とエラーが出ており困っております。
7
7
 
8
8
  ```
9
- エラーメッセージ
9
+ エラーメッセージI, [2019-10-03T00:56:21.711041 #4] INFO -- : [eb3f8fb8-27c3-413a-93cf-c2b167063968] Completed 500 Internal Server Error in 922ms
10
+ 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]
10
- NoMethodError (undefined method `sample' for nil:NilClass):
11
+ 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):
12
+ 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]
13
+ 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'
14
+ 2019-10-03T00:56:21.711881+00:00 app[web.1]: [eb3f8fb8-27c3-413a-93cf-c2b167063968] app/controllers/linebot_controller.rb:28:in `each'
15
+ 2019-10-03T00:56:21.711883+00:00 app[web.1]: [eb3f8fb8-27c3-413a-93cf-c2b167063968] app/controllers/linebot_controller.rb:28:in `callback'
16
+ sasaotookinoAir:ra-mennapp sasaotoyoki$
11
17
 
18
+
12
19
  ```
13
20
  ### 該当のソースコード
14
21
  ```
15
22
  events.each { |event|
16
23
  if event.message['text'] != nil
17
24
  place = event.message['text'] #ここでLINEで送った文章を取得
18
- result = `curl -X GET https://api.gnavi.co.jp/RestSearchAPI/v3/?keyid=ffb92f0f997a628153ecfa407099fe9b&category_s=RSFST08008&category_s=RSFST08009&#{place}`#ここでぐるなびAPIを叩く
25
+ result = `curl -X GET https://api.gnavi.co.jp/RestSearchAPI/v3/?keyid=78d2d49f3aa8747a6cc03da01cf41bdd&category_s=RSFST08008&category_s=RSFST08009&#{place}`#ここでぐるなびAPIを叩く
19
26
  else
20
27
  latitude = event.message['latitude']
21
28
  longitude = event.message['longitude']
22
29
 
23
- result = `curl -X GET https://api.gnavi.co.jp/RestSearchAPI/v3/?keyid=ffb92f0f997a628153ecfa407099fe9b&category_s=RSFST08008category_s=RSFST08009&latitude=#{latitude}longitude=#{longitude}` #ここでぐるなびAPIを叩く
30
+ result = `curl -X GET https://api.gnavi.co.jp/RestSearchAPI/v3/?keyid=78d2d49f3aa8747a6cc03da01cf41bdd&category_s=RSFST08008category_s=RSFST08009&latitude=#{latitude}longitude=#{longitude}` #ここでぐるなびAPIを叩く
24
31
  end
25
32
 
26
33
  hash_result = JSON.parse result #レスポンスが文字列なのでhashにパースする
27
34
  shops = hash_result["rest"] #ここでお店情報が入った配列となる
28
35
  shop = shops.sample #任意のものを一個選ぶ
29
36
  ```
30
- ### 試したこと
37
+ ### 全体ソースコード
38
+ ```
39
+ class LinebotController < ApplicationController
40
+ require 'line/bot' # gem 'line-bot-api'
41
+ require "json"
42
+
31
43
 
44
+ # callbackアクションのCSRFトークン認証を無効
45
+ protect_from_forgery :except => [:callback]
46
+
47
+ def client
48
+ @client ||= Line::Bot::Client.new { |config|
49
+ config.channel_secret = ENV["LINE_CHANNEL_SECRET"]
50
+ config.channel_token = ENV["LINE_CHANNEL_TOKEN"]
51
+ }
52
+ end
53
+
54
+ def callback
55
+ body = request.body.read
56
+
57
+ signature = request.env['HTTP_X_LINE_SIGNATURE']
58
+ unless client.validate_signature(body, signature)
59
+ error 400 do 'Bad Request' end
60
+ end
61
+
62
+ events = client.parse_events_from(body)
63
+
64
+ #ここでlineに送られたイベントを検出している
65
+ # messageのtext: に指定すると、返信する文字を決定することができる
66
+ #event.message['text']で送られたメッセージを取得することができる
67
+ events.each { |event|
68
+ if event.message['text'] != nil
69
+ place = event.message['text'] #ここでLINEで送った文章を取得
70
+ result = `curl -X GET https://api.gnavi.co.jp/RestSearchAPI/v3/?keyid=78d2d49f3aa8747a6cc03da01cf41bdd&category_s=RSFST08008&category_s=RSFST08009&#{place}`#ここでぐるなびAPIを叩く#{place}
71
+ else
72
+ latitude = event.message['latitude']
73
+ longitude = event.message['longitude']
74
+ puts event.message['latitude']
75
+ puts event.message['longitude']
76
+ puts event.message['latitude'].class
77
+ 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}
78
+ end
79
+ hash_result = JSON.parse(result)#レスポンスが文字列なのでhashにパースする
80
+ shops = hash_result["rest"] #ここでお店情報が入った配列となる
81
+ shop = shops.sample #任意のものを一個選ぶ
82
+ puts shop
83
+
84
+ 店の情報
85
+ url = shop["url_mobile"] #サイトのURLを送る
86
+ shop_name = shop["name"] #店の名前
87
+ category = shop["category"] #カテゴリー
88
+ open_time = shop["opentime"] #空いている時間
89
+ holiday = shop["holiday"] #定休日
90
+
91
+ if open_time.class != String #空いている時間と定休日の二つは空白の時にHashで返ってくるので、文字列に直そうとするとエラーになる。そのため、クラスによる場合分け。
92
+ open_time = ""
93
+ end
94
+ if holiday.class != String
95
+ holiday = ""
96
+ end
97
+
98
+ response = "【店名】" + shop_name + "\n" + "【カテゴリー】" + category + "\n" + "【営業時間と定休日】" + open_time + "\n" + holiday + "\n" + url
99
+ case event #case文 caseの値がwhenと一致する時にwhenの中の文章が実行される(switch文みたいなもの)
100
+ when Line::Bot::Event::Message
101
+ case event.type
102
+ when Line::Bot::Event::MessageType::Text,Line::Bot::Event::MessageType::Location
103
+ message = {
104
+ type: 'text',
105
+ text: response
106
+ }
107
+ client.reply_message(event['replyToken'], message)
108
+ end
109
+
110
+ end
111
+ }
112
+
113
+ head :ok
114
+ end
115
+ end
116
+ ```
117
+ ### 試したこと
118
+
32
119
  NET::htmlを試したり、このサイトのapi関連の記事を読み自分なりに実行してみましたが解決せず。
33
120
 
34
121
  ### 補足情報

1

書式改善

2019/10/03 01:05

投稿

sekimori0802
sekimori0802

スコア15

title CHANGED
File without changes
body CHANGED
@@ -11,6 +11,7 @@
11
11
 
12
12
  ```
13
13
  ### 該当のソースコード
14
+ ```
14
15
  events.each { |event|
15
16
  if event.message['text'] != nil
16
17
  place = event.message['text'] #ここでLINEで送った文章を取得
@@ -25,7 +26,7 @@
25
26
  hash_result = JSON.parse result #レスポンスが文字列なのでhashにパースする
26
27
  shops = hash_result["rest"] #ここでお店情報が入った配列となる
27
28
  shop = shops.sample #任意のものを一個選ぶ
28
-
29
+ ```
29
30
  ### 試したこと
30
31
 
31
32
  NET::htmlを試したり、このサイトのapi関連の記事を読み自分なりに実行してみましたが解決せず。