Skip to content
This repository has been archived by the owner on Jun 8, 2023. It is now read-only.

Commit

Permalink
adding hear_and_respond function plus rolling a new version of scribe…
Browse files Browse the repository at this point in the history
…-node
  • Loading branch information
markomanninen committed Nov 23, 2011
1 parent 536d86c commit 4726ed3
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"underscore.string": ">= 1.1.6",
"cradle": "0.5.7",
"clark": "*",
"scribe-node": "0.0.19"
"scribe-node": "0.0.20"
},

"directories": {
Expand Down
28 changes: 21 additions & 7 deletions src/scripts/oauth.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -54,41 +54,55 @@ handle_refresh = (robot, msg) ->
else
msg.send "Access token not found"

# small factory to support both gtalk and other adapters by hearing all lines or those called by bot name only
hear_and_respond = (robot, regex, callback) ->
robot.hear eval('/^'+regex+'/i'), callback
robot.respond eval('/'+regex+'/i'), callback

module.exports = (robot) ->
robot.hear /^get ([0-9a-zA-Z].*) authorization url$/i, (msg) ->
hear_and_respond robot, 'get ([0-9a-zA-Z].*) authorization url$', (msg) ->
handle_authorization robot, msg

robot.hear /^set ([0-9a-zA-Z].*) verifier (.*)/i, (msg) ->
hear_and_respond robot, 'set ([0-9a-zA-Z].*) verifier (.*)', (msg) ->
handle_verification robot, msg

robot.hear /^refresh ([0-9a-zA-Z].*) token$/i, (msg) ->
hear_and_respond robot, 'refresh ([0-9a-zA-Z].*) token$', (msg) ->
handle_refresh robot, msg

robot.hear /^get ([0-9a-zA-Z].*) request token$/i, (msg) ->
hear_and_respond robot, 'get ([0-9a-zA-Z].*) request token$', (msg) ->
if token = new scribe.OAuth(robot.brain.data, msg.match[1].toLowerCase(), services).get_request_token()
message = "Request token: " + token.getToken()
else
message = "Request token not found"
msg.send message

robot.hear /^get ([0-9a-zA-Z].*) access token$/i, (msg) ->
hear_and_respond robot, 'get ([0-9a-zA-Z].*) access token$', (msg) ->
if token = new scribe.OAuth(robot.brain.data, msg.match[1].toLowerCase(), services).get_access_token()
message = "Access token: " + token.getToken()
else
message = "Access token not found"
msg.send message

robot.hear /^get ([0-9a-zA-Z].*) verifier$/i, (msg) ->
hear_and_respond robot, 'get ([0-9a-zA-Z].*) verifier$', (msg) ->
if token = new scribe.OAuth(robot.brain.data, msg.match[1].toLowerCase(), services).get_verifier()
message = "Verifier: " + token.getValue()
else
message = "Verifier not found"
msg.send message

robot.hear /^remove ([0-9a-zA-Z].*) authorization$/i, (msg) ->
hear_and_respond robot, 'remove ([0-9a-zA-Z].*) authorization$', (msg) ->
api = msg.match[1].toLowerCase()
if robot.brain.data.oauth_user and robot.brain.data.oauth_user[api] == msg.message.user.reply_to
message = "Authorization removed: " + new scribe.OAuth(robot.brain.data, api, services).remove_authorization()
else
message = "Authorization can be removed by original verifier only: " + robot.brain.data.oauth_user[api]
msg.send message

hear_and_respond robot, 'set ([0-9a-zA-Z].*) access token (.*)', (msg) ->
api = msg.match[1].toLowerCase()
if new scribe.OAuth(robot.brain.data, api, services).set_access_token_code(msg.match[2])
robot.brain.data.oauth_user[api] = msg.message.user.reply_to
message = "Access token set"
else
message = "Error on setting access token"
msg.send message
15 changes: 10 additions & 5 deletions src/scripts/spin.coffee
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Spin & spun text formatter script
#
# Script can be used to generate different combinations of the input text by variating synonyms for example.
# Script can be used to generate different combinations of the input text by variating synonyms.
#
# 1. supports nested sets
# 2. supports multiline input
Expand Down Expand Up @@ -94,17 +94,22 @@ spun = (msg, spinner = null, robot_brain = null) ->
robot_brain.data.spin[msg.message.user.reply_to] = body.message
msg.send body.message

# small factory to support both gtalk and other adapters by hearing all lines or those called by bot name only
hear_and_respond = (robot, regex, callback) ->
robot.hear eval('/^'+regex+'/i'), callback
robot.respond eval('/'+regex+'/i'), callback

module.exports = (robot) ->
robot.hear /^spin me ([\s\S]*)/i, (msg) ->
hear_and_respond robot, 'spin me ([\\s\\S]*)', (msg) ->
msg.send spin msg.match[1]

robot.hear /^spun me ([\s\S]*)/i, (msg) ->
hear_and_respond robot, 'spun me ([\\s\\S]*)', (msg) ->
spun(msg, null, robot.brain)

robot.hear /^spun and spin me ([\s\S]*)/i, (msg) ->
hear_and_respond robot, 'spun and spin me ([\\s\\S]*)', (msg) ->
spun(msg, spin)

robot.hear /^spin the last spun$/i, (msg) ->
hear_and_respond robot, 'spin the last spun$', (msg) ->
# get user centric input from memory
if @robot.brain.data.spin[msg.message.user.reply_to]
msg.send spin robot.brain.data.spin[msg.message.user.reply_to]
Expand Down

0 comments on commit 4726ed3

Please sign in to comment.