messaging_twitter.module

Tracking 6.x-4.x branch
  1. drupal
    1. 6 contributions/messaging/messaging_twitter/messaging_twitter.module

Messaging Twitter sending method

Developed by Will White

Development Seed, http://www.developmentseed.org

Functions & methods

NameDescription
messaging_twitter_adminAdmin settings
messaging_twitter_admin_formAdmin settings form
messaging_twitter_admin_form_validateValidate admin settings
messaging_twitter_menuImplementation of hook_menu().
messaging_twitter_messagingImplementation of hook_messaging
messaging_twitter_send_atSend a direct message to Twitter user
messaging_twitter_send_dmSend a direct message to Twitter user
messaging_twitter_user_destinationMap user account to twitter screen name
_messaging_twitter_includeLoad twitter inc only once

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Messaging Twitter sending method
  5. *
  6. * Developed by Will White
  7. *
  8. * Development Seed, http://www.developmentseed.org
  9. */
  10. /**
  11. * Implementation of hook_messaging
  12. */
  13. function messaging_twitter_messaging($op = 'info') {
  14. switch($op) {
  15. case 'send methods':
  16. $info['twitter_dm'] = array(
  17. 'title' => t('Twitter Direct Message'),
  18. 'name' => t('Twitter Direct Message'),
  19. 'group' => 'twitter',
  20. 'address_type' => 'twitter', // Which kind of address this method uses
  21. 'type' => MESSAGING_TYPE_SEND,
  22. 'glue' => ' ',
  23. 'description' => t("Send as a Twitter direct message."),
  24. 'send callback' => 'messaging_twitter_send_dm',
  25. 'system accounts' => TRUE, // Supports multiple sending accounts
  26. 'account type' => 'twitter', // The type of system account to use
  27. );
  28. $info['twitter_at'] = array(
  29. 'title' => t('Twitter @ Message'),
  30. 'name' => t('Twitter @ Message'),
  31. 'group' => 'twitter',
  32. 'address_type' => 'twitter',
  33. 'type' => MESSAGING_TYPE_SEND,
  34. 'glue' => ' ',
  35. 'description' => t("Send as a Twitter @ message."),
  36. 'send callback' => 'messaging_twitter_send_at',
  37. 'system accounts' => TRUE, // Supports multiple sending accounts
  38. 'account type' => 'twitter', // The type of system account to use
  39. );
  40. return $info;
  41. case 'address types':
  42. $info['twitter'] = array(
  43. 'name' => t('Twitter account'), // Name of the address for this method
  44. 'user2address callback' => 'messaging_twitter_user_destination', // Mapping user account to address
  45. );
  46. return $info;
  47. }
  48. }
  49. /**
  50. * Map user account to twitter screen name
  51. *
  52. * @param $account
  53. * User account object or uid
  54. */
  55. function messaging_twitter_user_destination($account) {
  56. static $cache = array();
  57. $uid = messaging_user_uid($account);
  58. if (!array_key_exists($uid, $cache)) {
  59. _messaging_twitter_include();
  60. $twitter_accounts = twitter_get_user_accounts($uid);
  61. if (is_array($twitter_accounts) && !empty($twitter_accounts)) {
  62. reset($twitter_accounts);
  63. $first = current($twitter_accounts);
  64. // It used to be an array, for 6.x-3.x it is an object
  65. $cache[$uid] = is_object($first) ? $first->screen_name : $first['screen_name'];
  66. } else {
  67. $cache[$uid] = NULL;
  68. }
  69. }
  70. return $cache[$uid];
  71. }
  72. /**
  73. * Send a direct message to Twitter user
  74. */
  75. function messaging_twitter_send_dm($destination, $message, $params = array()) {
  76. _messaging_twitter_include();
  77. $url = 'http://twitter.com/direct_messages/new.xml';
  78. $site_username = variable_get('messaging_twitter_username', '');
  79. $site_password = variable_get('messaging_twitter_password', '');
  80. $headers = array('Authorization' => 'Basic '. base64_encode($site_username .':'. $site_password),
  81. 'Content-type' => 'application/x-www-form-urlencoded');
  82. $data = 'text=' . urlencode($message->get_text()) . '&user=' . $destination;
  83. $result = drupal_http_request($url, $headers, 'POST', $data);
  84. return $result && $result->code == 200;
  85. }
  86. /**
  87. * Send a direct message to Twitter user
  88. */
  89. function messaging_twitter_send_at($destination, $message, $params = array()) {
  90. _messaging_twitter_include();
  91. $url = 'http://twitter.com/statuses/update.xml';
  92. $site_username = variable_get('messaging_twitter_username', '');
  93. $site_password = variable_get('messaging_twitter_password', '');
  94. $body = '@' . $destination . ' ' . $message->get_text();
  95. $headers = array('Authorization' => 'Basic '. base64_encode($site_username .':'. $site_password),
  96. 'Content-type' => 'application/x-www-form-urlencoded');
  97. $data = 'status=' . urlencode($body);
  98. $result = drupal_http_request($url, $headers, 'POST', $data);
  99. return $result && $result->code == 200;
  100. }
  101. /**
  102. * Implementation of hook_menu().
  103. */
  104. function messaging_twitter_menu() {
  105. $items = array();
  106. $items['admin/messaging/settings/method/twitter'] = array(
  107. 'title' => 'Twitter',
  108. 'description' => 'Configure the Twitter ID information for the site.',
  109. 'page callback' => 'messaging_twitter_admin',
  110. 'access arguments' => array('administer messaging'),
  111. 'type' => MENU_LOCAL_TASK,
  112. );
  113. return $items;
  114. }
  115. /**
  116. * Admin settings
  117. */
  118. function messaging_twitter_admin() {
  119. _messaging_twitter_include();
  120. $output = '';
  121. $username = variable_get('messaging_twitter_username', '');
  122. $password = variable_get('messaging_twitter_password', '');
  123. if ($username && $password && function_exists('twitter_fetch_account_info')) {
  124. // This will work only for Twitter 6.x-2.x version
  125. $account = twitter_fetch_account_info($username, $password);
  126. $output .= theme('image', $account['profile_image_url'], '', '', array(), FALSE);
  127. $output .= $account['name'];
  128. }
  129. $output .= drupal_get_form('messaging_twitter_admin_form');
  130. return $output;
  131. }
  132. /**
  133. * Admin settings form
  134. */
  135. function messaging_twitter_admin_form() {
  136. $form = array();
  137. $form['messaging_twitter_username'] = array(
  138. '#type' => 'textfield',
  139. '#title' => t('Username'),
  140. '#default_value' => variable_get('messaging_twitter_username', ''),
  141. '#size' => 40,
  142. '#maxlength' => 255,
  143. '#required' => TRUE,
  144. );
  145. $form['messaging_twitter_password'] = array(
  146. '#type' => 'password',
  147. '#title' => t('Password'),
  148. '#default_value' => variable_get('messaging_twitter_password', ''),
  149. '#size' => 30,
  150. '#maxlength' => 64,
  151. '#required' => TRUE,
  152. );
  153. return system_settings_form($form);
  154. }
  155. /**
  156. * Validate admin settings
  157. */
  158. function messaging_twitter_admin_form_validate($form, &$form_state) {
  159. if (function_exists('twitter_authenticate')) {
  160. _messaging_twitter_include();
  161. if (!twitter_authenticate($form_state['values']['messaging_twitter_username'], $form_state['values']['messaging_twitter_password'])) {
  162. form_set_error('name', t('Twitter authentication failed. Please check your username and password.'));
  163. }
  164. }
  165. }
  166. /**
  167. * Load twitter inc only once
  168. */
  169. function _messaging_twitter_include() {
  170. static $include = FALSE;
  171. if (!$include) {
  172. module_load_include('inc', 'twitter');
  173. $include = TRUE;
  174. }
  175. }