function twitter_drush_tweet in Twitter 7.6
Same name and namespace in other branches
- 6.5 twitter.drush.inc \twitter_drush_tweet()
- 7.5 twitter.drush.inc \twitter_drush_tweet()
Callback for Drush command 'twitter-tweet'.
Posts a new status to Twitter using one of authenticated accounts.
1 string reference to 'twitter_drush_tweet'
- twitter_drush_command in ./
twitter.drush.inc - Implements COMMANDFILE_drush_command()
File
- ./
twitter.drush.inc, line 108 - Drush commands for the Twitter module.
Code
function twitter_drush_tweet($tweet = NULL, $screen_name = NULL) {
// Bail out if no tweet was supplied.
if (!isset($tweet)) {
return drush_set_error(dt('No tweet text was supplied.'));
}
// Bail out if the tweet is too long.
if (drupal_strlen($tweet) > 140) {
return drush_set_error(dt('This is !chars chars long, only 140 allowed.', array(
'!chars' => drupal_strlen($tweet),
)));
}
// Work out what user account to test from.
$uid = intval(drush_get_option('user'));
$account = user_load($uid);
if ($uid > 0 && empty($account)) {
return dt('User account @uid could not be found.', array(
'@uid' => $uid,
));
}
// Verify the user has permission to post to Twitter.
if (!user_access('post to twitter', $account)) {
return dt('User account @uid does not have permission to tweet.', array(
'@uid' => $uid,
));
}
// Get a list of all authenticated accounts for the current user. If no user
// was selected it will default to the anonymous user and therefore only list
// global accounts.
module_load_include('inc', 'twitter');
$access_global = user_access('post to twitter with global account', $account);
$accounts = array();
foreach (twitter_load_authenticated_accounts($account->uid, $access_global) as $twitter_account) {
$accounts[$twitter_account->screen_name] = $twitter_account;
}
// Bail out if there are no authenticated accounts.
if (empty($accounts)) {
if ($uid > 0) {
$message = dt('There are no global authenticated Twitter accounts or accounts owned by user @uid to post from.', array(
'@uid' => $uid,
));
}
else {
$message = dt('There are no global authenticated Twitter accounts to post from.');
}
return drush_set_error($message);
}
// Bail out if a screen name was supplied but is not valid.
if ($screen_name && !key_exists($screen_name, $available)) {
return drush_set_error(dt("'!name' not available. Omit screen name to be prompted to choose among available accounts.", array(
'!name' => $screen_name,
)));
}
// If no screen name was supplied, prompt for it.
if (empty($screen_name)) {
$screen_name = drush_choice($accounts, 'Choose an account to post from.', '!key');
if ($screen_name === FALSE) {
return;
}
}
// Tweet the message.
try {
twitter_set_status($accounts[$screen_name], $tweet);
drush_log(dt('If you see nothing above this line, we successfully posted to Twitter.'), 'success');
} catch (TwitterException $e) {
drush_set_error(dt('An error occurred when posting to Twitter: @message', array(
'@message' => $e
->getMessage(),
)));
}
}