From 32313e75e1355a065117b3a7507deb84c056af21 Mon Sep 17 00:00:00 2001 From: Travis CI Date: Wed, 13 Jan 2016 23:02:36 -0500 Subject: [PATCH] add to readme. fix package.json to remove fs. fix some comment typos --- README.md | 25 +++++++++++++++++++++++++ package.json | 1 - src/hipchat.coffee | 28 ++++++++++++++-------------- src/response.coffee | 4 ++-- 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 269e929..151baeb 100644 --- a/README.md +++ b/README.md @@ -91,6 +91,27 @@ Bonus: Add a notification hook to Heroku so a notification is sent to a room whe robot.messageRoom("1234_room@conf.hipchat.com", "message"); ``` +## hubot-hipchat specific featues + +In addition to the response methods provided by the normal hubot, such as ```resp.send```, hubot-hipchat provides these hipchat specific functions: +* ```resp.sendHtml``` takes any number of strings and sticks them together as one html message post. For example, you can send a hyperlink by doing this: +``` +robot.respond /send hyperlink/i, (resp)-> + resp.sendHtml "hipchat site" +``` + +* ```resp.sendFile``` takes an object that contains either file data or a path to a file along with other file info and posts the file to a room with an optional message: +``` +robot.respond /send file/i, (resp) -> + file_info = + name : "the name people will see in the room" + path : 'path/to/file.text' + type: "text" # required + msg : "optional message to post when file is uploaded" + + resp.sendFile file_info +``` + ## Adapter configuration This adapter uses the following environment variables: @@ -135,6 +156,10 @@ Optional. Set to `debug` to enable detailed debug logging. Optional. Seting to `false` will prevent the HipChat adapter from auto-reconnecting if it detects a server error or disconnection. +### HUBOT\_HIPCHAT\_TOKEN + +Optional. Set to the value of an API token for HipChat. Generate an API token [here](https://cs10.hipchat.com/account/api) (and give it the necessary scopes to post in rooms, the easiest way is just give it access to all scopes). This needs to be set in order to use the sendFile and sendHtml methods. + ## Running locally To run locally on OSX or Linux you'll need to set the required environment variables and run the `bin/hubot` script. An example script to run the bot might look like: diff --git a/package.json b/package.json index 82b0a87..67c6158 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,6 @@ "node": "~0.12.2" }, "dependencies": { - "fs": "0.0.2", "mime": "^1.3.4", "node-xmpp-client": "^2.1.0", "request": "^2.67.0", diff --git a/src/hipchat.coffee b/src/hipchat.coffee index 2c9a5ee..fa2ab3c 100644 --- a/src/hipchat.coffee +++ b/src/hipchat.coffee @@ -1,12 +1,12 @@ -{Adapter, TextMessage, EnterMessage, LeaveMessage, TopicMessage, User} = require "hubot" +fs = require "fs" HTTPS = require "https" +{Adapter, TextMessage, EnterMessage, LeaveMessage, TopicMessage, User} = require "hubot" {inspect} = require "util" +requestLib = require "request" # requestLib to avoid confusion with adapter's request method +mime = require "mime" Connector = require "./connector" promise = require "./promises" -requestLib = require "request" -fs = require 'fs' -mime = require 'mime' -HipchatResponse = require './response' +HipChatResponse = require './response' class HipChat extends Adapter @@ -14,7 +14,7 @@ class HipChat extends Adapter super robot @logger = robot.logger @room_endpoint = "http://www.hipchat.com/v2/room" - @robot.Response = HipchatResponse + @robot.Response = HipChatResponse reconnectTimer = null emote: (envelope, strings...) -> @@ -49,7 +49,7 @@ class HipChat extends Adapter return @logger.error "Not sure who to send html message to: envelope=#{inspect envelope}" if not @options.token - return @logger.error "A hubot api token must be set to send html messages" + return @logger.error "Must set HUBOT_HIPCHAT_TOKEN to send html messages" room_id = @room_map[target_jid].id fullMsg = strings.join('') @@ -64,10 +64,10 @@ class HipChat extends Adapter requestLib.post params, (err,resp,body) => if err || resp.statusCode >= 400 - return @logger.error "Hipchat API error: #{resp.statusCode}" + return @logger.error "HipChat API error: #{resp.statusCode}" # Send a file from hubot - # options = + # file_info = # name : the name to share the file with # path : send file from this path (a string) # data : send this base64 encoded data as a file @@ -80,7 +80,7 @@ class HipChat extends Adapter return @logger.error "Not sure who to send file to: envelope=#{inspect envelope}" if not @options.token - return @logger.error "A hubot api token must be set to send html messages" + return @logger.error "Must set HUBOT_HIPCHAT_TOKEN to send html messages" room_id = @room_map[target_jid].id url = "#{@room_endpoint}/#{room_id}/share/file" @@ -108,8 +108,8 @@ class HipChat extends Adapter sendMultipart: (path, name, data, mimeType, msg) -> - # Weird stuff from the hipchat api - # Must have filename="name" etc... with double quotes not single + # Must have filename="name" etc... in double quotes not single + quotedName = '"' + name + '"' params = method: 'POST' url: path @@ -124,14 +124,14 @@ class HipChat extends Adapter , { "Content-Type": "file/" + mimeType, - "Content-Disposition": 'attachment; name="file"; filename="' + name + '"', + "Content-Disposition": 'attachment; name="file"; filename=' + quotedName, "body": data } ] requestLib params, (err, resp, body) => if resp.statusCode >= 400 - return @logger.error "Hipchat API errror: #{resp.statusCode}" + return @logger.error "HipChat API errror: #{resp.statusCode}" topic: (envelope, message) -> diff --git a/src/response.coffee b/src/response.coffee index 70e2812..3f7db7a 100644 --- a/src/response.coffee +++ b/src/response.coffee @@ -1,6 +1,6 @@ {Response} = require 'hubot' -class HipchatResponse extends Response +class HipChatResponse extends Response sendFile: (file_info) -> @robot.adapter.sendFile(@envelope, file_info) @@ -8,4 +8,4 @@ class HipchatResponse extends Response sendHtml: (strings...) -> @robot.adapter.sendHtml(@envelope, strings...) -module.exports = HipchatResponse \ No newline at end of file +module.exports = HipChatResponse \ No newline at end of file