-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
142 lines (118 loc) · 3.77 KB
/
app.rb
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
#!/bin/env ruby
# encoding: utf-8
require 'sinatra'
require 'line/bot'
require 'http'
require 'json'
require 'geokit'
configure :development, :test do
require 'config_env'
ConfigEnv.path_to_config("#{__dir__}/config/config_env.rb")
end
def client
@client ||= Line::Bot::Client.new { |config|
config.channel_secret = ENV["LINE_CHANNEL_SECRET"]
config.channel_token = ENV["LINE_CHANNEL_TOKEN"]
}
end
get '/ping' do
"PONG"
end
post '/callback' do
body = request.body.read
signature = request.env['HTTP_X_LINE_SIGNATURE']
unless client.validate_signature(body, signature)
error 400 do 'Bad Request' end
end
events = client.parse_events_from(body)
events.each { |event|
case event
when Line::Bot::Event::Message
case event.type
when Line::Bot::Event::MessageType::Location
user_current_latitude = event.message['latitude']
user_current_longitude = event.message['longitude']
address = event.message['address']
current_location = Geokit::LatLng.new(user_current_latitude, user_current_longitude)
Geokit::default_units = :meters
response = HTTP.get('https://cafenomad.tw/api/v1.0/cafes').to_s
response_json = JSON.parse(response)
feedback = {}
coffees_columns = []
coffee_shops = []
response_json.each do |c|
dest_str = "#{c["latitude"]},#{c["longitude"]}"
distance_ms = current_location.distance_to(dest_str)
if distance_ms <= 1000
c['distance_ms'] = distance_ms
coffee_shops << c
end
end
sorted_coffee_shops = coffee_shops.sort_by { |x| x['distance_ms'] }
sorted_coffee_shops[0..4].each do |scs|
if scs['url'] == ""
office_site_hash = { type: 'message', label: 'Official Site', text: '目前並沒有提供官方網站' }
else
office_site_hash = { type: 'uri', label: 'Official Site', uri: scs['url'] }
end
coffees_columns << {
# thumbnailImageUrl: '',
title: scs['name'],
text: "#{scs['address']}\n#{scs['distance_ms'].round} 公尺",
actions: [
{
type: 'uri',
label: 'View on Cafe Nomad',
uri: "https://cafenomad.tw/shop/#{scs['id']}"
},
{
type: 'uri',
label: 'Google Map',
uri: "https://www.google.com/maps/dir/'#{user_current_latitude},#{user_current_longitude}'/'#{scs["latitude"]},#{scs["longitude"]}'"
},
office_site_hash
]
}
end
feedback = {
type: 'template',
altText: '咖啡廳資訊如下,謝謝你的查詢。',
template: {
type: 'carousel',
columns: coffees_columns
}
}
if coffees_columns.empty?
reply event, textmsg('這附近似乎沒有咖啡廳呢!')
else
p coffees_columns
reply event, feedback
end
# when Line::Bot::Event::Join
# reply event, textmsg('你好!歡迎使用 Cafe Nomad 小幫手')
# when Line::Bot::Event::MessageType::Text
# message = {
# type: 'text',
# text: event.message['text']
# }
# when Line::Bot::Event::MessageType::Image, Line::Bot::Event::MessageType::Video
# reply event, textmsg("謝謝分享,但我現在還看不懂圖片與影片呢。")
end
end
}
"OK"
end
def textmsg text
if text.is_a? String
return {
type: 'text',
text: text
}
end
# it is probably already wrapped. Skip wrapping with type.
return text
end
def reply event, data
client.reply_message event['replyToken'], data
p data
end