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?

Bootstrapping

Considering you have a slack account, log into it and open a new channel, for example “#sales”. Now go to http://your_slack_url.slack.com/services/new 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 https://build.envato.com/ and sign in with your account.

Now, to create a token, go to https://build.envato.com/create-token/ 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
<?php
$last = "";
define( "ENVATO_TOKEN", "ZTUyBGh*******uqtQ8uwL****6TR" );
define( "SLACK_WEBHOOK", "https://hooks.slack.com/services/T0BR4EA9G/CD0BREL0SW/w5sbJwuXogF1cFAevwZNaCIZ" );

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

$lastUpdate = trim( file_get_contents( "update.txt" ) );
$headers = array( 'Authorization: Bearer ' . ENVATO_TOKEN );
$ch = curl_init( "https://api.envato.com/v2/market/author/sales" );
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" =>
json_encode(array(
"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 );

if(!$lastUpdate){
break;
}

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

if($last) file_put_contents( "update.txt", $last );
[/code]

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
[/code]

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
[/code]

Now open the cron job editor using the following command

[code lang=bash]
sudo crontab -e
[/code]

And add the following line at the end

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

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!