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
Click on “Slack Commands” (we will create a new one to obtain the results from CoinMarketRank)
And let’s create a new one
Let’s create the command /coinmarketrank
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
Accept and install to your Slack Workplace
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.
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.
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
.
One thought on “How to create a Slack Bot in 5 minutes”