Skip to content

Instantly share code, notes, and snippets.

@taketin
Last active August 29, 2015 14:03
Show Gist options
  • Save taketin/146d1412d11f5596770b to your computer and use it in GitHub Desktop.
Save taketin/146d1412d11f5596770b to your computer and use it in GitHub Desktop.
Sample Hubot scripts to get data from gurunavi API
# 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/ver1/RestSearchAPI/?'
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']}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment