Skip to content
Browse files

Merge pull request #326 from vanetix/new-scripts

Prank-text script
  • Loading branch information...
2 parents ce54281 + 8c4a862 commit 70b9a4ab34406351a59615f5a603ce73264cc529 @tombell tombell committed Mar 3, 2012
Showing with 63 additions and 0 deletions.
  1. +63 −0 src/scripts/prank-text.coffee
View
63 src/scripts/prank-text.coffee
@@ -0,0 +1,63 @@
+# Prank text a friend (or enemy)
+#
+# drunk-text <number> - send a text to <number>
+# prank-text <number> - send a text to <number>
+#
+
+HTMLParser = require("htmlparser")
+Select = require("soupselect").select
+QueryString = require("querystring")
+util = require('util')
+
+
+TFLN = "http://textsfromlastnight.com/Random-Texts-From-Last-Night.html"
+SMSIFIED = "https://api.smsified.com"
+SMS_USERNAME = process.env.SMS_USERNAME
+SMS_PASSWORD = process.env.SMS_PASSWORD
+SMS_FROM = process.env.SMS_FROM
+
+
+module.exports = (robot) ->
+ robot.respond /(drunk-text|prank-text) (\d+)/i, (msg) ->
+ number = msg.match[2]
+
+ retrieveText msg, (text) ->
+ if text is false
+ msg.send "An error occurred while getting a random text."
+
+ sendText msg, number, text, (status) ->
+ if status is true
+ msg.send "Message: #{text} has been sent to #{number}."
+ else
+ msg.send "An error occurred while sending the text."
+
+
+sendText = (botHandle, to, text, callback) ->
+ payload = QueryString.stringify address: to, message: text
+ authString = "Basic " + new Buffer(SMS_USERNAME + ":" + SMS_PASSWORD).toString("base64")
+
+ botHandle
+ .http(SMSIFIED)
+ .path("/v1/smsmessaging/outbound/#{SMS_FROM}/requests")
+ .header("Authorization", authString)
+ .header("Content-Type", "application/x-www-form-urlencoded")
+ .post(payload) (err, res, body) ->
+ if not err? and res.statusCode is 201
+ callback true
+ else
+ callback false
+
+
+retrieveText = (msg, callback) ->
+ msg.http(TFLN).get() (err, res, body) ->
+ if res.statusCode is not 200 or err?
+ callback false
+ else
+ handler = new HTMLParser.DefaultHandler(((err) ->
+ callback 'Problem parsing the text.' if err
+ ), ignoreWhitespace: true )
+ parser = new HTMLParser.Parser handler
+ parser.parseComplete body
+ nodes = Select handler.dom, "#texts-list li .text p a"
+
+ callback nodes[0]?.children[0]?.data || false

0 comments on commit 70b9a4a

Please sign in to comment.
Something went wrong with that request. Please try again.