Twitter Bot with NodeJS

Slides:



Advertisements
Similar presentations
Client-server practices DSC340 Mike Pangburn. Agenda Overview of client-server development Editing on client (e.g., Notepad) or directly on server (e.g.,
Advertisements

Functionality and API. Talk Goals How do I set this up? What can I do with it? Where is this going?
Presentation Heading – font Arial
Using Evernote and Google Docs in your web or mobile application (and potentially Dropbox and Skydrive) By Peter Messenger Senior Developer – Triple Point.
Linux, it's not Windows A short introduction to the sub-department's computer systems Gareth Thomas.
Web Application Server Apache Tomcat Downloading and Deployment Guide.
Command Console Tutorial BCIS 3680 Enterprise Programming.
What is Dropbox ?– Dropbox is a file storage site which gives you an easy way to save your documents, files, and presentations online and access them from.
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
Getting Started with GIT. Basic Navigation cd means change directory cd.. moves you up a level cd dir_name moves you to the folder named dir_name A dot.
Streaming Twitter. Install pycurl library Use a lab computer From the course website Download the links from pycurl and twitter streamer Extract site-packages.zip,
MySQL + PHP.  Introduction Before you actually start building your database scripts, you must have a database to place information into and read it from.
Support Training Module. Support Manual 1.“On The Lot” – How it all works… 2.Craigslist Settings 3.Post to Craigslist 4.Backpage Settings 5.Post to Backpage.
An Introduction to Front-end Web Development Tom Perkins.
How To Download Music Legally and Free By Mahmud Fagge.
EIE375 BlueJ: Getting Started Dr Lawrence Cheung.
Programming for GCSE 1.0 Beginning with Python T eaching L ondon C omputing Margaret Derrington KCL Easter 2014.
Implementing and Using the SIRWEB Interface Setup of the CGI script and web procfile Connecting to your database using HTML Retrieving data using the CGI.
Unix Servers Used in This Class  Two Unix servers set up in CS department will be used for some programming projects  Machine name: eustis.eecs.ucf.edu.
1 Getting Started with C++ Part 1 Windows. 2 Objective You will be able to create, compile, and run a very simple C++ program on Windows, using Microsoft.
Practical Kinetics Exercise 0: Getting Started Objectives: 1.Install Python and IPython Notebook 2.print “Hello World!”
HINDU STYLE PORTFOLIO TEMPLATE
Learn R Toolkit D Kelly O'DayInstall & SetupMod 1 - Setup: 1 Module 1 Installing & Setting Up R Do See & HearRead Learn PowerPoint must be in View Show.
Node.JS introduction. What is Node.JS? v8 JavaScript runtime Event driven Non-blocking standard libraries Most APIs speak streams Provides a package manager.
Online Conference June 17 th and 18 th Modern SharePoint Development using Visual Studio Code.
APP DESIGN AND DEVELOPMENT WITH THE IONIC FRAMEWORK Chuck Leone
Office Introduction BERNARD DADY Bernard Dady.
Holland Computing Center STAT802 Create and access Anvil Windows 10 SAS instance 01/23/2017.
Core ELN Training: Office Web Apps (OWA)
bitcurator-access-webtools Quick Start Guide
Social Network.
NodeJS and MEAN cs6320.
CS1010: Intro Workshop.
Running a Forms Developer Application
Presentation Heading – font Arial
How to use IoT in Bluemix
Physics validation database
How to link a test to a launcher (in this case a shell launcher)
Web Concepts Lesson 2 ITBS2203 E-Commerce for IT.
Node.js Express Web Applications
Data Virtualization Demoette… ADO.NET Client
Getting Started with R.
Data Virtualization Tutorial… OAuth Example using Google Sheets
NodeJS and MEAN Prof. L. Grewe.
App deployment in Cloud
Node.js Packages Header Mastering Node.js, Part 4 Eric W. Greene
Skill Based Assessment
How to and Lessons Learned!
File Transfer Olivia Irving and Cameron Foss
CCA Skill Certification
Unit 8 NT1330 Client-Server Networking II Date: 8/2/2016
INSTALLING AND SETTING UP APACHE2 IN A LINUX ENVIRONMENT
Lecturer: Mukhtar Mohamed Ali “Hakaale”
NodeJS coding basics L. Grewe.
Tech Drop In: Google Drive
Using K2 applications How can users interact with K2 applications?
IOTA HOW TO START BUILDING.
CGS 3175: Internet Applications Fall 2009
Module P3 Practical: Building a webapp in nodejs and
Cordova & Cordova Plugin Installation and Management
Lecture 16B: Instructions on how to use Hadoop on Amazon Web Services
bitcurator-access-webtools Quick Start Guide
Introduction to Group Policy
CSCI N207 Data Analysis Using Spreadsheet
Java IDE Dwight Deugo Nesa Matic Portions of the notes for this lecture include excerpts from.
Lecture 12: The Fetch Api and AJAx
Executing Host Commands
CSCE 206 Lab Structured Programming in C
Web Programming and Design
Web Programming and Design
Presentation transcript:

Twitter Bot with NodeJS A presentation by Sean O’Mahoney

What is NodeJS? NodeJS is a JavaScript runtime, you can type in JavaScript “Node is designed to build scalable network applications” Async…

Setting up NodeJS NodeJS is installed on the Uni Machines, under the Campus Applications then under a “NodeJS” folder (not the “documentation” or “website”) Although it is free to install on your laptop/machine (with all programs mentioned in this presentation), by simply following the link - https://nodejs.org/en/download/

Quick Demonstration

Setting up a Project / Creating a File Create a new folder on our Desktop called “NodeJS Twitter Bot” On Uni machines, copy the file directory location, then with NodeJS open, type “cd ” then paste (changing the directory to the folder on our desktop) On your laptop/machine (without restrictions), you can type “cmd” on the file directory location to open a command prompt accessing that area From there we will type “npm init” (Node Package Manager - Initiate) Once we have done that, we will then create a .js file in that folder Open up this JavaScript file in an editor, personally I use Brackets (with the “Monokai Dark Soda” theme and various plug-ins)

Importing Libraries You can search for packages/libraries on the Node Package Manager site https://www.npmjs.com/ “npm install x” (x is the library you will use for your project, twit, being for this)

Using a Library (twit) “var x = require(‘y’);” “x” being what you define for the use of the library “y” being the predefined name of the library “var Twit = require(‘twit’);” “var z = new x()” “z” being a new object of the library type “x” being the library variable defined above “var T = new Twit()” Next we will discuses the paraments of the “new Twit()”, being an object with 4 properties

Creating a Twit Instance We will replace these dots with the keys we will gain later on: var Twit = require('twit') var T = new Twit({ consumer_key: '...', consumer_secret: '...', access_token: '...', access_token_secret: '...‘, })

Hiding Keys The “require” statement is not only used for Libraries', it can also be used to import files. Create a new js file in the same directory and name it “keys.js” Now open it in your text editor and place your keys in there, like this: module.exports = { consumer_key: ‘…', consumer_secret: ‘…', access_token: ‘…', access_token_secret: ‘...', }

Hiding Keys (Part 2) We will now create another requires statement, but this time it won’t be in the default “node_modules” folder, so we have to go up a directory (using “./”), then specify the file (not having to include the “.js”). var Keys = require('./keys'); Now we can replace the object in the creation of an instance, like so: var T = new Twit(Keys);

Twitter Keys (API) To use twit, we will need to pass through 4 keys in the object, needed for the API (Application Program Interface). Create or Log In to your Twitter Account Ensure there is a mobile number associated with your account Then go to https://apps.twitter.com/ Create a new app Go to “Keys and Access Tokens” Generate Your Access Token “Regenerate My Access Token and Token Secret”

Twit Functions There are 3 main functions in the Twit Library: Post Get Stream The first two have these arguments: T.y(‘type', { object }, function(err, data, response) { //y being post/get Code //console.log(data) })

Posting a Tweet (.post) We will now use that “T” twit instance we created, to simply post to the Twitter account To achieve this, we will use the “post” function specified in the documentation This will use the “statuses/update” type, have an object which contains the status, then the returned values from the function. Then we’ll simply show what was pushed to Twitter on the console. T.post('statuses/update', { status: 'hello world!' }, function(err, data, response) { console.log(data) })

Repetition We will first call our function when we run our code, then it will run again on an interval, we first define which function we want to run, then specify the time (being in milliseconds), setting it to 1000, will run it every second. process(); setInterval(process,1000*5); //5 Seconds function process() { //Code }

Searching Tweets (.get) – References If we wish, we can search for tweets similar to how we would with the search bar, using the code below, where “q” is our query (search terms). T.get('search/tweets', { q: '#CodingRainbow', count: 10 }, function(err, data, response) { console.log(data.statuses) })

Like/Retweet (.stream) var stream = T.stream('statuses/filter', { track: '#MMUNodeJSDemo' }) stream.on('tweet', gotTweet); function gotTweet(tweet) { console.log('Retweeting ' + tweet.id_str + ": " + tweet.text); T.post('favorites/create', { id: tweet.id_str }, retweeted); //Likes T.post('statuses/retweet', { id: tweet.id_str }, retweeted); //Retweets function retweeted(err, data, response) { if (err) { console.log("Error: " + err.message); } else { console.log('Retweeted: ' + tweet.id); } }

Followers (.stream) var stream = T.stream('user'); stream.on('follow', followed); function followed(eventMsg) { var name = eventMsg.source.name; var screenName = eventMsg.source.screen_name; T.post('statuses/update', { status: 'Thank you ' + '@' + screenName + ', for the follow!' }, tweeted); function tweeted(err, data, response) { if (err) { console.log(err); } else { console.log('Success: ' + data.text) } }

Thank You! - Any Questions? Sean O'Mahoney Email: sean.omahoney@stu.mmu.ac.uk LinkedIn: https://uk.linkedin.com/in/sean12697 ePortfolio: https://seanomahoney.me/ GitHub Repo’ for Examples: https://github.com/Sean12697/TwitNodeJS Computing Society Facebook: “MMUComputingSociety” Twitter: @MMUCompSoc Union: Computing Society Resource's NodeJS (REQUIRED): https://nodejs.org/en/download/ Brackets (Preferred Text Editor): http://brackets.io/ NPM (Node Package Manager Search): https://www.npmjs.com/

Deployment Lets imagine we want our code to constantly be running or used as a service, we cannot have the script always running on our machine, which might be off, offline or just not available to use to run the script/s. To combat this we will have to upload it to a server, for it to be ran, to do this, we will use a service called Heroku.

Heroku Setup Firstly, we will want to set up an account here - https://www.heroku.com/ Then we will want to install the toolbelt from the following link - https://devcenter.heroku.com/articles/heroku-cli#download-and-install Once that is installed, we will set it up to connect to our account, we will run the following command in a command prompt and login (with the credentials we have just created): heroku login Finally, we will want to install GitHub (as well as login or create an account), to push the project files - https://desktop.github.com/

Heroku Deployment (GitHub) From here we can go to the following link and create a new app - https://dashboard.heroku.com/apps Then, before we upload it, we want to create a new file in our project called “Procfile” (no extension, not “Procfile.js”), with the single line of “worker: node script.js”, where the “script.js” is the file you want to execute. Once we have done all that, we can finally upload it, to do this, we will enter the commands showed in the deploy section of the app, from where our project file is (containing the scripts, procfile, keys, node_modules folder and so on).

Thank You! - Any Questions? Sean O'Mahoney Email: sean.omahoney@stu.mmu.ac.uk LinkedIn: https://uk.linkedin.com/in/sean12697 ePortfolio: https://seanomahoney.me/ Computing Society Facebook: “MMUComputingSociety” Twitter: @MMUCompSoc Union: Computing Society Extra Resource's Heroku Toolbelt: https://devcenter.heroku.com/articles/heroku-cli#download-and-install GitHub: https://desktop.github.com/