Jump to content


Photo

SocialTwitterUpdate

Module

  • Please log in to reply
22 replies to this topic

#1 Pete

Pete

    Forum Admin

  • Administrators
  • 2,380 posts
  • 1427

  • LocationChester, England

Posted 14 January 2012 - 01:33 PM

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.

Attached Files


Edited by Pete, 18 January 2012 - 01:39 PM.
Updated to version 1.1.0


#2 Pete

Pete

    Forum Admin

  • Administrators
  • 2,380 posts
  • 1427

  • LocationChester, England

Posted 14 January 2012 - 02:08 PM

Just an additional few thoughts:
  • 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.
  • 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
  • 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
  • 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 :)

#3 ryan

ryan

    Reiska

  • Administrators
  • 7,783 posts
  • 6531

  • LocationAtlanta, GA

Posted 14 January 2012 - 09:32 PM

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 :)

#4 Pete

Pete

    Forum Admin

  • Administrators
  • 2,380 posts
  • 1427

  • LocationChester, England

Posted 15 January 2012 - 05:11 AM

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.

#5 ryan

ryan

    Reiska

  • Administrators
  • 7,783 posts
  • 6531

  • LocationAtlanta, GA

Posted 16 January 2012 - 04:26 PM

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.

#6 Pete

Pete

    Forum Admin

  • Administrators
  • 2,380 posts
  • 1427

  • LocationChester, England

Posted 16 January 2012 - 04:49 PM

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.

#7 ryan

ryan

    Reiska

  • Administrators
  • 7,783 posts
  • 6531

  • LocationAtlanta, GA

Posted 17 January 2012 - 10:52 AM

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.

#8 Pete

Pete

    Forum Admin

  • Administrators
  • 2,380 posts
  • 1427

  • LocationChester, England

Posted 17 January 2012 - 05:14 PM

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.

#9 Pete

Pete

    Forum Admin

  • Administrators
  • 2,380 posts
  • 1427

  • LocationChester, England

Posted 18 January 2012 - 04:08 AM

Updated the module to fix some errors with variables. It would have worked fine before, but there was an unneccessary line as well as an unnamed variable.

#10 Pete

Pete

    Forum Admin

  • Administrators
  • 2,380 posts
  • 1427

  • LocationChester, England

Posted 18 January 2012 - 01:47 PM

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.

#11 apeisa

apeisa

    Hero Member

  • Moderators
  • 3,152 posts
  • 1697

  • LocationVihti, Finland

Posted 18 January 2012 - 02:36 PM

This sounds great Pete. Now you and Nico need to put these together and build the ultimate social update module that handles Facebook also ;-)

#12 Nico Knoll

Nico Knoll

    The Boss.

  • Members
  • PipPipPipPipPip
  • 784 posts
  • 389

  • LocationBerlin, Germany

Posted 19 January 2012 - 09:38 AM

This weekend I definitely will finish my Facebook plugin :)

#13 MarcC

MarcC

    Sr. Member

  • Members
  • PipPipPipPip
  • 315 posts
  • 107

  • LocationCalifornia

Posted 15 May 2012 - 12:44 PM

Great module. Does this support a suffix with things like hash tags? For example, "/cc @example" or "#processwire #cms" etc., to appear at the end of the Twitter post.

I'm a freelance, processwire-using web designer based in california. work site | personal site | visuals


#14 Pete

Pete

    Forum Admin

  • Administrators
  • 2,380 posts
  • 1427

  • LocationChester, England

Posted 16 May 2012 - 07:12 AM

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.

#15 MarcC

MarcC

    Sr. Member

  • Members
  • PipPipPipPip
  • 315 posts
  • 107

  • LocationCalifornia

Posted 21 May 2012 - 03:16 PM

That would be way cool, Pete. Love to see it when you can share. Thanks!

I'm a freelance, processwire-using web designer based in california. work site | personal site | visuals


#16 Pete

Pete

    Forum Admin

  • Administrators
  • 2,380 posts
  • 1427

  • LocationChester, England

Posted 21 May 2012 - 03:40 PM

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.

#17 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 968 posts
  • 334

  • LocationMidlands, UK

Posted 09 November 2012 - 05:38 AM

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.co...khkhkjhkjhjkhjk

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!

#18 Pete

Pete

    Forum Admin

  • Administrators
  • 2,380 posts
  • 1427

  • LocationChester, England

Posted 09 November 2012 - 06:11 AM

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!

#19 onjegolders

onjegolders

    Hero Member

  • Members
  • PipPipPipPipPip
  • 968 posts
  • 334

  • LocationMidlands, UK

Posted 09 November 2012 - 11:04 AM

Cheers Pete! Also noticed that I was getting repeat tweets, don't know if this is due to editing an existing file...

#20 Pete

Pete

    Forum Admin

  • Administrators
  • 2,380 posts
  • 1427

  • LocationChester, England

Posted 10 November 2012 - 03:49 AM

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.





Also tagged with one or more of these keywords: Module

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users