めしガチャ LINE bot
「めしガチャ」って話すと大学から半径8km以内で現在営業中の飲食店を、画像と詳細リンクつきでランダムに返してくれるLINE bot、気分転換に作ったら思ったよりも便利
https://pbs.twimg.com/media/DUjl3D2VoAMed_q.jpg
参考情報
店舗情報はYelpのAPIから
Rubyで書いて、Herokuにdeploy
code:ruby lib/meshi.rb
require 'rest-client'
require 'json'
def search_businesses_from_yelp(times)
params = {
latitude: 36.108722,
longitude: 140.103961, #筑波大学 radius: 8000,
limit: 50,
open_now: true,
price: 1
}
response = RestClient.get yelp_ep, { :Authorization => "bearer #{token}", params: params } response_json = JSON.parse(response)
end
def get_random_meshi(num)
results = []
businesses = search_businesses_from_yelp(num)
businesses.each do |business|
results.push(
{
"imageBackgroundColor": "#FFFFFF",
"actions": [
{
"type": "uri",
"label": "詳細をみる",
}
]
}
)
end
{
"type": "template",
"altText": "ガチャ結果",
"template": {
"type": "carousel",
"columns": results,
"imageAspectRatio": "rectangle",
"imageSize": "cover"
}
}
end
code:ruby lib/gacha.rb
require_relative 'meshi'
def get_gacha_reply(word)
times = 1
case word.tr("0-9", "0-9")
when /(\d+)連/ then
times = $1.to_i
end
case word
when /(飯|めし|メシ)/ then
get_random_meshi(times)
end
end
code: ruby lib/interactor.rb
require_relative 'gacha'
def get_bot_response_message(word)
case word
when /ガチャ/ then
get_gacha_reply(word)
when /(ping|生きてる?)/ then
{
type: 'text',
text: '生きとるで〜'
}
end
end
code: ruby app.rb
require 'sinatra' # gem 'sinatra'
require 'line/bot' # gem 'line-bot-api'
require_relative 'lib/interactor'
def client
@client ||= Line::Bot::Client.new { |config|
}
end
post '/callback' do
body = request.body.read
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::Text
message = get_bot_response_message(event.message'text') end
end
}
"OK"
end