Creating a slack bot using PHP to monitor your envato sales

Slack is awesome. Slack is the de-facto of modern team communication and I am pretty sure that this is not the first time you’re hearing something like this about slack. But if you’re really never used slack, oh boy, I feel pity for you 🙂

Anyway, in this article we’re going to make a slack bot that will monitor the sales in our envato account and notify from time to time as soon as someone purchased an item. Pretty useful, eh?


Considering you have a slack account, log into it and open a new channel, for example “#sales”. Now go to or just click on “Configure Integrations” from your slack app.

At the bottom of your integration page like this screenshot below, select Incoming Webhooks

Now, select your channel, click on “Add” and then give this service a name, like “Envato Bot”

It’s important that you copy this WebHook URL from this screen. We will need it later in our code.

Generating Envato Token

To use Envato API without much pain, we need to create a token that we will use to make a call to their API endpoint. Let’s go to and sign in with your account.

Now, to create a token, go to and fill up the form. Give it a name, like “Envato Slack Bot”. From the checkboxes in this form, make sure that you checked only the following two

  1. View and search Envato sites
  2. View your items sales history

Like this screenshot below

You will also need to check the “Agree with Terms” checkbox.

Now move ahead and click on “Generate Token” button on that screen and make sure to copy this token. Save it in some place for later use.

Now we have all the necessary information to build our slack bot.

Coding the bot

Now it’s time to code the bot which will check the sales from time to time, and compare the new sales which were purchased since the last time it checked. If new sales are found, it will make a HTTP POST call to our slack channel’s webhook url

Add a file named update.txt in your current directory and make sure that it has write permission. Now, let’s create a file named bot.php and add the following codes in it

[code lang=php]
#!/usr/bin/env php
$last = "";
define( "ENVATO_TOKEN", "ZTUyBGh*******uqtQ8uwL****6TR" );
define( "SLACK_WEBHOOK", "" );

date_default_timezone_set("Asia/Dhaka"); //important

$lastUpdate = trim( file_get_contents( "update.txt" ) );
$headers = array( 'Authorization: Bearer ' . ENVATO_TOKEN );
$ch = curl_init( "" );
curl_setopt( $ch, CURLOPT_HTTPHEADER, $headers );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );

$data = curl_exec( $ch );
curl_close( $ch );

if ( $data ) {
$jsonData = json_decode( $data, true );
foreach ( $jsonData as $item ) {
$soldAt = $item['sold_at'];
$itemName = $item['item']['name'];

if ( ! $last ) {
$last = $soldAt;

if ( $soldAt != $lastUpdate ) {
$localTime = date("d M, Y H:i:s A",strtotime($soldAt));
$messageText = "One {$itemName} was sold at {$localTime}";

$message = array(
"payload" =>
"text" => $messageText,

$ch = curl_init( SLACK_WEBHOOK );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $message );
curl_exec( $ch );
curl_close( $ch );


} else {
break; //no new sales since last time

if($last) file_put_contents( "update.txt", $last );

If your update.txt has proper write permission then on first script execution you will see a slack notification in your slack app instantly with the last sold item.

Did you notice that we’re using local time correctly in this script using this line

[code lang=php]
date_default_timezone_set("Asia/Dhaka"); //important

You can change it to your timezone to make sure that all notifications from this slack bot matches your local time.

The notification will look like this

Run it regularly using a cron job

Well, now that our bot is ready, we need to make sure that it runs in a regular interval, for example after every 10 minutes.

First, give it proper permission to execute as a shell script

[code lang=bash]
chmod +x /path/to/your/bot.php

Now open the cron job editor using the following command

[code lang=bash]
sudo crontab -e

And add the following line at the end

[code lang=bash]
*/10 * * * * /path/to/your/bot.php > /dev/null 2>&1

And you’re done. Now you will get sales notification in your slack channel regularly without checking it on the envato sites.

Hope you’ve enjoyed this article. I’d love to hear your comments. Enjoy!


Leave a comment

Your email address will not be published. Required fields are marked *