26
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Last updated at Posted at 2014-07-01

Installation

$ npm install hubot-harahe --save

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

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']}"

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

Usage

get ぐるなびAPI key id

run hubot

# default address
hubot harahe

# with address
hubot harahe 福岡市天神

enjoy!

26
26
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
26

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?