Showing posts with label DM. Show all posts
Showing posts with label DM. Show all posts

Wednesday, 20 November 2013

Twitter Changing Their API Again and Parsing Twitter Tweet Responses

Twitter Changing Their API Again and Parsing Twitter Tweet Responses

If you haven't noticed by now you should do the next time you try and use Twitter on the twitter.com website.

Not only are they re-working their API they are cracking down on the sending of links in DM messages.

I personally have had an account suspended within the last week then re-activated 3 days later and I still have had no explanation why this happened from their support team.

Also this happened directly after I was in contact with them about the problems they were having falsely identifying double encoded links as phishing or spam and directing users of my account to a "Twitter thinks this page might be dangerous" page!

Even though their development team claimed the links I was using were okay and safe if you clicked them you would be redirected to this page by mistake.

I think the problem was due to the fact that to get a link into 140 chars you have to first encode it with a link shortener like bit.ly and then Twitter always encode ALL links with their own t.co shortener so they can track the clicks.

I reckon they were having issues identifying the final destination URL and were therefore flagging double encoded links as spam/phishing/hacks etc - despite their claims they weren't.

However now it seems Twitter has changed their DM API. Not only have I found that it constantly refreshes as I am typing causing me to lose my message but if I receive a message as I type it also refreshes causing the same lost wording.

It seems more like a badly written interactive Messenger service now with an AJAX timer to reload new content than the good old DM service it used to be. They still maybe working on it so hopefully these bugs will get fixed soon.

A few people have complained to me they cannot now send links in D or M messages (Direct Messages) and I have found the same problem. It seems Twitter are cracking down on companies that log people who follow and unfollow you as well as treating the majority of links in DM's as spam, whether they are or not.

Anyway bitching time is now over.

What I wanted to write about was that I had to release new versions of my WordPress plugins Strictly AutoTags and Strictly TweetBot the other day.

I was finding on sites with a lot of posts that the Tweets from the TweetBot (which are hooked into the onpublish action/event) were going out before tagging could be completed. Therefore only the default #HashTags were being used.

Therefore if you wanted to use categories or post tags as your #HashTags in the Tweet they were not available at the point of Tweeting.

Therefore I changed the Strictly AutoTags plugin to fire an event once tagging had been completed in the SaveAutoTags method which enabled Strictly TweetBot to run then instead of on publish. I also pass in the Post ID so the event listener knows which post to send Tweets for e.g:


do_action('finished_doing_tagging', $object->ID);

The code in the TweetBot automatically checks for the existence of the AutoTag plugin by looking for a function that I use within the plugin that will only exist if the plugin is active.

If this is found the TweetBot hooks the listener to fire off Tweets into this event instead of the standard on publish event.

As I was explaining to a WordPress developer the other day, I prefer the use of the terms events and listeners due to my event driven coding in JS or C# etc whereas hooks and actions are more WordPress driven.

However as I was re-writing this code I also noticed that in the admin panel of Strictly TweetBots which shows a history of the last Tweets sent and any error messages returned from Twitter that I was not getting back my usual "Duplicate Tweet" error when I tried sending the same tweet multiple times.

By the looks of things the JSON response from Twitter when using OAuth to send a Tweet has changed slightly (when I don't know) and now I have to parse the object and containing array for any error messages rather than checking just for a string as I used to be able to do.

If anyone is interested, or works with OAuth to send Twitter messages, the code to parse the JSON response to collect error messages is below.

Please notice the ShowTweetBotDebug debug function which outputs arrays, objects and strings to the page if needed.


/*
 * My Show Debug Function
 *
 * @param $msg string, object or array
 */
function ShowTweetBotDebug($msg)
{ 
 if(!empty($msg))
 {
  if(is_array($msg)){
   print_r($msg);
   echo "<br />";
  }else if(is_object($msg)){
   var_dump($msg);
   echo "<br />";
  }else if(is_string($msg)){
   echo htmlspecialchars($msg) . "<br>";
  }
 } 
}



$twitterpost = "Some tweet about something";

ShowTweetBotDebug("post this exact tweet = $twitterpost");

// Post our tweet - you should have set up your oauth object before hand using the standard classes
$res = $oauth->post(
 'statuses/update', 
 array(
  'status' => $twitterpost,
  'source' => 'Strictly Tweetbot'
 )
);



ShowTweetBotDebug("response from Twitter is below");

//output errors using vardump to show whole object
ShowTweetBotDebug($res);

// parse error messages from Twitter
if($res){
 // do we have any errors?
 if(isset($res->errors)){
  
  ShowTweetBotDebug("we have errors");

  // if we do then it will be inside an array
  if(is_array($res->errors)){

   ShowTweetBotDebug("we have an array of errors to parse");

   foreach($res->errors as $err){     
    
    ShowTweetBotDebug("Error = " . $err->message);        
   }

  }
 }else{
  ShowTweetBotDebug("tweet sent ok");  

 }
}else{
 ShowTweetBotDebug("Could not obtain a response from Twitter");
}


This code might come in handy to other people who need to parse Twitter responses and just remember - Twitter is ALWAYS changing their API which is a real pain the ass to any developers using their API or trying to make a business model from it.

It seems a lot of companies have had to shut down due to the recent changes to Direct Messages so who knows what they will do in the future?

Thursday, 13 June 2013

Changes to Twitter API

Changes to Twitter API 1.1

As Twitter has changed their API from 1.0 to 1.1 which is totally reliant on oAuth and JSON I have had to take down the links to the Twitter Hash Tag Scanner as it was reliant on the old search RSS feeds which are no longer available.

I have had a quick look but it will take some time to re-develop and involve adding in consumer keys, access keys and so on. You would probably get blocked after a few scans anyway as you would have to login to your own twitter account to make the scans and their rate limits would apply.

Therefore I don't think a new version will be forthcoming to anyone who has purchased a previous version I can only apologise. It's a shame as I wanted to extend it but if I cannot make thousands of scans without being blocked the application just won't work with Twitters new API.

As for the Strictly TweetBot Wordpress plugin I have updated this to use the new API and I have tested it on a couple of my own blogs and it seems to be working.

Today was the switch off day so if you were using the plugin you would have noticed either:
  • No tweets being sent out when you posted.
  • In the Twitter message console lots of error messages saying Tweet not sent or Authentication error.
However if you upgrade to version 1.1.3 then this should fix the problem. 

You can get the latest version from Wordpress.

Also I am pissed off!

And I only just wrote a Twitter Direct Message Responder in PHP the other day which was working fine up until tonight as well!

Damn bloody Twitter.

Even with me being logged in and authenticated I was trying to get a list of my followers and for some reason I kept getting a message like this:

{"errors":[{"message":"Bad Authentication data","code":215}]}

I did write a post to the developer discussion boards on Twitter but as always I have cracked the problem before I got a response.

Basically I am using a very common Twitter / oAuth class which is used by my Twitter Plugin and many other plugins use it as well.

To fix the problem I had to do the following:

Change line 29 in the Twitter class to:

 /* Set up the API root URL. */
 public $host = "https://api.twitter.com/1.1/";


This resolved the issues in my own wordpress plugins which solved sending normal tweets out but to get my Direct Message Responder code working I needed to do one more thing.

Whereras before I was making use of a simple file_get_contents call to an XML feed which Twitter has now abandoned for JSON I had to change this to use the inbuilt HTTP request functions in the Twitter class e.g

$response = $oauth->get($followers_url);

This returns 20 of your new followers (I have not worked out how to get more yet) but in a JSON object.

You can either loop through the nested objects of you could use json_encode to convert the object to a string to do a simple regex to just get a list of screen_names e.g

$body = json_encode($response);

preg_match_all('@"screen_name":"([\s\S]+?)",@i',$body,$matches,PREG_SET_ORDER);

And that solved the problem!