Skip to content
This repository
file 77 lines (63 sloc) 2.189 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
# Description:
# Prank text a friend (or enemy)
#
# Dependencies:
# "htmlparser": "1.7.6"
# "soupselect: "0.2.0"
#
# Configuration:
# SMS_USERNAME
# SMS_PASSWORD
# SMS_FROM
#
# Commands:
# hubot drunk-text <number> - send a text to <number>
# hubot prank-text <number> - send a text to <number>
#
# Author:
# vanetix

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
Something went wrong with that request. Please try again.