MyBB Community Forums

Full Version: automatic posting of a message to a topic
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have done this before with a different bulletin board , and now to do something similar with Mybb.
I am working as the webmaster for a flight simulator virtual airline and I am just setting up MYBB for us and I want to be able to do the following as an example.
 A pilot attains a certain number of flight hours and is awarded a new pilot rank,  This is done in our main software and thus I want to extend our software to then communicate automatically with MYBB via API to create a posting in a forum topic to acknowledge the pilot promotion.

What I need to know is 

(a) is there a MYBB API available for posting messages automatically from other software using PHP
{b} if there is a plug-in for latest MYBB to do this as we just installed the forum
{c} if it is built into MYBB are there any tutorials and examples 

Any help on the above ASAP will be gratefully appreciated
I have worked this out myself and to help others here is my example - :
<?php
// 
// Code required to make a posting to MyBB Forum
// Developed by Leslie Jarrett
// July 2021
//
// This software uses a Function to make the posting and uses
// three parameters which you need to set up before calling the
// function
// 
    // The first thing we need to do is to initiate a global variable as this is checked
// against other Mybb operations as you cannot run these on their own as a security device.
//
	define("IN_MYBB", 1);
//
// In order to get access and to use parts of Mybb we need to include the module that sets up
// and makes available any global varuiables that other modules use
// You will need to change the path to this module as required by your folder structure 
//
	require_once "../forum/global.php";
// *****************************************************************************
// Function - PostMybb
// This is a function to make a posting to a myBB forum
// The function consists of three paramaters as follows -:
// 		$PostSubject 	- the title of the posting i.e WELCOME TO THE FORUM
//      $PostText    	- the text of the posting i.e. Thank you for joining our forum
//      $PostParam   	- the information in an array relating to where you are posting
//
//      $PostParam   	- this array of posting information is set up as follows -:
//			$PostFid 	- the forum id number need to get from table mybb_forums field fid
//          $PostUid    - the forum user id number need to get from table mybb_users field uid
// 			$PostIcon	- the icon used for posting need to get from table mybb_icons field iid
//          $PostName   - the name of the person posting the message i.e. John
//          $PostReply  - the id of the reply to  need to get from table mybb_posts field replyto
// ******************************************************************************
//
	function PostMybb ($PostSubject, $PostText, $PostParam) {
// 
// Include the Mybb module to handle the actual posting of the message
// If the global.php was loaded correctly then you should not need to change the path
// to this module
//
	require_once MYBB_ROOT."inc/datahandlers/post.php";
//
//  set up a new instance of creating a message 
//
	$NewMessage = new PostDataHandler("insert");
//
//  set up the action type for the message 
//
	$NewMessage ->action = "thread";
//
//  Now we take the parameters and build an array 
//  for posting the message
//
	$new_posting 					= array(
			"fid" 					=> $PostParam['PostFid'],
			"subject" 				=> $PostSubject,
			"icon" 					=> $PostParam['PostIcon'],
			"uid" 					=> $PostParam['PostUid'],
			"replyto"   			=> $PostParam['PostReply'],
			"username"  			=> $PostParam['PostName'],
			"message" 				=> $PostText,
			"ipaddress" 			=> $session->packedip,
	);
//
// set up the options for the message 
//
	$new_posting['options'] = array(
			"signature" 			=> 1,
			"subscriptionmethod" 	=> "email",
			"disablesmilies" 		=> 1
	);
//
// set up moderation options as well
//
	$new_posting['modoptions'] = array(
			"stickthread" 			=> 1
	);
//
// all paramters and options are now set up 
// so we can process the posting of the message
//
// Set the data for the message into the posting instance
//
	$NewMessage->set_data($new_posting);
//
// Firstly we need validate the message we are trying to write
//
	$ValidMessage = $NewMessage->validate_thread();
//
//  Obviously if we have an errors we need to find out
//  what is wrong with the message
//
	$ErrorMessage = array();
//
	if(!$ValidMessage)
			{
				$ErrorMessage = $NewMessage->get_friendly_errors();
			}
//
// Now we have found out if we have any errors to report 
//
	if(count($ErrorMessage) > 0)
			{
				$thread_errors = inline_error($ErrorMessage);
				$mybb->input['action'] = "newthread";
			}
//
// If we have set everything up correctly we should be at this point
// where we can actually post the message 
//
		else
			{
				$thread_info = $NewMessage->insert_thread();
				$tid = $thread_info['tid'];
				$visible = $thread_info['visible'];
			}
	}
//
// *************************************************************************************
// 
// Create the message to post to the mybb forum
// NOTE - : The data used below is obviously for a specific message and you will need to
// 			change the values to suit your own forum of course
//
// Once you have the various id's and references from tables we put them in an array
// 
//
	$MessageCodes = array();
//
//			$PostFid 	- the forum id number need to get from table mybb_forums field fid
// 
	$MessageCodes['PostFid'] = 4;
//
//          $PostUid    - the forum user id number need to get from table mybb_users field uid
//
	$MessageCodes['PostUid'] = 1;
//
// 			$PostIcon	- the icon used for posting need to get from table mybb_icons field iid
//
	$MessageCodes['PostIcon'] = 22;
//  
//          $PostName   - the name of the person posting the message i.e. John
//
	$MessageCodes['PostName'] = 'Leslie';
//
//          $PostReply  - the id of the reply to  need to get from table mybb_posts field replyto
//
	$MessageCodes['PostReply'] = 0;
//
// 		$PostSubject 	- the title of the posting i.e WELCOME TO THE FORUM 
//
	$MessageSubject = "New Pilot EIN782";
//
//      $PostText    	- the text of the posting i.e. Thank you for joining our forum
//	
	$MessageText = 'Please give a warm welcome to our newest member Mike Adhern EIN782, welcome in and enjoy your stay';
//
// we have set up the parameters for the message we want to post 
// so now we can post the message 
//
	$MessagePosted = PostMybb ($MessageSubject, $MessageText, $MessageCodes);
//
?>