How to build a preferential steem upvote bot using php

in utopian-io •  7 years ago  (edited)

What Will I Learn?

  • You will learn how to build an upvote bot (with preference) using steem sc2-sdk-php

Requirements

  • You will need to have PHP powered server with openSSL
  • You will need to Install https://github.com/hernandev/sc2-sdk-php/
    You can do that via composer using these commands composer require hernandev/sc2-sdk-php
  • You will need to have a steemconnect registered app

Difficulty

  • Intermediate

Tutorial Contents

Make sure you have followed all necessary requirements above before reading these tutorial. There is a big possibility that this tutorial might not work at all on your localhost even if you use a SELF-SIGNED SSL certificates as only self-signed certificates can work on localhosts. There is a class called "GUZZLE" that sc2-sdk-php works with. This class only works with verified certificates except when otherwise manually edited.

So before I start this tutorial, I will offer a little fix on that, so you can easily work from your localhost, this fix is important,as there does not seem to be a straight forward way of working with sc2-sdk-php unless this fix is done.

THE FIX

Provided you have installed sc2-sdk-php via composer , follow the procedures below:

  • ENTER this address in the file explorer C:\wamp\www\bb\vendor\guzzlehttp\guzzle\src\Handler
    Edit the above address according to the location of your PUBLICHTML and location of PHP SERVER.
    You should see a list of different folders
  • Edit CurlFactory.php
  • Go to line 332 & 333 and edit the lines as stated below
 $conf[CURLOPT_SSL_VERIFYHOST] = 0; //previously 2
 $conf[CURLOPT_SSL_VERIFYPEER] = false; // previously true

Once this is done, the authentication will no longer be verified. Now, we can start the tutorial.

THE TUTORIAL

  • Go to the root of the folder where sc2-sdk-php was installed (where the vendor folder is located)
  • Create a new file there and name it index.php. Our upvote bot is going to be located here.

STEP 1 - Open the PHP TAG and add the following codes as stated below

 <?php
 ini_set('max_execution_time', 6000);
include('vendor/autoload.php');
use SteemConnect\Config\Config;
use SteemConnect\Client\Client;
use SteemConnect\Operations\Comment;
use SteemConnect\Operations\CommentOptions;
use SteemConnect\Operations\Vote;
// oauth client id.
$clientId = 'your-app-name';
// oauth client secret.
$clientSecret = 'app-secret-key';
// return url.
$returnUrl = 'https://localhost/vote';
// list of required scopes.
$scopes = [
    'login', 'offline', 'vote', 'comment', 'comment_delete',
    'comment_options', 'custom_json', 'claim_reward_balance',
];
// starts the configuration object, passing the client id and secret.
$config = new Config($clientId, $clientSecret);
// configure the return / callback URL.
$config->setReturnUrl($returnUrl);
// set the reqired scopes.
$config->setScopes($scopes);
// set the return/callback URL, so SteemConnect will redirect
// users back to your application.
$config->setReturnUrl('https://localhost/bb');
$sdk = new Client($config);
// get the URL to send the users that will authorize your app.
$redirectUrl = $sdk->auth()->getAuthorizationUrl();
if (empty($_GET['code'])){
// now redirect the user:
header('Location: '.$redirectUrl);
}
// exchanging the authorization code by a access token.
$token = $sdk->auth()->parseReturn();
// set the Token instance on the SDK instance.
$sdk->setToken($token);

All the above contains configuration processes for sc2-sdk-php


`ini_set('max_execution_time', 6000);` : This will increase the execution time of the script beyond the PHP.INI defaults ,an upvote bot takes time to process as this script is gonna take into account the STEEM BLOCKCHAIN time interval of 20 secs between simultaneous upvotes.
`include('vendor/autoload.php');` : This will help include all the sc2-sdk-php classes and helper classes**
`use SteemConnect\Config\Config;` : This helps with configuring the Connection
use SteemConnect\Client\Client; : This helps with CLient Class
`use SteemConnect\Operations\Comment;`,`use SteemConnect\Operations\Vote;`: All these are used comment and voting operations

The rest of the configuration codes can be understood from the comment and you can also sc2-sdk-php to see how it works.

NOTE

`if (empty($_GET['code'])){' : This IF statement is an antidote for redirection issues that can occur, also make sure that the $returnurl is the same as the address of the script. There might be an error if it is not, also the return url must be included in the list of REDIRECT URIs on your SteemConnect APP page (you should know that).

BOT PROCESS

Step 1:


$chosen = array("user1","user2","user3");

This represents the set of votee that needs to be upvoted by the bot, this is good for accounts that are jointly owned for the single purpose of upvoting a certain set of people. There are a lot of accounts like that on steem. Populate the array with as much name as you want.

Step 2:

$voter = 'upvoter';
$loyal = 'upvoter';
 $query = urlencode('{"tag":".$loyal.", "limit": "100"}');
$url = 'https://api.steemjs.com/get_discussions_by_blog?query='.$query;
$json= file_get_contents($url);
$postData = json_decode($json,true);

$voter represents the account that will be responsible for voting.
This bot only upvotes users that upvote the last post of a particular account. That account is represented with $loyal

The last lines queries the steem blockchain and returns the json equivalents of $loyal's last post and then decodes it into a total array. Without the true argument on the last line, the $postData will still contain sub-jsons in each multi-level array.

STEP 3

$n = 0;
foreach ($postData as $item){ 
if ($voter == $item["author"] && $n < 1){
    $votee = array();
    $a = 0;
    foreach ($item["active_votes"] as $itemVoter){  
        if (in_array($itemVoter['voter'],$chosen)){$votee[$a] = $itemVoter['voter']; $a++;} 
        $n++;       
    }
}
}

This gets the list of voters from the $postData and checks it against the $chosen votee so as not vote outsiders.

STEP 4:

foreach ($votee as $v){
 $query = urlencode('{"tag":"'.$v.'", "limit": "100"}');
$url = 'https://api.steemjs.com/get_discussions_by_blog?query='.$query;
$json= file_get_contents($url);
$postData2 = json_decode($json,true);
$n2 =0;
foreach ($postData2 as $item2){ 
if ($v == $item2["author"] && $n2 < 1){
    $link=  $item2['permlink'];
    $d =0;
    $zlist = array();
    foreach ($item2["active_votes"] as $z){
        $zlist[$d] = $z['voter'];
        $d++;   
    }
$n2++;  
}
}
if (!in_array($voter,$zlist)){  
$body = 'Congratulations!,@'.$v.' You have been upvoted';
// upvoting / downvoting a post:
$vote = new Vote();
$p = 45*100;
$vote
    ->account($voter)
    ->on($v,$link)
    ->upVote($p);    
$comment = new Comment();
$comment
    ->reply($v,$link)
    ->permlink($link)
    ->author($voter)
    ->body($body);
echo $v.'<br/>';
// the broadcast method magically accept any number of arguments, so broadcasting multiple operations is easily accomplished.
if ($sdk->broadcast($vote)){echo 'Vote  Made Successfully<br/><br/>';}

// the broadcast method magically accept any number of arguments, so broadcasting multiple operations is easily accomplished.
if ($sdk->broadcast($comment)){echo 'Comment Made Successfully<br/>';}
sleep(25);
}
}

This last foreach performs the whole operation of Upvoting and commenting



The first six lines gets the first post of the verified votee , gotten from the previous foreach.

The nex foreach (foreach ($item2["active_votes"] as $z){......if (!in_array($voter,$zlist)){) gets all the voters on the votee post and then checks if the post has not been upvoted before to avoid error which will be generated if the script votes an already voted post.

Then the $body of the comment and the percentage ($p) is set which is then prepared with $vote and $comment class initializations.

After this, $sdk->broadcast($comment) & $sdk->broadcast($vote) broadcast the operation ,meaning the operation is sent into the blockchain via sc2-sdk-php

The SLEEP(25) is there to make sure there is a gap between each upvote, an error will be generated if there is less than 20 seconds time frame between each upvote.

Because of these timeframe, these script can take time to finish.

Now, let's get the whole script.

<?php
ini_set('max_execution_time', 1000);
include('vendor/autoload.php');
// alias the Config class.
use SteemConnect\Config\Config;
use SteemConnect\Client\Client;
use SteemConnect\Operations\Comment;
use SteemConnect\Operations\CommentOptions;
use SteemConnect\Operations\Vote;
// oauth client id.
$clientId = 'your-app-name';
// oauth client secret.
$clientSecret = 'app-secret-key';
// return url.
$returnUrl = 'https://localhost/bb';
// list of required scopes.
$scopes = [
    'login', 'offline', 'vote', 'comment', 'comment_delete',
    'comment_options', 'custom_json', 'claim_reward_balance',
];
// starts the configuration object, passing the client id and secret.
$config = new Config($clientId, $clientSecret);
// configure the return / callback URL.
$config->setReturnUrl($returnUrl);
// set the reqired scopes.
$config->setScopes($scopes);
// set the return/callback URL, so SteemConnect will redirect
// users back to your application.
$config->setReturnUrl('https://localhost/bb');
$sdk = new Client($config);
// get the URL to send the users that will authorize your app.
$redirectUrl = $sdk->auth()->getAuthorizationUrl();
if (empty($_GET['code'])){
// now redirect the user:
header('Location: '.$redirectUrl);
}
// exchanging the authorization code by a access token.
$token = $sdk->auth()->parseReturn();
// set the Token instance on the SDK instance.
$sdk->setToken($token);
$chosen = array("user1","user2","user3");
$voter = "upvoter';
$loyal= 'upvoter';
   $query = urlencode('{"tag":"'.$loyal.'", "limit": "100"}');
$url = 'https://api.steemjs.com/get_discussions_by_blog?query='.$query;
$json= file_get_contents($url);
$postData = json_decode($json,true);
$n = 0;
foreach ($postData as $item){ 
if ($voter == $item["author"] && $n < 1){
    $votee = array();
    $a = 0;
    foreach ($item["active_votes"] as $itemVoter){
        if (in_array($itemVoter['voter'],$chosen)){$votee[$a] = $itemVoter['voter']; $a++;}
        $n++;   
    }
}
}
foreach ($votee as $v){
 $query = urlencode('{"tag":"'.$v.'", "limit": "100"}');
$url = 'https://api.steemjs.com/get_discussions_by_blog?query='.$query;
$json= file_get_contents($url);
$postData2 = json_decode($json,true);
$n2 =0;
foreach ($postData2 as $item2){ 
if ($v == $item2["author"] && $n2 < 1){
    $link=  $item2['permlink'];
    $d =0;
    $zlist = array();
    foreach ($item2["active_votes"] as $z){
        $zlist[$d] = $z['voter'];
        $d++;       
    }
$n2++;  
}
}
if (!in_array($voter,$zlist)){  
$body = 'Congratulations!,@'.$v;
// upvoting / downvoting a post:
$vote = new Vote();
$p = 45*100;
$vote
    ->account($voter)
    ->on($v,$link)
    ->upVote($p);
$comment = new Comment();
$comment
    ->reply($v,$link)
    ->permlink($link)
    ->author($voter)
    ->body($body);
    echo $v.'<br/>';
// the broadcast method magically accept any number of arguments, so broadcasting multiple operations is easily accomplished.
if ($sdk->broadcast($vote)){echo 'Vote  Made Successfully<br/><br/>';}
// the broadcast method magically accept any number of arguments, so broadcasting multiple operations is easily accomplished.
if ($sdk->broadcast($comment)){echo 'Comment Made Successfully<br/>';}
sleep(25);
}
}


With this, you can now run the script from your browser. This bot still needs to be run the first time before it can now run on its own , this is to authenticate the $voter .

You can find the codes of this tutorial via this github link



Posted on Utopian.io - Rewarding Open Source Contributors

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thanks for sharing this @akintunde, I have not messed around with any PHP since a very very long time ago... So I don't understand much but maybe I should try to get into it again...

That would be nice. Looking forward to that.

Brilliant!

Thank you ma

Congratulations!,@akintunde Your post has been upvoted by Reach Out, which is proudly sponsored by @eturnerx. Our goal is to support Nigerian minnows on Steemit. Join our discord group https://discord.gg/NWAkKfn

Benefactor : @bleepcoin

Curator On Duty: Richie, the Manual Bot

We invite you to the @eoscafe community [https://discord.gg/wdUnCdE]

Hey @akintunde I am @utopian-io. I have just upvoted you!

Achievements

  • Seems like you contribute quite often. AMAZING!

Utopian Witness!

Participate on Discord. Lets GROW TOGETHER!

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Hello @akintunde ... I'm a PHP developer in Uyo, Nigeria. I have always wanted to build some cool tools on the steem blockchain using PHP preferably with the Laravel Framework.
I believe this tutorial will help me alot on getting thee basic concepts. Thank YOU for this

Hello @akintunde ... I'm a PHP developer in Uyo, Nigeria. I have always wanted to build some cool tools on the steem blockchain using PHP preferably with the Laravel Framework.
I believe this tutorial will help me alot on getting thee basic concepts. Thank YOU for this