Skip to content

Instantly share code, notes, and snippets.

@hirokazumiyaji
Last active January 10, 2016 13:31
Show Gist options
  • Save hirokazumiyaji/e91f37836ad9abaa7500 to your computer and use it in GitHub Desktop.
Save hirokazumiyaji/e91f37836ad9abaa7500 to your computer and use it in GitHub Desktop.
docomo bot
class Docomo
API_PREFIX = "https://api.apigw.smt.docomo.ne.jp"
constructor: (apiKey) ->
@apiKey = apiKey
@dialogueStatus = {}
@dialogue: (request, username, message, t = null) ->
url = "#{API_PREFIX}/dialogue/v1/dialogue?APIKEY=#{@apiKey}"
data =
"utt": message || ''
data = "t": t if t?
data = "nickname": username if username? and username.length <= 10
data = "context": context if context?
now = new Date.getTime()
if now - status["time"] > 2 * 60 * 1000
dialoqueStatus =
"context": ''
"mode": ''
request
.http(url)
.headers("Content-Type": "application/json")
.post(JSON.stringify(data)), (err, response, body) ->
if err?
console.log ""
else if response.statusCode == 200
parseData = JSON.parse(body)
request.send parseData.utt
dialogueStatus =
"context": parseData.context
"mode": parseData.mode
else
request.send "API Error: #{response.statusCode}"
# Description
# A Hubot script that calls the docomo api
#
# Dependencies:
# None
#
# Configuration:
# HUBOT_DOCOM_API_KEY
# Commands:
# hubot <message>
#
# Author:
# Hirokazu Miyaji
#
module.exports = (robot) ->
docomo = new Docomo(process.env.HUBOT_DOCOMO_API_KEY)
excludeCommands = []
for helpCommand in robot.helpCommands()
commandPrefix = helpCommand.split(' ')[1]
excludeCommands.push commandPrefix if excludeCommands.indexOf(commandPrefix) != -1
robot.respond /(.*)/i, (msg) ->
message = msg.match[1]
return if message is ''
return if excludeCommands.indexOf(message) > 1
docomo.dialogue(msg, msg.message.user.mention_name, message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment