How to Post Directly to Steem Using Ruby

in #steem7 years ago

You can post DIRECTLY to the Steem blockchain using Ruby in just a few lines of code!

More than five tags are now possible

I was planning on posting part 3 of my Steem API Basics series, but I got so damn excited writing this code that I had to share it right away!

A recent project with @steemsmarter required me to automate the posting of our daily reports. Using the BADASS code created by https://steemit.com/@inertia with his awesome Radiator gem for Ruby, I came up with the following simple class to make it easy for anyone's Ruby app to post directly to the Steem blockchain itself. (view the gist here)

require 'radiator'

class SteemPostBroadcaster
  attr_accessor :options

  def initialize(opts={})
    @options = opts
  end

  def post!
    raise "Post is missing title, body, or tags" unless is_valid_post?
    account.post!(post_options)
  end

  def post_options
    {
      title: @options[:title],
      body: @options[:body],
      tags: @options[:tags],
      percent_steem_dollars: percent_steem_dollars,
      max_accepted_payout: max_accepted_payout,
      allow_votes: allow_votes,
      allow_curation_rewards: allow_curation_rewards
      #self_vote: self_vote
    }
  end

  def account
    @account ||= Steem.new(account_name: ENV['ACCOUNT_NAME'], wif: ENV['WIF'])
  end

  private

  def percent_steem_dollars
    @options.fetch(:percent_steem_dollars, 10000)
  end

  def max_accepted_payout
    @options.fetch(:max_accepted_payout, "1000000.000 SBD")
  end

  def allow_votes
    @options.fetch(:allow_votes, true)
  end

  def allow_curation_rewards
    @options.fetch(:allow_curation_rewards, true)
  end

  def self_vote
    @options.fetch(:self_vote, true)
  end

  def is_valid_post?
    [:title, :body, :tags].all? { |opt| !@options[opt].blank? }
  end
end

How it works

The Radiator gem by @inertia has some serious functionality baked in for easily interacting with the Steem blockchain! Let's take a look at how to use the Radiator::Chain class to publish posts.

Set up your ENV variables

You need to have the following two variables defined in your .env file or in your remote providers's configuration settings (aka Heroku Settings -> Config Variables):

  • ACCOUNT_NAME: name of Steem account you will be posting under
  • WIF: Steem account POSTING permission private key (NOT your Owner key!) -- see this great article by @rgeddes about Getting Your Posting Key for more details.

Connecting your account to the Steem blockchain

SteemPostBroadcaster uses the Radiator::Chain class (source code) to set up a up a Steem object for a given account:

def account
  @account ||= Steem.new(account_name: ENV['ACCOUNT_NAME'], wif: ENV['WIF'])
end

Now we can use account to directly interact with the Steem blockchain! Steem objects use Radiator::Chain to post, vote, comment, and even transfer funds on the blockchain. For this post, we're focusing on the .post! method.

Create your post options hash

The most basic post options hash is very easy to build. SteemPostBroadcaster will throw an error if these three parameters are not defined:

opts = {
  title: "My Post Title",
  body: "My Post Body",
  tags: ["tag-1","tag-2"]
}

Here's a further breakdown of these variables:

  • title (string)
    The title of the post. The title is automatically dasherized as the post permlink.

  • body (string)
    The full markdown and/or HTML body of the post.

  • tags (array of strings)
    Array of tags for the post. The first tag will automatically be used as the category.

Post away!

All we have left to do now is feed our options hash opts into a new instance of SteemPostBroadcaster and tell it to post!

THIS IS THE MAGIC PIECE OF CODE

SteemPostBroadcaster.new(opts).post!

And that's it! Our post will almost instantly be pushed to the Steem blockchain! If it works, you will get a response with information on the transaction block in which your post was published, eg:

<Hashie::Mash id=1 result=#<Hashie::Mash block_num=20907858 expired=false id="65d4a6709f6d4c69fd0cbdf9a6fbdcf43acdc2de" trx_num=35>>

If there's a problem with your account authorization or the contents of your post options hash, you will get an error as response instead, eg:

<Radiator::ErrorParser [3030000: missing required posting authority]>

Seriously awesome advanced features

The post options hash supports a number of optional advanced features, some which let you do some really awesome stuff.

extended_opts = {
  percent_steem_dollars: 10000,
  max_accepted_payout: "1000000.000 SBD",
  allow_votes: true,
  allow_curation_rewards: true
}

Here's a breakdown of these variables:

  • percent_steem_dollars (integer between 0 and 10000)
    Percentage of the payout to pay in Steem Dollars (SBD). For 50/50 payout, this value is 5000; for 100% SBD payout, this value is 10000. As with all Steem percentages, you have to divide this number by 100 to get the traditional percentage (eg 5000 -> 50.0%) and divide by 10000 to get the decimal percentage (5000 -> 0.5)

  • max_accepted_payout (string)
    This parameter is a string of the integer value of the max payout plus the string "SBD". By default, the max_accepted_payout is the truly enormous value of "1000000.000 SBD" (good luck earning this much!).

  • allow_votes (boolean)
    By default voting is allowed. Set this to false to disable voting on the post.

  • allow_curation_rewards (boolean)
    By default curation rewards are allowed. Set this to false to disable voting on the post.

Post with more than five tags!

Who says you have to be restricted to the five tag limit imposed by Steemit.com and Busy.org? In the SteemPostBroadcaster you can go crazy with you tags; just fill up that tags array in the post options hash with all the tags you want to use.

That said, don't abuse the platform! There's a big difference between posting 6-10 tags and posting 30 tags: one is reasonable, the other is spammy.

Customize your payouts!

Steemit.com and Busy.org restrict you to either 100% SBD or 50/50 SBD/SP payouts. But what if you want to, say, get mostly paid but keep a little SP coming in... for example, an 80/20 SBD/SP payout?

Now you can! All you have to do is set percent_steem_dollars to any value between 0 and 10000 to change your payout percentage. For example, we would set our percent_steem_dollars to 8000 to collect 80/20 payout.

Decline your payouts!

If you're feeling particular altruistic, you can easily decline payout on your post. Simply set the value max_accepted_payout to "0.000 SBD" and your payouts will be declined.

One minor problem: self voting (help!)

The Radiator gem Radiator::Chain class appears to have the functionality to handle a self_vote boolean on the post options hash, which is supposed to automatically self-vote for the post as soon as it is published. See the self_vote code here

However, every time I define any value for self_vote on the post options hash, the post fails to post and returns the <Radiator::ErrorParser [3030000: missing required posting authority]> error. You'll notice I've commented it out in the SteemPostBroadcaster code above.

@inertia and the rest of the Steem/Ruby community, if you have any idea what's going on here and how to make it work, please let me know in the comments... I'd really appreciate it.

What's Next?

I promise I'll get back to finishing up my post about parsing votes from the discussion payload!

Thanks for reading, and if you have any questions please ask away in the comments section. If you enjoyed this, please resteem and upvote, and remember to follow @thescubageek and @steemsmarter to stay on top of our latest Steem projects.

Until next time, keep on Steemin' Steemians!

Sort:  

Great insight to be had here, glad I found this (*bookmarking) however I'm still in hot pursuit for a post queue or an automator to regularly post updates to my blog. Either way, a step in the right direction here. :D

Thank you for sharing @thescubageek!

Thanks @craigahamilton! I actually have some scripts that accomplish exactly what you are describing! They aren't quite in a shareable state yet, but I look forward to leaking details in future posts.

Take a look into Sidekiq scheduled jobs for an idea of how I pull it off....

Awesome!
I've followed you for more golden knowledge like this, so I wont miss it.
I've got a few post templates at my disposal I 've created to help speed up my regular posts like my divinations, or my recap posts, which I could really use an automator process to go, get, gather my post content over a span of time and compile them on a regular basis. It would especially helpful when I've had a very busy posting month and have 50+ posts to sort through, label, link, and tag content whew
Note the signals I embedded...if ever such a posting agent so sophisticated that could handle this workload, I'd be forever grateful.

Much obliged @thescubageek!

Super impressed with what you have put together here @thescubageek and @steemsmarter!

so amazing to see how far we (@SteemSmarter) and you have come in the past 10 weeks or so. Absolutely ridiculous. You are completely rocking the Steem blockchain now!

Thanks @ashe-oro I look forward to coding the next evolution of @steemsmarter and our library of awesome Steem code!

One thing about your blog that differentiates it from the rest is the way you make us understand about everything. Much appreciated.

So, did anyone else catch that serious cheat code... You can go higher than 50% SBD? So with SBD being > $2 you could essentially double instead of the present 1.5x payouts... This is huge.

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 62948.49
ETH 2583.15
USDT 1.00
SBD 2.74