#How I hacked my own pseudo-data plan ##An exploration in being frugal by Amir Sharif
###Preface
I don't have a data plan.
I do however have a social plan, which is pretty much unlimited Twitter & Facebook.
That's all I ever use on my phone anyway, but every now and then I'd like to quickly search the web for something. Something really quick, like what's the population of China? I can't do that.
Another big annoyance is that all the posts on Facebook and Twitter that link to external websites won't load unless the domains of those images are Facebook or Twitter. This prevents me from viewing the images in my feeds that contain anything from Instagram, Imgur, etc. As you can imagine this makes my feed very dull.
###Being resourceful
I have this long running joke with my friends where I ask them to "Orbital Command" (it's a reference to StarCraft) me whenever I need an answer to something fast. The idea is that I'll just send them a text message with what I need, and they'll look it up on the Internet and text me back with the answer.
Wouldn't it be great if I could just code a bot that did that for me? Yes. Yes it would.
But unfortunately creating a robot that responded to text messages is a bit of a hassle, and I don't really have any idea of how that works. But wait! I have Twitter data! So I went on to create a bot on Twitter that essentially would take my queries and send me a message of the response.
###Nitty gritty The project: Create a Twitter bot that takes in direct messages, parses the input, and whips back out a response.
There are a whole bunch of these on Twitter already. But nothing quite like what I had in mind. I wanted some interface that I could query Wolfram Alpha for those quick-to-get questions I have, and also be able to rehost any image back onto Twitter (since I can only view images on pic.twitter.com).
Luckily, this is all pretty easy with a couple ruby gems. I used Chatterbot and Wolfram gems to get things going.
Here's the code I whipped up to run the bot:
require 'chatterbot'
require 'wolfram'
require 'open-uri'
require 'nokogiri'
require 'pp'
Wolfram.appid = 'removed'
bot = Chatterbot::Bot.new
loop do
bot.client.direct_messages_received(:since_id => bot.since_id).each do |msg|
bot.since_id = msg.id if msg.id >= bot.since_id
puts "== NEW MESSAGE =="
puts "Sender: #{msg.sender.screen_name}"
puts "Text: #{msg.text}\n"
command = msg.text.split(' ').first
query = msg.text[command.length+1 .. -1]
response = "Sorry, I couldn't compute your command."
if command.casecmp( 'w' ).zero?
q = Wolfram.query(query)
puts "-> Wolfram: #{query}"
result = (q.result.success? ? q.result.select {|x| x.to_s[0,6] == 'Result' }.first : nil)
if !result.nil?
response = result.to_s
puts "-> Wolfram Result: #{result}"
end
elsif command.casecmp( 'i' ).zero?
# Grab the image file from Instagram
instagram_url = query
puts "-> Instagram: #{instagram_url}"
begin
doc = Nokogiri::HTML(open(instagram_url))
style_text = nil
doc.css('#media_photo span.img').each do |node|
style_text = node.attribute('style').value
end
if !style_text.nil?
image_url = style_text[22..style_text.length-3]
open(image_url) {|f|
File.open("tempImg.jpg","wb") do |file|
file.puts f.read
end
}
puts "-> Instagram Image: #{image_url}"
# We have the image -- now upload to twitter
rehost_tweet = bot.client.update_with_media('Rehost: ', File.new('tempImg.jpg'))
response = "It has been rehosted. Check it out at #{rehost_tweet.text.split(' ')[1]}"
puts "-> Rehosted"
end
rescue Exception => ex
puts "ERROR: " + ex.message
end
end
if response.length > 140
response = response[0..137] + '...'
end
begin
bot.client.direct_message_create( msg.sender.id, response )
rescue Exception => ex
puts "ERROR: " + ex.message
end
puts "\n--> Response: '#{response}'"
end
bot.update_config
sleep 60
end
I fired it up, and it worked! I started sending direct messages to it, things like "What is the population of China." The bot then sends the answer back as a direct message, to my Twitter, and I can check it on my phone. I'm essentially using this bot to query Wolfram, something I was never able to do before.
The image rehosting was a bit trickier, but once you extract the image from Instagram, you just publicly tweet with the bot.
###Whats next?
I still got to support Imgur.
I also want to do some stuff by maybe condensing Google Maps directions so I can use the bot as pseudo-GPS.
Creating a Twitter bot can be a lot of fun. There a whole bunch of cool examples here.