Jump to content

SocialTwitterUpdate


Pete
 Share

Recommended Posts

As per this thread, I've been working on a module this afternoon which automatically posts a tweet on Twitter when a page is saved.

Important Settings

  • You must have a Twitter account
  • You need to sign up for a dev account, as you will need a consumer key, consumer secret, oAuth Token and oAuthTokenSecret from Twitter
  • When creating your "application" on Twitter, please ensure you set it to read/write and not just read, else it won't be able to post!
  • You can enter a prefix for your url, such as "Check out our latest blog post:"
  • You will need to select "categories" whose children this module will apply to. Eg. if you have two root pages called News and Gallery and you want to post a tweet about any new pages created directly under those sections, then select them from the Allowed Parent Pages dropdown (if nothing is selected in this field, the module simply doesn't do anything ;)).
  • There ar two Allowed Parent Pages lists as of v1.1.0 - the second one is for tweets with a photo upload to Twitter and will attempt to upload the first image from the "images" field for the current page (if the field exists and there is at least one image uploaded).

Optional

  • I thought that whilst I was at it I might as well add bit.ly support, so you will need your bit.ly username and a bit.ly API key to have your URLs automatically shortened, else it will post normal URLs in your tweet

I've also put a decent amount of error checking in there so it should warn you if there's anything missing.

You don't have to worry about double-tweets. The class I used to connect to Twitter will prevent this, although when you save the page it will still tell you that your tweet was posted successfully.

To delete any tweets you've created whilst testing, or tweets you don't want for whatever reason, log in to Twitter to delete them.

Credits

As from 1.1.0, the class used is from Matt Harris who is on the Twitter dev team (@themattharris) - his full library is on GitHub and contains a lot of good examples for interacting with Twitter in other ways. I switched modules as Matt's class allows for the picture upload functionality.

I'm leaving in the bit below about Tijs' class as he has a lot of other good classes and without his Twitter class I wouldn't have actually been able to start work on this modue at all.

Aside from the usual thanks to those module authors whose code I've inspected to build this (mostly ryan I think this time - I worked out the ASMSelect from the InputfieldPage module), none of this would be possible at all without Tijs Verkoyen's Twitter and bit.ly classes which are bundled with this module.

Check out all of his awesome classes here: http://classes.verkoyen.eu/

EDIT: I've attached the modile as a zip file to this post since Github is being a git and won't let me push my commit for some reason.

SocialTwitterUpdate.zip

Edited by Pete
Updated to version 1.1.0
  • Like 1
Link to comment
Share on other sites

Just an additional few thoughts:

  1. It would be nice to upload photos to Twitter so this could be used with galleries, but this class doesn't have that functionality, nor amI entirely sure the Twitter API allows you to do it through the API.
  2. It doesn't include any check for length before trying to post the tweet. I'm pretty certain however that a tweet that is too long will still throw an exception so you should still see an error message
  3. Carrying on from number 2, it would be nice to have a character counter next to the Tweet Text field so you know how many characters you're using
  4. Carriny on from 3, it would also be nice if there was some indicator when editing the page to give you an idea of how the tweet would look as well as how many characters, so you could override the default Tweet Text value if the whole thing with the page URL makes it too long

All of these are just random thoughts - if someone wants to implement any of them then please feel free, however I've gone as far as I can and as far as I need to with this version so I don't plan on doing much more to it except any bug fixes :)

Link to comment
Share on other sites

This sounds awesome Pete, I can't wait to try it out along with the other stuff you sent me today. It seems like every time I have to take a few days away from the computer, all the coolest stuff gets released :)

Link to comment
Share on other sites

Hehe, it's only because I tend to have more time to look at it on the weekend, although I must admit I thought this would take longer and contain less features than it ended up having.

Link to comment
Share on other sites

Pete I was able to test this out today, and really impressed with what you've put together here. Quite a lot of work went into this, and it looks like you've done a great job with the code and functionality here. I signed up for the Twitter dev account and plugged everything in, but unfortunately wasn't able to make it work because my dev server isn't accessible to Twitter. However, I was able to get a good understanding of how it works and what the possibilities are here and it's quite exciting. I've already got a plan to use this with an upcoming client project, and this is going to be perfect for them.

The code looks great here. The only suggestion I could come up with was to try out your module config screen with $config->debug enabled. It looks like there are a couple initialized variables (line 111, 115 and 121). Minor issue, but just wanted to mention it since it causes error messages to appear when debug mode is on. This is my first experience with the Twitter API and I must say I really like it and what you've implemented here. Nice work.

Link to comment
Share on other sites

Thanks ryan - you're right, I didn't have debug mode on whilst I was coding it so that's one to remember in future and I'll take a look at those variables at some point this week.

As for the code, it was made reasonably easy with Tijs' classes, and I'll admit I know very little about "try catch" (I think those blocks of code were adapted from one of his examples) but I did have fun implementing that ASMSelect in the config (I was surprised how simple it was in the end actually once I worked it out).

I'm actually getting pretty into the idea of turning third party classes into installable, configurable modules.

I've got a few fun ideas I'd like to try out at some point involving automated weekly emails using something like PHPMailer or perhaps MailChimp - with good enough classes and good enough reasons to turn them into modules the options are pretty limitless, though obviously licensing issues come into play in some cases.

Link to comment
Share on other sites

I'm actually getting pretty into the idea of turning third party classes into installable, configurable modules.

Great ideas, keep it up!

I'm planning to use this SocialTwitter module on the new PW site so that every time a new module or site is added, it'll automatically post to the Twitter feed. This is going to be a big time saver.

Link to comment
Share on other sites

With regards to this module, I'm looking to expand the included Twitter class to support sending photos as well (seems logical since Twitter now natively supports photos so they display inline in feeds). It's needed for a website I'm building so it makes sense.

My only worry about that idea is that at present you can select multiple parent pages whose child content will be auto-tweeted. How could I go about optionally selecting which categories would send a photo to Twitter?

The most likely option is that I'll have a config option to turn photo uploads on/off and have it just be a global setting, so if there's an images field in the template, it just takes the first picture and uploads it.

I suppose another option would be to have two more config fields - one for "sections whose children upload the first image from the images field" and "sections whose children upload ALL images from the images field" and have them as ASMSelects based off the first ASMSelect (if that's possible). Or just a comma-separated list. The latter option of uploading all images would be good for news sections for any type of site, but in my mind I've got a gaming site with 3 exclusive photos accompanying a new article for example. That assumes Twitter will allow multiple images per tweet and what the limits are.

Link to comment
Share on other sites

Just updated this to version 1.1.0 as it now supports the ability to upload an image with a post :D

There's now an additional field called allowedPagePhotoParents that lets you pick page parents that you want to have the child pages upload an image along with a tweet.

The original allowedPageParents field is still there, so you can specify sections where a new page will only create a text tweet, and if you select the same page in both lists, the photo field will take priority and it will try and upload a photo (seemed the easiest way to get around two conflicting values in the lists).

If the page you save is in a section that is in the allowedPagePhotoParents list, it will check for an images field on that page and, if it finds the field and there is at least one image in it, it takes the first image and passes it along to Twitter along with the link to the page as well as your optional configurable tweetText prefix text.

EDIT: Only slight thing is that this class comes with some sort of certificate file (part of advanced authentication I think) so the module package is a lot bigger. It shouldn't be a problem though as the module will only be run when a page is saved, and that certainly doesn't happen every second.

Link to comment
Share on other sites

  • 3 months later...

Funnily enough I do have this in a later version that I've not quite polished up yet, so I'll take a look at it soon.

My requirement was that articles (which would produce the tweet) could be linked to various categories or companies, each of which had their own field for hashtags/DM's etc. There was also some code to prevent it posting duplicate hashtags and so on.

I'll dig out the code hopefully tonight.

Link to comment
Share on other sites

Hi Marc

No probs - I'll dig it out soon (honest ;)). I was testing a newer version today and it had a few unwanted side-effects so I'll have to sort those out first.

Link to comment
Share on other sites

  • 5 months later...

Hiya Pete,

Wanted to ask if there was anyway to output the page's title in the link somehow. At the moment I can either have -

New blog article - bit.ly/hkhkhkkkh OR

New blog article - http://www.mysite.com/blog/jkhjkhhkhkhkjhkjhjkhjk

Would be great to have it output the page's name though I have no idea if that's even possible.

Thanks anyway for a really useful module!

Link to comment
Share on other sites

I'll have a look at it at the weekend when I'm back at my PC but it should be possible and I think I have a newer version with this in already.

I really need to remember to look at this as it looks like I was going to back in March!

Link to comment
Share on other sites

I think that that was one of the side-effects - so every time a page is saved in a certain version of this module it sends the tweet (so if you edit it).

I think this was due to a change in Twitter as previously they didn't allow identical tweets, but they do now - there are some cases where it makes sense in fact but not in our case.

Link to comment
Share on other sites

  • 4 months later...
Hey Pete,

Thanks for the module. Just wanted to let you know of a couple of small issues. I get this error:

Notice: Undefined index: allowedPagePhotoParents in xxx/site/modules/SocialTwitterUpdate/SocialTwitterUpdate.module on line 113

Also, the page parents selectors are described as "A comma-separate list" which is not really the case for the user with your ASM implementation.
Link to comment
Share on other sites

  • 1 year later...

Thanks for the module. Just wanted to let you know of a couple of small issues. I get this error:

Notice: Undefined index: allowedPagePhotoParents in xxx/site/modules/SocialTwitterUpdate/SocialTwitterUpdate.module on line 113
 
Also, the page parents selectors are described as "A comma-separate list" which is not really the case for the user with your ASM implementation.

Get the same error after installation, after filling the credentials it looks good first.

But I don't get any errors or messages at all when publishing a page under the allowed parent.

No idea what to  :( would love to use it :)

Update: Just checked again, hook is getting fired. The module compatibillity is only up to 2.2 this might be the reason..?!

Update 2.: I'm now using Codebird. Works like a charm.

My hook looks like this 

if(empty($this->page->tweet_id)) {
	$this->pages->addHook('Pages::saveReady', $this, "sendTweet");
}

so only if tweet_id is empty it will get fired and Pages::saveReady because it's saving the tweet_id which I'm using to create a retweet button for blog posts

Update 3: My example above is probably not correct. Maybe I should make a little break I get a little confused ^^

Edited by Can
Link to comment
Share on other sites

  • 8 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...