Tutorial - Running your Steem.js Bot 24/7 on a Server #11

in #utopian-io6 years ago (edited)

bot-tut.png

Learn to make bots and projects with Steem-JS. This tutorial is part of series that aims to demonstrate practical applications for building bots and projects with Steem-js.

Tutorials - 1-10

The completed bot code is here - I know some people find it easier to see everything in one block and follow along that way. ⬅️

Outline 📝

I’ve had a number of people ask If I can do a tutorial about running bot code on a server. Shout to @aussieninja. If you want your bot to run 24/7 you’ll need it to sit on a server. The missing piece of the puzzle.

Difficulty

Intermediate - Please run through all other tutorials in the series to better understand what we’re working on.

Learning Goals

  • Create a Steem JS bot that runs 24/7 🤖
  • use the now-cli tool from zeit.co
  • setup environment variables to safely store secret data

Intro

A heads up this might not be the tutorial you’re hoping for. There are ways you can get free or trial accounts for severs and or cloud service but I’m going to recommend that if you want to run your bot 24/7 that you ignore those.

Why? it’s not worth the hassle. Unless you want to be a sys admin I’d wait before learning how do DIY server setup. It’s fun and a great skill to have but I certainly wouldn’t recommend it to beginners. The first time I setup a VPS after following a number of tutorials for hours I proceeded to set a specific port for access then block that only port. oops, start again from scratch.

You might look at the one-year free AWS and think that’s awesome but honestly, it will save you $5/mo or $60 total for using a service you wouldn’t choose unless it was free. $60 is not worth the time investment if you’re choosing it only because it’s free.

Today we’re going to look at using Zeit.co a cloud hosting platform that is super friendly. Far more enjoyable. You can use it for free to get everything setup then if you want to pay to run your bot 24/7 it’ll cost you about $5, cheaper if you run more. p.s you can also use it for free if your project is open source https://zeit.co/account/plan 🙌

I know I know it costs $$ but if whatever you’re doing isn’t worth $5 a month to you or someone else then why are you doing it?

Bot Setup

Code to get started or use your code from a previous bot tutorial I am using the code from Tutorial #2 that acts as a curation trail matching the votes of a target user.

We’re starting from the previous tutorial that was written to run in the browser. First, we’ll strip all of the HTML leaving just the javascript, change the file extension and include steem as a node module through npm rather than linking to the CDN.

  • cd bot-11/ - navigate to the correct folder in your terminal app.
  • npm init - create a new npm project so you can include packages.
  • npm install steem —save - include steem as a package and save it to the package.json file so once deployed the server knows to do the same.
    -update your ACCOUNT_NAME & ACCOUNT_KEY and check it’s working.
  • node bot-11.js to kickstart the bot. Vote from your target account and watch as your main account copies the upvote. If all is good we should have a node.js based bot ready to go on a server. Next we’ll look at our server host of choice and how to secure that private key.

Account Setup

Next create an account over on [https://zeit.co/login]. Then go ahead and download the command line tool from the website or by installing it globally through Node.js npm install -g now. There is also a desktop app that helps visually see what deployments you have.

Fire up your command line and check Now is installed by testing for its version now -v.

Creating secrets & Deploying

Before moving any code from our computer it’s important realise the posting key we have used in the tutorials should never be in your source code. I always use a separate account/key for testing just to be safer.

In your terminal run the secret command to add the key as a value. now secret add posting-key value-here

Add a now.json with an environment key matching your new secret.

{
  "env": {
    "POSTING_KEY": "@posting-key"
  }
}

On deployment now looks for a start script to know how to run your Node.js project this is part of your package.json file.

  "scripts": {
    "start": "node ./bot-11.js"
  },

You posting key is now safely available in your code as an environment variable.process.env.POSTING_KEY This is a safe way to use private information such as password, tokens, keys within applications that run on remote servers or where the source code is public. The might have seemed like a couple hoops to jump through but as hosting/server config goes it was about as simple as it gets.

Deploy it

If you managed to add your post key as an environment variable you’re ready to launch. 🚀

It’s as simple as running now in the command line to deploy.

now

> Deploying ~/Projects/steem/bot-11 under [email protected]
> Using Node.js 8.9.4 (default)
> Ready! https://bot-11-1337jkjkjk.now.sh (copied to clipboard) [3s]
> Initializing…

OUTPUT - you’ll get a URL to follow where you can look at the logs. you can also visit your dashboard https://zeit.co/dashboard click your app then ’logs’ toward the top right.

Screen Shot 2018-02-16 at 21.50.47.png

Setup

Screen Shot 2018-02-16 at 21.50.32.png
Voting

You will likely get the No open port found error because now expects a web server running by default.
Screen Shot 2018-02-16 at 21.50.55.png

That the only downside of using now I’ve found. You can simply add a one-line HTTP server to your app so now allows you to scale as usual. e.g

const {createServer} = require('http').createServer().listen(3000)

Run 24/7

By default all now instances will go to sleep after awhile (they’re ambiguous about how long) so we’ll need to set the minimum instance to 1 so it stays online. This is where it will either cost you money or your code needs to be open source. Run now scale with you url provided earlier with 1 instance

now scale https://bot-11-1337jkjkjk.now.sh 1

With that, your bot is ready to take on the world while you sleep.

Let me know if you need/want help setting up your bots! I know this isn’t a perfect solution but I enjoy nows simplicity compared to other options. If there are better hosting options out there that you have used please let me (and the other readers) know in the comments.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Hey Sam! This is amazing!! Thank you so much for this...

I've got myself a bit tangled and would love it if you could help me... once I get this first one under control, I should be good to deploy the others. I've sent you a message in Discord if you wouldn't mind checking it out?

Thanks so much for this... super super super appreciate it!

Great tutorial about running bots. It will be very helful who are related and interested with this bots. Thank you for sharing.

@sambillingham, Upvote is the only thing I can support you.

Gracias por siempre tratar de ayudarnos amigo mio :D

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Great tutorial.
very helpful.
Thank you for sharing.

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

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

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

Coin Marketplace

STEEM 0.19
TRX 0.13
JST 0.030
BTC 59965.71
ETH 3286.33
USDT 1.00
SBD 2.36