Contao News Twitter Sync

inspiredminds/contao-news-twitter

by inspiredminds

Project web site: Contao News Twitter Sync

This extension allows to automatically import Twitter tweets as news entries. It also enables you to automatically post news entries on a Twitter profile as a tweet. This allows you to keep a Twitter profile in sync with a news archive.

Installation

To install this extension, the composer.json of your Contao installation has to be modified first. Two adjustments have to be done: adding the private repository & adding the dependency.

To add the repository, add the following to your composer.json:

{
    "repositories": [
        {
            "type": "composer",
            "url": "https://token:<YOUR_TOKEN>@packdis.inspiredminds.at/r"
        }
    ]
}

Replace <YOUR_TOKEN> with the repository token you received from inspiredminds.

To add the dependency, add the following to your composer.json:

{
    "require": {
        "inspiredminds/contao-news-twitter": "^2.0"
    }
}
View a full example

After making this adjustment, run a composer update on the command line or update your packages via the Contao Manager. Then open the Contao Install Tool to update the database as usual.

Configuration

To configure this extension, a “Twitter App” needs to created first. The details of this app are then needed for the configuration in the back end.

Creating a Twitter App

  1. First you need to go to apps.twitter.com.
  2. Click on Create New App.
  3. Fill out the basic information.
  4. Fill out the Callback URL with the Contao back end URL, e.g. https://www.example.org/contao.
  5. Click on Create your Twitter application.

Configure the Consumer Key and Consumer Secret in Contao

In Contao, go to System » Settings, scroll down to the Twitter App section, enter the Consumer Key and Consumer Secret. You can find both in the settings of your Twitter App under Keys and tokens.

Configure the Contao News Archive

Go to the settings of your news archive. In the Twitter sync section enable Twitter sync. Now you have the following options:

Authentication: by clicking on the profile picture (or question mark), you can (re-)authenticate with your Twitter account. This is necessary if you want to tweet your news entries to your profile automatically. It is not necessary to authenticate, if you only want to fetch tweets from hashtags or Twitter profiles.

Profile: the Twitter profile where tweets should either be fetched from or posted to.

Hasthtags: space or comma separated list of hashtags that should be fetched from Twitter. Note: if you have also defined a Profile this lists acts as a filter for that Profile only. Without a Profile any public tweets with that hashtag will be fetched.

Fetch tweets: tweets will only be fetched from the given profile (or globally from the hashtags) if this options is active.

Publish tweets: any tweets that are fetched as news entries will be automatically be set to published.

Usage

Fetch Tweets

Once you have configured the news archive and also enabled the Fetch tweets option the extension will check for new Tweets hourly via Contao’s cronjob.

Publish Tweets

Contao news entries will be published as tweets under two conditions:

  • in the news entry’s properties, under the Twitter sync section, the Post to Twitter profile option must be enabled
  • the news entry must be published

You can also define a custom message for the tweet. If no such message is defined, the news entry’s headline is used by default.

If the news entry has a teaser image, that image will also be attached to the tweet.

The extension will check for new news entries to be published minutely via Contao’s cronjob.

Manual Sync Trigger

There is a link in the news archive overview to manually trigger the synchronisation.

Hooks

processTweet

The extension processes a tweet and tries to convert it to a fitting Contao news entry as best as it can. If you want to customize the final data for a news entry of a tweet, you can use the processTweet hook. It expects an array containing the final news entry data as the return value. If the return value equals to false, no news entry will be created.

Parameters

  1. array $arrData The already processed data which will be used for the new news entry.
  2. object $objTweet The original tweet data.
  3. object $objArchive The news archive object.

changeTwitterMessage

When posting a Contao news entry as a tweet, the extension either uses the teaser or the specified message of the news entry. If you want to automatically provide a different message, you can use the changeTwitterMessage hook. It expects the final message as the return value.

Parameters

  1. string $message The already prepared message.
  2. object $objArticle The original Contao news entry.
  3. object $objArchive The news archive object.

Template data

There is additional data available in the news templates:

  • object twitterData The original tweet data
  • string tweetId The tweet ID of the linked tweet
  • char fromTwitter Indicates whether this news entry was fetched from Twitter