{"id":1168,"date":"2015-04-23T02:09:36","date_gmt":"2015-04-23T02:09:36","guid":{"rendered":"https:\/\/www.courses.tegabrain.com\/SS15\/?page_id=1168"},"modified":"2015-04-23T19:49:04","modified_gmt":"2015-04-23T19:49:04","slug":"deploying-a-bot-to-a-server","status":"publish","type":"page","link":"https:\/\/www.courses.tegabrain.com\/SS15\/?page_id=1168","title":{"rendered":"Deploying a bot to a server"},"content":{"rendered":"<p>Example file <a href=\"https:\/\/www.courses.tegabrain.com\/SS15\/wp-content\/uploads\/2015\/04\/imagebot.zip\">download here.<\/a><\/p>\n<p>You will deploy your bots on a <a href=\"https:\/\/www.heroku.com\/\">Heroku server<\/a>. Heroku is a remote server service. It offers free server space for low use. It also has a good command line interface. HUZZAH!<\/p>\n<p>You will be deploying your bot using the command line and a program called git. Git is a version management system that also underlies the website &#8216;Github&#8217;. It is a way of sending files between your computer and a server and keeping track of the different versions and how each version is changed. It can be VERY complicated so we are only going to use it a little as we have to to manage the versions of your bot.<\/p>\n<p>Don&#8217;t worry, this tutorial outlines how you can use it to get your bot running autonomously.<\/p>\n<h2>Setup<\/h2>\n<p>1. Install git. For a mac you can install git from the Git for Mac website at <a href=\"http:\/\/mac.github.com\">http:\/\/mac.github.com<\/a>. For Windows, install from here: <a href=\"https:\/\/windows.github.com\/\">https:\/\/windows.github.com\/<\/a>. During install, this tool has an option to install command line tools as well. Make sure you choose this option.<\/p>\n<p>2. <a href=\"https:\/\/signup.heroku.com\/www-header\">Sign up for a Heroku account<\/a>. Choose node.js as your development language. Login to your account and follow the instructions to download and install the Heroku toolkit for your operating system <a href=\"https:\/\/devcenter.heroku.com\/articles\/getting-started-with-nodejs#set-up\">from here.<\/a> (https:\/\/devcenter.heroku.com\/articles\/getting-started-with-nodejs#set-up).<\/p>\n<p>Once you have finished making your bot javascript file and set up your config file with the twitter authentification information, go to your terminal. We are now going to set up.<\/p>\n<p>3. Open your terminal and cd to the folder where you have your bot files saved.<\/p>\n<p>4. Check your bot is working with no errors, you should be able to run it in your terminal by calling<\/p>\n<pre lang=\"java\">node yourfilename.js<\/pre>\n<h2>Git in your local directory<\/h2>\n<p>5. Now make your local folder a Git repository. This adds a .git folder to the current directory and makes it possible to start recording revisions of the project. It is a bit of software that tracks what version is in your local folder and what version is in your remote folder (on the server). You do this by:<\/p>\n<pre lang=\"java\">git init<\/pre>\n<h2>Authenticate with Heroku<\/h2>\n<p>6. Authenticate with heroku on your computer. Do this by entering<\/p>\n<pre lang=\"java\">heroku login<\/pre>\n<p>and enter your heroku user name and password.<\/p>\n<h2>Create your app on the Heroku server.<\/h2>\n<p>7. You can do this with the following command. This creates a placeholder for where we will upload your new app on the Heroku server. Doesn&#8217;t matter what you call it here, its just for your future reference. I&#8217;ve called mine filebotty.<\/p>\n<pre lang=\"java\">heroku create filebotty<\/pre>\n<p>You should see text print out in the terminal like:<br \/>\n<em>Creating filebotty&#8230; done, stack is cedar-14<br \/>\nhttps:\/\/filebotty.herokuapp.com\/ | https:\/\/git.heroku.com\/filebotty.git<\/em><\/p>\n<p>You can check also this worked by going to the heroku website and now looking at the menu option myApps where you should see the name of your app in the list.<\/p>\n<p>8. Set this up as your remote directory with the command:<\/p>\n<pre lang=\"java\">heroku git:remote -a filebotty<\/pre>\n<p>9. Check it worked by entering:<\/p>\n<pre lang=\"java\">git remote -v<\/pre>\n<p>. You should see something like this print out:<br \/>\n<a href=\"https:\/\/www.courses.tegabrain.com\/SS15\/wp-content\/uploads\/2015\/04\/Screen-Shot-2015-04-22-at-10.46.29-PM.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1180\" src=\"https:\/\/www.courses.tegabrain.com\/SS15\/wp-content\/uploads\/2015\/04\/Screen-Shot-2015-04-22-at-10.46.29-PM.png\" alt=\"Screen Shot 2015-04-22 at 10.46.29 PM\" width=\"1014\" height=\"292\" srcset=\"https:\/\/www.courses.tegabrain.com\/SS15\/wp-content\/uploads\/2015\/04\/Screen-Shot-2015-04-22-at-10.46.29-PM.png 1014w, https:\/\/www.courses.tegabrain.com\/SS15\/wp-content\/uploads\/2015\/04\/Screen-Shot-2015-04-22-at-10.46.29-PM-300x86.png 300w, https:\/\/www.courses.tegabrain.com\/SS15\/wp-content\/uploads\/2015\/04\/Screen-Shot-2015-04-22-at-10.46.29-PM-535x154.png 535w\" sizes=\"(max-width: 1014px) 100vw, 1014px\" \/><\/a><br \/>\nNow you have set up a link between your local file and the folder on the Heroku server. Good job! Nearly there.<\/p>\n<h2>Prepare your bot files for deployment<\/h2>\n<p>10. Now it is time to push your files to your remote folder. Git requires you do this in 3 stages, add, commit, and push.<br \/>\nStage your files.<\/p>\n<pre lang=\"java\">    git add .\r\n<\/pre>\n<p>Now commit the changes to the repository:<\/p>\n<pre lang=\"java\">    git commit -m \"Demo\"\r\n<\/pre>\n<p>Now deploy, just as you did previously:<\/p>\n<pre lang=\"java\">    $ git push heroku master\r\n<\/pre>\n<p>Congratulations you have pushed your first app to a remote server.<\/p>\n<h2>Package.json<\/h2>\n<p>You are not quite finished. If your repository (your folder) does not contain a Package.js file or a Procfile you will need to create one. If it does, you need to update, save and then push this to heroku.<\/p>\n<p>If you don&#8217;t have these, add in a json file that will enable your bot to run. This file contains instructions as to what modules your bot uses to run. JSON refers to a file with a particular structure for storing data. Many APIS return their data as a JSON file. Generally JSON files have lots of brackets.<\/p>\n<p>In your text editor open a new file and copy the <a href=\"https:\/\/gist.github.com\/tegacodes\/cbd21c60c3db63861512#file-gistfile1-txt\">code from here into it<\/a>. Update the author with your name and twitter handle. Save the file in your twitter bot repository as package.json<\/p>\n<h2>Create a Procfile<\/h2>\n<p>Create a file called Procfile in your project\u2019s folder using your text editor. This file tells the server what sort of app you\u2019re running and the command that runs the app.<\/p>\n<p>The default Node.js gettings started docs explain that you should define a process type of web, but in our case, without a web interface to our Twitter bot, we need a different type of single process type.<\/p>\n<p>There are a variety of process types that can be defined in a Procfile, but for something really simple like our Twitter bot, we just a need a worker. A worker is a background job that can run outside a user-initiated request\/response cycle.<\/p>\n<p>Put the following text into your Procfile (using the name of your js file).<\/p>\n<p>worker: node filebot.js<\/p>\n<p>This file does not have an extension.<\/p>\n<h2>Push changes to Heroku<\/h2>\n<p>As you change and modify your bot you will want to push them to the remote server. Edit your files as normal and when you are done, save them and cd into the folder from the terminal. Once there enter the following:<\/p>\n<p>Stage local changes<\/p>\n<pre lang=\"java\">    git add .\r\n<\/pre>\n<p>Now commit the changes to the repository:<\/p>\n<pre lang=\"java\">    git commit -m \"Demo\"\r\n<\/pre>\n<p>Now deploy, just as you did previously:<\/p>\n<pre lang=\"java\">    git push heroku master\r\n<\/pre>\n<p>Now you have a bot, ready to go on your Heroku server. Well done.<\/p>\n<h2>It&#8217;s alive<\/h2>\n<p>Last thing to do is to go to the Heroku website, go to myApps. Click on your app and go to the Resources folder. You need to set up a dyno. This is the process that will kick off your bot. Click edit over on the right and then pull the slider so that you have one dyno running.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Example file download here. You will deploy your bots on a Heroku server. Heroku is a remote server service. It offers free server space for low use. It also has a good command line interface. HUZZAH! You will be deploying your bot using the command line and a program called git. Git is a version [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":[],"_links":{"self":[{"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=\/wp\/v2\/pages\/1168"}],"collection":[{"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1168"}],"version-history":[{"count":10,"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=\/wp\/v2\/pages\/1168\/revisions"}],"predecessor-version":[{"id":1226,"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=\/wp\/v2\/pages\/1168\/revisions\/1226"}],"wp:attachment":[{"href":"https:\/\/www.courses.tegabrain.com\/SS15\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}