hubot + ぐるなびAPIで飲食店検索

More than 3 years have passed since last update.


Installation

$ npm install hubot-harahe --save

インストールしたら harahe.coffee を編集して API Key と検索アドレスを入力してください。

https://www.npmjs.com/package/hubot-harahe


scripts/harahe.coffee

# Description:

# Get restaurant data from Gurunavi API
#
# Commands:
# hubot harahe - Reply with restaurant info

Client = require("node-rest-client").Client
client = new Client()
parseString = require('xml2js').parseString

apiHost = 'http://api.gnavi.co.jp/RestSearchAPI/20150630/?'
keyId = 'YOUR API KEY ID'
address = '福岡県福岡市博多'
hitPerPage = 1

module.exports = (robot) ->
robot.respond /HARAHE ?(.*)$/i, (msg) ->
attr = msg.match[1].trim()
address = attr if attr != ""
req = "#{apiHost}keyid=#{keyId}&hit_per_page=#{hitPerPage}&offset_page=1&address=#{encodeURIComponent(address)}"

client.get req, (data, response) ->
parseString data, (err, result) ->
totalCount = result['response']['total_hit_count']
offsetPage = Math.floor Math.random() * totalCount
req = "#{apiHost}keyid=#{keyId}&hit_per_page=#{hitPerPage}&offset_page=#{offsetPage}&address=#{encodeURIComponent(address)}"

client.get req, (data, response) ->
parseString data, (err, result) ->
items = result['response']['rest'][0]
msg.send "Recommend: #{items['name']}"
msg.send "Category: #{items['category']}"
msg.send "Address: #{items['address']}"
msg.send "URL: #{items['url']}"
msg.send "Image: #{items['image_url'][0]['shop_image1']}"

https://gist.githubusercontent.com/taketin/146d1412d11f5596770b/raw/harahe.coffee

total_hit_count 取得するためにAPI2回叩いてるのがエコじゃない..


Usage


get ぐるなびAPI key id

http://api.gnavi.co.jp/api/use.html


run hubot

# default address

hubot harahe

# with address
hubot harahe 福岡市天神

enjoy!