Back to vBulletin 3 Articles

[How-To] Use API for Private Messages
by harmor19 03 Jul 2006
Rating: (1 vote - 5.00 average)

Have you ever wanted to create a hack that'll private message users when they reach a certain point in the script?
For instance say you wanted to private message user "abc" when he or she reaches the end of the form.

The first thing you'll need to do is require "includes/class_dm.php" along with "global.php".

I have put the Private Message API into a function. I decided to do this in my own script due to the repetiveness.

PHP Code:
function pm_api($fromuserid$fromusername$title$message$recipients)
{
    global 
$vbulletin$botpermissions;
    
    
$pmdm =& datamanager_init('PM'$vbulletinERRTYPE_ARRAY);
        
$pmdm->set('fromuserid'$fromuserid);
        
$pmdm->set('fromusername'$fromusername);
        
$pmdm->set('title'$title);
        
$pmdm->set('message'$message);
        
$pmdm->set_recipients($recipients$botpermissions);
        
$pmdm->set('dateline'TIMENOW); 
        
$pmdm->save();
        unset(
$pmdm);
        
        return 
$pmdm;
    

Now that you have the function you'll need to create a method to use with it.
Let's say you owned a auction type forum and wanted to private message an user after they've entered their product's information into the form.

PHP Code:
$fromuserid 1;
        
$fromusername "Auctioneer";
        
$title "Your Entry is Being Reviewed";
        
$message "Hello ".$vbulletin->userinfo['username'].",
                      We are now reviewing your entry."
;
         
$recipients $vbulletin->userinfo['username'];

pm_api($fromuserid$fromusername$title$message$recipients); 
To put it all together
PHP Code:
<?php

// ######################## SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// ##################### DEFINE IMPORTANT CONSTANTS #######################
define('THIS_SCRIPT''test'); 

// #################### PRE-CACHE TEMPLATES AND DATA ######################
// get special phrase groups
$phrasegroups = array();

// get special data templates from the datastore
$specialtemplates = array();

// pre-cache templates used by all actions
$globaltemplates = array();

// pre-cache templates used by specific actions
$actiontemplates = array();

// ########################## REQUIRE BACK-END ############################
require_once('./global.php');
require_once(
'./includes/class_dm.php');

// #################### HARD CODE JAVASCRIPT PATHS ########################
$headinclude str_replace('clientscript'$vbulletin->options['bburl'] . '/clientscript'$headinclude);

// ########################################################################
// ######################### START MAIN SCRIPT ############################
// ########################################################################

//create a function for the Private Message API.
function pm_api($fromuserid$fromusername$title$message$recipients)
{
    global 
$vbulletin$botpermissions;
    
    
$pmdm =& datamanager_init('PM'$vbulletinERRTYPE_ARRAY);
        
$pmdm->set('fromuserid'$fromuserid);
        
$pmdm->set('fromusername'$fromusername);
        
$pmdm->set('title'$title);
        
$pmdm->set('message'$message);
        
$pmdm->set_recipients($recipients$botpermissions);
        
$pmdm->set('dateline'TIMENOW); 
        
$pmdm->save();
        unset(
$pmdm);
        
        return 
$pmdm;
    
}

$fromuserid 1;
        
$fromusername "Auctioneer";
        
$title "Your Entry is Being Reviewed";
        
$message "Hello ".$vbulletin->userinfo['username'].",
                      We are now reviewing your entry."
;
         
$recipients $vbulletin->userinfo['username'];

pm_api($fromuserid$fromusername$title$message$recipients);

?>

vblts.ru supports vBulletin®, 2022-2024