Below is some code to programatically post a custom message to Twitter using the current user's configured Twitter account. Sometimes you want to tweet something other than a node and I couldn't find an easy way of doing this, so last night I hacked up this piece of code. If you know better way to do it, please tell me. To make it work in your module, change "mymodule" to whatever your module is called. Because of the otherwise excellent Twitter module's notorious lack of return value checks, I modified the twitter_set_status function in twitter.inc to actually collect the returned status. Since the return value is never collected in the original function, it won't effect any existing code. In twitter.inc, just change the line
$twitter->status_update($status);
to
return($twitter->status_update($status));
and my error checking code will work ok. There are still some checks that are missing, e.g. the commented parts, so if anyone has an idea, please share.
<?php
function mymodule_publish_on_twitter($message) {
global $user;
module_load_include('inc', 'twitter');
// Load and connect user's twitter account
$account = twitter_get_user_accounts($user->uid);
if (!$account) {
$err_message = t("Unable to find your Twitter account. ") . l(t('Click here to a Twitter account to your profile.'),'user/' . $user->uid . '/edit/twitter');
drupal_set_message($err_message, 'warning');
return FALSE;
}
// Check message length
if (strlen($message) > 140) {
$err_message = t("Unable to set your Twitter status. Your status text is too long. The maximum message size on Twitter is 140 charaters.");
drupal_set_message($err_message, 'warning');
return FALSE;
}
$twitter_account = twitter_account_load($account[0]->id);
// TODO: Connected ok check should go in here
// Try to set twitter status
$status = twitter_set_status($twitter_account,$message);
// Status is always returned but empty if setting the status didn't succeed
// Check for errors
if ($status && $status->id < 1) {
/* This should work but doesn't
$twitter = twitter_connect($twitter_account);
$params = array();
if ($since) {
$params['since_id'] = $since;
}
$err_message = t("Couldn't set your status on Twitter.");
$statuses = $twitter->user_timeline($account->id, $params, $account->protected);
foreach ($statuses as $status) {
if ($status->text == $message) {
$err_message = t("Status text is a duplicate of a recent tweet and Twitter didn't accept it. Try modifying the text a bit.");
break;
}
} */
$err_message = t("Couldn't set your status on Twitter. If the status text is a duplicate of a recent tweet, Twitter doesn't accept it. Try modifying the text a bit.");
drupal_set_message($err_message, 'warning');
} else {
$message = t("Twitter status updated.");
drupal_set_message($message, 'status');
// Save status if all is ok
twitter_status_save($status);
return TRUE;
}
return FALSE;
}
?>
Comments
Comment #1
escore commentedFinally got around to fixing the non-working parts. In the above code, replace lines 27-47 with the following code:
Comment #2
nbailly commentedAwesome code! I've been trying to figure out how to do exactly the same thing, but I am not super saavy at building Drupal modules so this came in VERY HANDY!
Thanks for the contribution.
Comment #3
escore commentedGlad to be of help! I've been missing several useful API calls in the Twitter module, so any day soon I'll try to bake my first patch to make them easily available.
Comment #4
ayathullah commentedHi,
I've been looking for something like this for quite a while. Just wanted to say thanks for the contribution.
But I've got a specific request...
How do you set up a rule to post a message to the current user's Twitter account? At the moment, the form in the "Post a message to Twitter" action only permits a absolute value. I want to use a token for the currently logged in user's Twitter account.
Perhaps you can point me in the right direction as to what I should change in the code below.
I'm pretty certain that someparts of your code above will come in handy just not sure exactly what.
Any Help would be greatly appreciated.
Many Thanks,
ayathullah
Comment #5
escore commented@ayathullah: Happy if my code is of help to you. As for your question, since it's really a different question, please submit it as a new issue and I'll add my two cents, although I don't think I have much to offer.
Comment #6
ayathullah commented@escore
I have posted it as a new issue please visit the link http://drupal.org/node/1135026
Comment #7
escore commentedJust posted my reply there.
Comment #8
BManuel commentedIf make this custom module, is it enough to update user's status or more coding is required?
BM
Comment #9
michaek commentedHi, folks. Is the use case for this something like: "as a module developer, I want to be able to implement a hook to post an arbitrary message to Twitter, as the current user"?
It's definitely one of the goals of this module to provide an API for other modules to use, and I think there's good reason to make sure this functionality is present in the module and provide documentation for it.
Comment #10
BManuel commentedYes and to also allow some automated status update as the current user does certain activities on the site.
This what I personally need it to do.
1. update status upon successful registration
2. Update status upon adding some defined content type
3. Update status upon flagging a particular content type or user
4. update status periodically advertising the current user post (user can enable/disable this)
5. (Optional) tracking update status, in order to identify what users are the super stars.
Buddy can you implement this module, if the cost is reasonable, I pay for it.
Thanks
BM
You can send me private mail to discuss pricing
Comment #11
ayesh commentedUse Rules module, with custom token module.
You will need some db_fetch php code to get the token though.
Comment #12
BManuel commented@Ayesh I am no programmer hence I am willing to pay to get this done :)
Thanks again for the responses
Comment #13
ayesh commentedNot yet tested!
Comment #14
steinmb commentedComment #15
phelix commentedI don't know if I am just completely over looking something but I have the twitter and twitter actions modules installed and have tried using functions from your bit of code. Every function I try to call says that it can not find it. I have also added the module include at the top. But I am getting errors like this.Call to undefined function twitter_get_user_accounts() Any idea on what I could be missing here?
Comment #16
13rac1 commentedThis functionality is supported now using Rules due to #1045304: Rules: Post to current user's Twitter account.
Comment #17
juampynr commentedMarking as duplicate.
Comment #18
AMMAR ALDUMAINI commentedلعبة بوبجي
صحة
يمن نت