Hi, I've created a module that sends reminders to users to check-in weekly to my site. It uses hook_cron. I was just sending them with drupal_mail, but I'd like to leverage Messaging Framework to have more sending methods.

I'm calling hook_messaging and setting up my Message Group as "Check-In Reminder". I have set up Message Templates and Tokens, but I can't figure out how to actually call these into my module when I want to send the message.

Right now, I've replaced my call to drupal_mail with this:

$user = user_load(array('uid'=>$user->uid));
	  
$message = array();
$message['type'] = 'checkin_reminder';

messaging_message_send_user($user, $message);

I have this working for email and SMS, but the messages are blank. Do I have to call more functions before I can call messaging_message_send_user, to actually assemble the message from the templates?

Reading the documentation here http://drupal.org/node/252684 for Message Producers leads to me believe I'm on the right track, but missing something.

Any help would be appreciated, thanks.

Comments

jschulz-1’s picture

I've been trying to figure out what the different pieces of the Messaging module do. So I've added a bit to my code. Seems like you have to call the parts of the message template first before calling messaging_message_send_user. I thought the functions in Messaging would render everything if you had your templates established, but maybe not.

So here's what I have now.

  $user = user_load(array('uid'=>$user->uid));
	  
	$message = array();
	$message['type'] = 'checkin_reminder';
	$message['subject'] = messaging_message_part('checkin_reminder', 'subject', $user->messaging_default);
	$message['body'] = messaging_message_part('checkin_reminder', 'main', $user->messaging_default);

	messaging_message_send_user($user, $message);

This grabs the parts of the template okay, but does not do token replacement. In the admin settings for my Check-in Reminder templates it lists a bunch of available tokens that I've specified in hook_messaging ($op = 'tokens'). However, these are not getting replaced. I've tried different Input Filters with no luck. I cannot find in the code where it does this token replacement.

Is this how modules should send messages? By calling messaging_message_part for each part and then messaging_message_send?

Thanks for any guidance, Messaging seems like a solid module (I just need to figure out the logic).

jschulz-1’s picture

Well, I think I've finally got what I need. I'm not sure if this is the best method, but I'll document what I've done here in case others are trying to produce messages from their modules. If someone with more insight into the Messaging Framework wants to chime in here, that would be great.

 
// code that selects which user I want to send to goes here

$user = user_load(array('uid'=>$user->uid));

if (function_exists('messaging_message_send_user')) {    
		
$message = array();
$message['type'] = 'checkin_reminder';
$message['subject'] = t(token_replace(messaging_message_part('checkin_reminder', 'subject', $user->messaging_default), 'reminder_urls', $user));

$message['body']['header'] = t(token_replace(messaging_message_part('checkin_reminder', 'header', $user->messaging_default), 'reminder_urls', $user));

$message['body']['content'] = t(token_replace(messaging_message_part('checkin_reminder', 'main', $user->messaging_default), 'reminder_urls', $user));

$message['body']['footer'] = t(token_replace(messaging_message_part('checkin_reminder', 'footer', $user->messaging_default), 'reminder_urls', $user));

messaging_message_send_user($user, $message);



// I'll post my hook_messaging and hook_token stuff as well here

function checkin_reminder_messaging($op, $arg1 = NULL, $arg2 = NULL, $arg3 = NULL, $arg4 = NULL) {
  switch ($op) {
    case 'message groups':
      $info['checkin_reminder'] = array(
        'module' => 'checkin_reminder',
        'name' => t('Check-In Reminder'),
      );
      return $info;
    case 'message keys':      
      $type = $arg1;
      switch ($type) {
        case 'checkin_reminder':
          return array(
            'subject' => t('Subject for check-in reminder'),
            'header' => t('Header for check-in reminder'),
            'main' => t('Content for check-in reminder'),
            'footer' => t('Footer for check-in reminder'),
          );
      }
      break;
    case 'messages':
      $type = $arg1;
      if ($type == 'checkin_reminder') {
        return array(
          'subject' => t('Check-In reminder for [user] from [site-name]'),
          'header' => t("Greetings [user],"),
          'main' => t("The check-in period for the week is now open. Please login."),
          'footer' => array(
              t('This is a reminder from [site-name]'),
              t('To manage your notifications, browse to [subscriptions-manage]'),
              t('You can unsubscribe at [unsubscribe-url]'),
          ),
        );
      }
      break;
    case 'tokens':
      $type = $arg1;
      $tokens = array();
      // These are the token groups that will be used for this module's messages
        $tokens = array('global', 'reminder_urls');
      return $tokens; 
  }
}


/**
 * Implementation of hook_token_values()
 *
 * This sets up the token values to be replaced
 * 
 */
function checkin_reminder_token_values($type, $object = NULL, $options = array()) {
  switch ($type) {
    case 'reminder_urls':
      if ($account = $object) {
		  $values = array();
			$values['login-url'] = url('user/login', NULL, NULL, TRUE); 
			$values['pw-reset-url'] = url('user/password', NULL, NULL, TRUE); 
			$values['receiver-username'] = $account->name;   
		  return $values;
	  }
  }
}



/**
 * Implementation of hook_token_list().
 * This lists the available tokens on the admin template settings page.
 */
function checkin_reminder_token_list($type = 'all') {
  $tokens = array();
  if ($type == 'reminder_urls' || $type == 'all') {
    $tokens['reminder_urls']['login-url']    = t('The url for participants to login.');
	$tokens['reminder_urls']['pw-reset-url']    = t('The url for participants to reset their password.');
	$tokens['reminder_urls']['receiver-username']    = t('The username of the receiver of the message.');
  }
  return $tokens;

jose reyero’s picture

Your code looks good, I guess it may work, just some suggestions:

// Composing the message
$sending_method = messaging_method_default($user);
$message['subject'] = messaging_message_part('checkin_reminder', 'subject', $sending_method );
$message['body'] = messaging_message_part('checkin_reminder', 'main', $sending_method);

// Token replacement
$objects['reminder_urls'] = $user; 
$objects['global'] = NULL; // This makes global tokens available too.
$message = token_replace_multiple($text, $objects);

You also could use 'user' tokens, or provide your own inside the 'user' group (they will be added to the ones produced by token module, so you can use them all in your templates)

Wrapping the strings in t() after toking replacement may not be a good idea, the templating system currently doesn't support multilingual templates though, but also using templates is not mandatory for messaging, you can just use your own t() strings there instead of using messaging_message_part().

Hope this helps.

jose reyero’s picture

Status: Active » Closed (fixed)
liquidcms’s picture

have been looking all over for this.. hope this is pretty close for D6..