Sending an alert

AndyB

Well-known member
Hello,

I have an add-on called Birthday Thread where I create a birthday thread via Cron. I was asked if I can send an alert when the thread is created. I'm real close with this code, but something is missing:

PHP:
		foreach ($userIds as $user)
		{
			// add username to subject
			$subjectNew = str_replace('{username}', $user['username'], $subject);
			
			// add username to message
			$messageNew = str_replace('{username}', $user['username'], $message);			
			
			$writer = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
			$writer->set('user_id', $starterId);
			$writer->set('username', $starterName);
			$writer->set('title', $subjectNew);
			$postWriter = $writer->getFirstMessageDw();
			$postWriter->set('message', $messageNew);
			$postWriter->setOption(XenForo_DataWriter_DiscussionMessage::OPTION_SET_IP_ADDRESS, false);
			
			$writer->set('node_id', $forumId);
			$writer->preSave();
			$writer->save();
			$thread = $writer->getMergedData();	

			// define variable
			$contentType = 'thread';
			
			// define variable
			$action = '';

			// define variable
			$extraData = array(
				'title' => $thread['title'],
				'link' => XenForo_Link::buildPublicLink('threads', $thread),
				'reason' => ''
			);
			
			// send alert
			XenForo_Model_Alert::alert($user['user_id'], $starterId, $starterName, $contentType, $thread['thread_id'], $action, $extraData); 
		}

The alert I get looks like this:

pic001.webp

Thank you.
 
Are you added the template for alert? As I see, you should have an template: alert_thread. I recommend dont leave action field to empty.
 
You need to populate a template.

The template name that alerts use follows the form:
Code:
'alert_'.$contentType.'_'.$action
 
Hi Nobita.Kun and Xon,

Thank you both for taking a look at this.

I think the template that I want to use is called "alert_post_insert". So I tried defining the variable $action as that, but that didn't work.

PHP:
			// define variable
			$action = 'alert_post_insert';
 
Hi Nobita.Kun and Xon,

Thank you both for taking a look at this.

I think the template that I want to use is called "alert_post_insert". So I tried defining the variable $action as that, but that didn't work.

PHP:
            // define variable
            $action = 'alert_post_insert';
The action should be: 'insert' and $contentType should be 'post'.
 
  • Like
Reactions: Xon
@AndyB To make the alert system use that template, call XenForo_Model_Alert::alert with:
Code:
$contentType = 'post';
$action= 'insert';
 
Thank you both!

This works perfect. :)

PHP:
		foreach ($userIds as $user)
		{
			// add username to subject
			$subjectNew = str_replace('{username}', $user['username'], $subject);
			
			// add username to message
			$messageNew = str_replace('{username}', $user['username'], $message);			
			
			$writer = XenForo_DataWriter::create('XenForo_DataWriter_Discussion_Thread');
			$writer->set('user_id', $starterId);
			$writer->set('username', $starterName);
			$writer->set('title', $subjectNew);
			$postWriter = $writer->getFirstMessageDw();
			$postWriter->set('message', $messageNew);
			$postWriter->setOption(XenForo_DataWriter_DiscussionMessage::OPTION_SET_IP_ADDRESS, false);
			
			$writer->set('node_id', $forumId);
			$writer->preSave();
			$writer->save();
			$thread = $writer->getMergedData();	

			// define contentType
			$contentType = 'post';
			
			// define action
			$action = 'insert';

			// define extraData
			$extraData = array();
			
			// send alert
			XenForo_Model_Alert::alert($user['user_id'], $starterId, $starterName, $contentType, $thread['first_post_id'], $action, $extraData); 
		}
 
Top Bottom