In this post we will create in an easy way a new Slack Bot to obtain results of the rankings from Crypto-coin markets. In this case from https://coinmarketrank.io/

Just go to https://api.slack.com/apps?new_app=1

Set an App name and a Workplace to add the robot development like in the picture

Captura de pantalla 2020-06-12 a las 21.30.02

Click on “Slack Commands” (we will create a new one to obtain the results from CoinMarketRank)

Captura de pantalla 2020-06-12 a las 21.32.07

And let’s create a new one

Captura de pantalla 2020-06-12 a las 21.32.17

Let’s create the command /coinmarketrank

Captura de pantalla 2020-06-12 a las 21.34.41

In the Request URL param we must set the link to the URL that will process the robot command /coinmarketrank to do the query to CoinMarketRank API and process the results to show finally in the Slack chat channel.

For this exercise you can use this Endpoint URL to start: https://coinmarketrank.io/getReport.php

But to complete all the exercise you must copy two files in some HTTPS server that accepts PHP files. You can copy and download the files from Github:

Here the source of the file getReport.php


$url = "https://jira.coinmarketrank.io/plugins/servlet/eazybi/accounts/3/export/report/92-slackbot.csv?embed_token=sxcelctjzjza0d0euj4rcdvh7kabf4hhhokv8hkjxtoghe3w67mqngqei18e";
$result = str_replace(","," | ",file_get_contents($url));
$result = str_replace("\n"," |\n| ",$result);
$result = "*CoinMarketRank* results for *TOP20:*\n".$result." More info in https://coinmarketrank.io \n";
echo $result;

Now we can install the Bot! Go to Install App to Workplace option

Captura de pantalla 2020-06-12 a las 21.45.33

Accept and install to your Slack Workplace

Captura de pantalla 2020-06-12 a las 21.46.28

That’s all! Now go to your channel and test it, just launch the command /coinmarketrank in some channel. You will see the latest prices of the Top20 Cryptocoins like Bitcoin, Ethereum, etc.

Captura de pantalla 2020-06-12 a las 21.48.05

 

If you want to distribute your App/Bot, is super easy, just observe the second PHP file “auth.php” . In order to provide your App of the famous dance of the OAuth, just place a file like “auth.php” in some webserver HTTPS that accepts PHP.

In the OAuth and Permissions section we must add the link to the URL of the Auth file. In this case we use https://coinmarketrank.io/auth.php but you must use your link.

Captura de pantalla 2020-06-12 a las 21.52.28

In the file remember to add the OAuth_client and OAuth_secret parameters!


// Define Slack application identifiers
// Even better is to put these in environment variables so you don't risk exposing
// them to the outer world (e.g. by committing to version control)
define( 'SLACK_CLIENT_ID', 'xx' );
define( 'SLACK_CLIENT_SECRET', 'xx' );
//$api_root = 'https://slack.com/api/oauth.access';
//$api_root = "https://slack.com/oauth/v2/authorize";
$api_root = "https://slack.com/api/oauth.v2.access";

function get_client_id() {
// First, check if client ID is defined in a constant
if ( defined( 'SLACK_CLIENT_ID' ) ) {
return SLACK_CLIENT_ID;
}

// If no constant found, look for environment variable
if ( getenv( 'SLACK_CLIENT_ID' ) ) {
return getenv( 'SLACK_CLIENT_ID' );
}

// Not configured, return empty string
return '';
}

/**
* Returns the Slack client secret.
*
* @return string The client secret or empty string if not configured
*/
function get_client_secret() {
// First, check if client secret is defined in a constant
if ( defined( 'SLACK_CLIENT_SECRET' ) ) {
return SLACK_CLIENT_SECRET;
}

// If no constant found, look for environment variable
if ( getenv( 'SLACK_CLIENT_SECRET' ) ) {
return getenv( 'SLACK_CLIENT_SECRET' );
}

// Not configured, return empty string
return '';
}

$code = $_GET['code'];

function httpPost($url, $source)
{
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept: application/json'));
//curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
'code' => $source,
'client_id' => get_client_id(),
'client_secret' => get_client_secret(),
'redirect_uri' => "https://coinmarketrank.io/auth.php"
));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}

$response = json_decode(httpPost( $api_root ,$code ));
if ($response->ok) header('Location: https://app.slack.com/client/');
else echo $response." END";

And now yes… That’s all!!

By MrAddon

 

.

Posted by:.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s