Wordpress / PHP Generated Pages

output
lists
quotes
points
commands

#1

Just wanted to share with everyone the page I made for our quotes (more coming soon).
I’m thinking of adding sorting to the table. Also, if anyone has any feedback let me know. I coded this at 2 AM and I hate PHP lol

UPDATE 7/30/17

I’ve moved these over to github here: https://github.com/jaredkotoff/phantombot-webpage-scripts
If you have any problems please report them as issues over there.

Everything below is now old


Here is the json file generator. I run this with a cron job twice a day.

<?php
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_HTTPHEADER, array(
		'webauth: [webauthtoken]'
	));

	curl_setopt($curl, CURLOPT_URL, 'https://[boturlhere]/dbquery?table=quotes&getKeys');
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	$results = json_decode(curl_exec($curl));

	// starts json object
	$quotes = '{';
	// results are a list of keys
	foreach($results->table->keylist as $keys) {
		$key = $keys->key;
		// get the object for each key
		curl_setopt($curl, CURLOPT_URL, 'https://[boturlhere]/dbquery?table=quotes&getData='.$key);
		// convert string to json
		$quoteObject = json_decode(curl_exec($curl));
	
		// append array element of the quote
		$quotes .= "\"{$key}\": {$quoteObject->table->value},";
	}
	// close curl resource
	curl_close($curl);
	// remove trailing comma
	$quotes = rtrim($quotes,',');
	// close object
	$quotes .= '}';

	// write to a file
	$fp = fopen('/home/user/public_html/phantombot/quotes.json', 'w');
	fwrite($fp, $quotes);
	fclose($fp);
?>

Here is the wordpress template page. I put this code just below: the_content();

	// get quotes for file
	$filename = "/home/user/public_html/phantombot/quotes.json";
	$fp = fopen($filename, "r");
	// read and convert to json
	$quotes = json_decode(fread($fp, filesize($filename)));
	fclose($fp);
?>
<table>
	<tr>
		<th style="padding-right:0;">ID</th>
		<th>Quote</th>
	</tr>
	<?php
		/*
			quote[0] = who said the quote
			quote[1] = quote string
			quote[2] = time in epoch in ms
			quote[3] = game title
		*/
		foreach($quotes as $key => $quote) {
			// quote time is in ms, conver to seconds
			$seconds = floor($quote[2] / 1000);
			// convert epoch to date time
			$dt = new DateTime("@$seconds", new DateTimeZone('UTC')); 
			$dt->setTimeZone(new DateTimeZone('PDT'));
			$date = $dt->format('h:i A Y-m-d e');
			echo "<tr>";
			// ugly, but im lazy
			echo "<td'>#{$key}:</td><td>{$quote[1]} -<em>{$quote[0]}</em><br />During: <u>{$quote[3]}</u> at {$date}</td>";
			echo "</tr>";
		}

echo "</table>";
// update when the json was written
echo "<i style='font-size:11px';>Last update: " . date("F d Y H:i:s e.", filemtime($filename)) . "</i>";

Bot Command List HTML
API Site creation
Presenting commands data from db on website
Point shop for users?
Writing to phantombot.db with 3rd Party Program
Points Leaderboard?
Data Rendering Service is LIVE
User Time In Chat
#2

Love to be able to do this type of thing with Top 100 Points as well


#3

For the bulk of the code it should be pretty similar. Im going to be making one to list all the custom commands we have but I can probably do Top 100 after that if you need.


#4

Here’s a printout of custom commands:

cron job

<?php
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_HTTPHEADER, array(
		'webauth: [botauth]'
	));
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	
	// Get Commands
	curl_setopt($curl, CURLOPT_URL, 'http://[boturlhere]/dbquery?table=command&getKeys');
	$results = json_decode(curl_exec($curl));
	$commands = [];
	
	foreach($results->table->keylist as $keys) {
		$key = $keys->key;
		curl_setopt($curl, CURLOPT_URL, "http://[boturlhere]dbquery?table=command&getData={$key}");
		$object = json_decode(curl_exec($curl));

		$commands["{$key}"] = "{$object->table->value}";
	}
	ksort($commands);
	$commands = json_encode($commands);
	
	// write to a file
	$fp = fopen('/home/user/public_html/phantombot/commands.json', 'w');
	fwrite($fp, $commands);
	fclose($fp);
	unset($commands);
	
	curl_close($curl);
?>

wordpress content

// get commands for file
$filename = "/home/user/public_html/phantombot/commands.json";
$fp = fopen($filename, "r");
// read and convert to json
$commands = json_decode(fread($fp, filesize($filename)));
fclose($fp);
?>
<table>
	<tr>
		<th>Command</th>
		<th>Output</th>
	</tr>
	<?php
		foreach($commands as $key => $command) {
			echo "<tr>";
			echo "<td><strong>!{$key}</strong>:</td><td>{$command}</td>";
			echo "</tr>";
		}

echo "</table>";
// update when the json was written
echo "<i style='font-size:11px';>Last update: " . date("F d Y H:i:s e.", filemtime($filename)) . "</i>";

#5

Quick Question: So the hosting needs to run from the same place as the bot? From the look at the commands in the wordpress section.


#6

Nope. In fact, my bot is ran on my personal server with the cron jobs on my webhosting. Your bot would need to be publically accessible, but that’s setup falls outside of this post.


#7

Top 100
https://www.updownleftdie.com/bot/top100/

cron job

<?php
	$curl = curl_init();
	curl_setopt($curl, CURLOPT_HTTPHEADER, array(
		'webauth: [authkey]'
	));
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	
	// Get Top 100
	curl_setopt($curl, CURLOPT_URL, 'https://[boturl]/dbquery?table=points&getKeys');
	$results = json_decode(curl_exec($curl));
	$points = [];
	
	foreach($results->table->keylist as $keys) {
		$key = $keys->key;
		curl_setopt($curl, CURLOPT_URL, "https://[boturl]/dbquery?table=points&getData={$key}");
		$object = json_decode(curl_exec($curl));

		$points["{$key}"] = "{$object->table->value}";
	}
	arsort($points);
	$points = json_encode($points);
	
	// write to a file
	$fp = fopen('/home/user/public_html/phantombot/points.json', 'w');
	fwrite($fp, $points);
	fclose($fp);
	unset($points);
	
	curl_close($curl);
?>

wordpress content

	// get points form file
	$filename = "/home/udld/public_html/phantombot/points.json";
	$fp = fopen($filename, "r");
	// read and convert to json
	$points = json_decode(fread($fp, filesize($filename)));
	fclose($fp);
?>
<table>
	<tr>
		<th>Rank</th>
		<th style="padding-right:0;">User</th>
		<th>SackJuice</th>
	</tr>
	<?php
		$rank = 1;
		foreach($points as $user => $userPoints) {
			if($rank > 100) {
				break;
			}
			echo "<tr>";
			echo "<td>#{$rank}</td><td align='left' style='padding-right:0;'>{$user}</td><td><strong>{$userPoints}</strong></td>";
			echo "</tr>";
			$rank++;
		}
echo "</table>";
// when when the json was written
echo "<i style='font-size:11px';>Last update: " . date("F d Y H:i:s e.", filemtime($filename)) . "</i>";

#8

Question what is the web auth? is that the auth for the phantombot that you can use their api to make an oauth?


#9

it’s the webauthro key in the botlogin.txt


#10

I’m trying to run a cron job for my bot off a web server, however, it doesn’t seem to be able to access the bot’s database, though I know the script is running - I read something above about setting the bot to be publicly accessible? It worked fine on my local web server when I was testing it, running the script manually, rather.


#11

This falls outside of the scope of this topic so i’ll respond to your PM.


#12

Can i message you UpDownLeftDie as i think I have everything setup correctly but still cant get it to work.


#13

Feel free too, but I’m more responsive on discord: https://discord.me/udld


#14

Hi, @UpDownLeftDie

I appreciate your work. I receive PHP Warning: Invalid argument supplied for foreach() on line 13 for cron job file.

Any ideas?

Thanks!


#15

Check your request via Postman or cURL. Make sure you have the right ip, ports are open (if not on LAN), you have the right webauth header key set, and the bot is responding.
If you get “not authorized” or something then the bot is working but you have the wrong auth. If it doesnt respond at all then your setup is not correct.


#16

I have yet to get a php script working for my bot. I have to be doing something wrong. I tried a few of them. Are we supposed to use the webauth or the webauthro key here:

Honestly I tried both with no luck, but I’d like to know which is correct. When I run any of the php files it just creates an empty json file. I tried to echo the results and etc, and it was all blank.

Any ideas?


#17

webauth.
webauthro doesnt seem to have permissions for these endpoints.

My suggestion is the same as above. You need to establish where the problem is. You do this reducing complexity and stepping your way through the problem. The first step is to verify that your bot is actually accessible and responding to API request. I recommend using Postman but you can also use cURL.

If that works and the script doesn’t then you can reduce the php scripts to the bare minimum, have it simply printout the first result just do you can inspect the object. You can then build back to the full script or replace the script and work backwards until you find where its breaking.


#18

Thanks for answering!

Issue fixed.

Looking forward to your further work :slight_smile:


#19

Awsome! Im glad to hear.
I’m nearly done moving these to github with a new tool that will dynamically generate the script file!
This should help reduce user error and allow others to contribute.
I’ll have a blog post write up about how to set it up on as well and hopefully cover some of the issues some of you have faced.

I hope to start contributing some custom modules to phantombot soon too.


#20

im assuming with this you could design a store for people to spend there points on ??