PDA

View Full Version : [How-To] Use API for Private Messages


harmor19
03 Jul 2006, 02:31
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.

function pm_api($fromuserid, $fromusername, $title, $message, $recipients)
{
global $vbulletin, $botpermissions;

$pmdm =& datamanager_init('PM', $vbulletin, ERRTYPE_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.


$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

// ######################## 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', $vbulletin, ERRTYPE_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);

?>

akanevsky
03 Jul 2006, 03:12
I believe this article was already posted.

harmor19
03 Jul 2006, 04:30
I searched for "private message api" and only my thread came up.

Code Monkey
03 Jul 2006, 05:41
http://www.vbulletin.org/forum/showthread.php?t=82786

From over a year ago. It is listed in the summary of How To's that is stickied in this forum.

harmor19
03 Jul 2006, 09:32
Well that sucks, well I explain another approach to do it.

pyro.699
03 Jul 2006, 14:42
No, thankyou!
I dont know how, but you solved my Pm Counters not updating problem! When no-one else wanted to answer it!

Thanks a ton!
~Cody Woolaver

harmor19
03 Jul 2006, 14:46
No, thankyou!
I dont know how, but you solved my Pm Counters not updating problem! When no-one else wanted to answer it!

Thanks a ton!
~Cody Woolaver
You're Welcome.

Honestly I don't know where it would update the counters. Perhaps the 'save()' function?

I'm glad you found my tutorial useful.

pyro.699
03 Jul 2006, 14:53
nope, i had a save ^^; its actchually still puzzling, because all you had wa puting it in a function, and adding return and unset.... which shouldent have made any differance! but, whats with the global at the top? i never had that...
(my guess is that its because it is in a function)

Code Monkey
03 Jul 2006, 15:34
No need to return a value.


unset($pmdm);

return $pmdm;

harmor19
03 Jul 2006, 15:59
nope, i had a save ^^; its actchually still puzzling, because all you had wa puting it in a function, and adding return and unset.... which shouldent have made any differance! but, whats with the global at the top? i never had that...
(my guess is that its because it is in a function)
The reason I globalize "$vbulletin" and "$botpermissions" is to carry the value of those two variables into the function (I think that's how to explain it).
If I haven't have globalized those two the API would be rendered useless.



No need to return a value.


unset($pmdm);

return $pmdm;


I can remove those two tidbits?
It's been a while but I think I tried to use the function without returning the value and it didn't work. I could be wrong though.

Adrian Schneider
03 Jul 2006, 17:07
Yeah you can remove them ... in this case it doesn't matter, because you are deleting the variable, then returning it (null)... no point.

harmor19
03 Jul 2006, 17:15
Oh Ok.
And to think I'm a coder. :shrugs:

brandondrury
30 Apr 2007, 20:38
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.
Welcome, brandondrury.
You last visited: Today at 07:14 PM
Private Messages: Unread 0, Total 0.

Thanks,
Brandon Drury

brandondrury
03 Jun 2007, 16:13
$db->query_write("UPDATE user SET pmunread=pmunread+1,pmtotal=pmtotal+1 WHERE userid=$userid");

This took care of it.

Brandon

liquidx
03 Feb 2008, 14:31
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.

Napalmus
25 Mar 2013, 10:39
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 ?

MagicPID
27 Dec 2013, 20:32
@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)?