Creating Conversations via Cron Entries

whatupdoc

Member
I've been stuck on this for the past day and I can't figure this out. I'm trying to setup a cron entry to send a conversation message but it's not working. When I checked the server error logs, I get this error:

XenForo_Exception: You may not start a conversation with the following recipients: testuser. - library/XenForo/DataWriter.php:1345
Generated By: Unknown Account, 3 minutes ago

The thing is, when I manually run this cron entry, it works perfectly. I'm assuming the Cron Entry function uses whatever account that runs the cron entry? I'm not sure. My code to the cron entry is shown below. Any help would be appreciate, thanks.


Code:
        $writer = XenForo_DataWriter::create('XenForo_DataWriter_ConversationMaster');
        $writer->set('user_id', '10');
        $writer->set('username', 'randomusername');
        $writer->set('title', 'Test');
        $writer->set('open_invite', '0');
        $writer->set('conversation_open', '1');
        $writer->addRecipientUserNames(array('testuser'));

        $messageDw = $writerConv->getFirstMessageDw();
        $messageDw->set('message', 'first message');

        $writer->preSave();

         if ($writerConv->hasErrors()) { return array('error' => TRUE, 'errors' => $writerConv->getErrors()); }
              
        $writer->save();
 
I have not tested this and I only have an older version of XF at the moment but I'm pretty sure you need to pass in a user array for the creator of the conversation.
You can fetch your user like so:
PHP:
$creator = XenForo_Model::create('XenForo_Model_User')->getUserById($userId, array(
   'join' => XenForo_Model_User::FETCH_USER_FULL | XenForo_Model_User::FETCH_USER_PERMISSIONS
));

$creator['permissions'] = XenForo_Permission::unserializePermissions($creator['global_permission_cache']);

Then provide it to the datawriter:
PHP:
$conversationDw->setExtraData(XenForo_DataWriter_ConversationMaster::DATA_ACTION_USER, $creator->toArray());
$conversationDw->setExtraData(XenForo_DataWriter_ConversationMaster::DATA_MESSAGE, $message);

Also to the message datawriter:
PHP:
$messageDw->setExtraData(XenForo_DataWriter_ConversationMessage::DATA_MESSAGE_SENDER, $creator);

Skip IP:
PHP:
$messageDw->setOption(XenForo_DataWriter_ConversationMessage::OPTION_SET_IP_ADDRESS, false);

See ControllwrPublic/Conversation.php - function actionInsert()

I hope this helps.
 
I have not tested this and I only have an older version of XF at the moment but I'm pretty sure you need to pass in a user array for the creator of the conversation.
You can fetch your user like so:
PHP:
$creator = XenForo_Model::create('XenForo_Model_User')->getUserById($userId, array(
   'join' => XenForo_Model_User::FETCH_USER_FULL | XenForo_Model_User::FETCH_USER_PERMISSIONS
));

$creator['permissions'] = XenForo_Permission::unserializePermissions($creator['global_permission_cache']);

Then provide it to the datawriter:
PHP:
$conversationDw->setExtraData(XenForo_DataWriter_ConversationMaster::DATA_ACTION_USER, $creator->toArray());
$conversationDw->setExtraData(XenForo_DataWriter_ConversationMaster::DATA_MESSAGE, $message);

Also to the message datawriter:
PHP:
$messageDw->setExtraData(XenForo_DataWriter_ConversationMessage::DATA_MESSAGE_SENDER, $creator);

Skip IP:
PHP:
$messageDw->setOption(XenForo_DataWriter_ConversationMessage::OPTION_SET_IP_ADDRESS, false);

See ControllwrPublic/Conversation.php - function actionInsert()

I hope this helps.

Thanks for taking the time to help! It definitely helped a lot and the problem has been resolved.
 
Top Bottom