It has been a very active obsession of mine to interact with social media, though I might be using it a little differently to your every day status updates, friend requests, and publicity fails.
I am more active at the back end, the underlying structure of the social media platforms, commonly referred to as the Application Program Interface, or A.P.I.
Take Twitter for example… I love how there is this continuous waterfall of messages is just thrown out into the world, all right there in the open to scoop up and play with. Twitter even enables you to access their A.P.I. directly, all you have to do is register as a developer and create an app, simple. You’ll get all the consumer tokens, access tokens, and secret tokens respectively.
The one thing that Twitter requires these days though, which they didn’t in the past, is that you have a phone number attached to your Twitter account, and this phone number can not be in use on another account already, which sucks for me since I am all out of phones to use…
This problem is amplified by my decision not to own or use a mobile phone myself, for reasons that will become all to clear as this website progresses. I will tell you all about mobile phones and the mobile network soon enough, since this is primarily focused on my own, and your, privacy.
Anyway, let’s not get off topic here. Once you have set yourself up to be able to connect to the Twitter A.P.I. you have two choices to connect and start receiving data from Twitter, and even start manipulating the Twitter data directly.
One more thing before we get started on some code… Obviously there is no way Twitter is ever going to let you do anything via their public A.P.I. that you couldn’t do using the service normally. This is merely an ongoing effort of automation and experimentation.
For this, part one on the subject, I will be writing a simple, and quite silly program I came up with that tried to cheat my way into a chat bot. Remember those “psychiatry” bots on the Commodore 64? Or, remember “Eliza”?
Let’s connect to the Twitter A.P.I. first… These code examples are all in Ruby, the full source code to this article is hosted on github.
Firstly, let’s install Sferik’s excellent Twitter Gem:
gem install twitter
Next create a new Ruby script and require the gem:
require 'twitter'
Connect to the REST A.P.I.:
@client = Twitter::REST::Client.new do |config|
config.consumer_key = consumer_key
config.consumer_secret = consumer_secret
config.access_token = access_token
config.access_token_secret = access_secret
end
Note: Replace the consumer_key, consumer_secret, access_token, and access_secret with the details you received when creating your Twitter app.
Now we start an infinite loop, and things get a little weird… I will explain, but have a look at the following code first:
loop do
printf '>'
msg = gets.gsub("\r", '').gsub("\n", '')
if msg.upcase == 'QUIT'
exit
else
@client.search(msg).each do |tweet|
if tweet.reply?
begin
puts @client.status(tweet.in_reply_to_tweet_id).text.to_s.gsub(/\B[@#]\S+\b/, '')
break
rescue Twitter::Error::NotFound
puts 'Whatevs...'
end
end
end
end
end
Basically what we’re doing here is, take some input from the keyboard, so any kind of “conversation” starter. Then we search all of Twitter for some tweet by anybody which matches what we just said to the computer. Next we look through any matching tweets to see if this tweet was in reply to another tweet, and if so we feedback the tweet that our matching tweet was in reply to, to the user that is chatting to our bot.
So in a way, you are now passively chatting with all users on Twitter, sort of turning Twitter into one collective personality, or entity, or some thing. It’s a bit difficult to describe, and it sort of sits at the fringe of experimental and insane…
I wonder if you caught the logical mistake in this system though… Ever since Twitter updated their A.P.I. we have lost one important particular piece of functionality that forces me to currently have this huge reversal of the logic in this script, which makes a significant difference as to how this bot should work, and how it does work…
If you can spot it, good on you, otherwise I will be back to talk about this “bug” soon…
qxzxq