Go Back   vb.org Archive > vBulletin Article Depository > Read An Article > vBulletin 3 Articles
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools
[How-To] Use API for Private Messages
harmor19
Join Date: Apr 2005
Posts: 1,324

 

Show Printable Version Email this Page Subscription
harmor19 harmor19 is offline 07-02-2006, 10:00 PM

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);

?>
Reply With Quote
  #12  
Old 07-03-2006, 03:07 PM
Adrian Schneider's Avatar
Adrian Schneider Adrian Schneider is offline
 
Join Date: Jul 2004
Posts: 2,528
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Yeah you can remove them ... in this case it doesn't matter, because you are deleting the variable, then returning it (null)... no point.
Reply With Quote
  #13  
Old 07-03-2006, 03:15 PM
harmor19 harmor19 is offline
 
Join Date: Apr 2005
Posts: 1,324
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Oh Ok.
And to think I'm a coder. :shrugs:
Reply With Quote
  #14  
Old 04-30-2007, 06:38 PM
brandondrury brandondrury is offline
 
Join Date: Oct 2005
Posts: 136
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I see this article is nearly a year old, but I figured I'd ask anyway.

I'm using vBulletin 3.6.5.

Any idea why this isn't updating the user that they have a private message?

For example, this is what I see at the top right of my forum even though I have about 10 PMs sent using this script.
Quote:
Welcome, brandondrury.
You last visited: Today at 07:14 PM
Private Messages: Unread 0, Total 0.
Thanks,
Brandon Drury
Reply With Quote
  #15  
Old 06-03-2007, 02:13 PM
brandondrury brandondrury is offline
 
Join Date: Oct 2005
Posts: 136
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

PHP Code:
$db->query_write("UPDATE user SET pmunread=pmunread+1,pmtotal=pmtotal+1 WHERE userid=$userid"); 
This took care of it.

Brandon
Reply With Quote
  #16  
Old 02-03-2008, 12:31 PM
liquidx liquidx is offline
 
Join Date: Nov 2002
Posts: 20
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I know this is an old thread but does anyone have a proper solution for the problem brandon described in #13?

His fix doesn't work properly.
It does make the PM appear in the upper-right as unread and total.. but it does not allow for the new PM popup to appear.
Reply With Quote
  #17  
Old 03-25-2013, 09:39 AM
Napalmus Napalmus is offline
 
Join Date: Jan 2013
Posts: 3
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Hey guys, thanks for this topic, it helps to send PMs on vBulletin 4.x version, but now I would like to read PMs of specific user using his ID or anything, do someone has an idea how to display them ?
Reply With Quote
  #18  
Old 12-27-2013, 07:32 PM
MagicPID's Avatar
MagicPID MagicPID is offline
 
Join Date: Jul 2011
Posts: 117
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

@harmor19 - I am bumping this to recommend that this be moved to vBulletin 4 Articles as it is working on vBulletin 4.2.1.
A query though: How do you get a message to span multiple lines (like <br /> in HTML)?
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:40 AM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.06109 seconds
  • Memory Usage 2,299KB
  • Queries Executed 24 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (4)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)modsystem_article
  • (1)navbar
  • (4)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (8)post_thanks_box
  • (8)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (8)post_thanks_postbit_info
  • (7)postbit
  • (8)postbit_onlinestatus
  • (8)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete