Last active
May 27, 2019 09:24
-
-
Save Diederikjh/dd679f6e54e055f87f9b71670e583364 to your computer and use it in GitHub Desktop.
Glitch twitter bot for git hub commit comments (stil lots of getting started glitch code.)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"//1": "describes your app and its dependencies", | |
"//2": "https://docs.npmjs.com/files/package.json", | |
"//3": "updating this file will download and update your packages", | |
"name": "git-commit-bot", | |
"version": "0.0.1", | |
"description": "Tweet random git commits!", | |
"main": "server.js", | |
"scripts": { | |
"start": "node server.js" | |
}, | |
"dependencies": { | |
"express": "^4.14.1", | |
"github-api": "3.0.0", | |
"request": "^2.81.0", | |
"twit": "^2.2.9", | |
"random-word": "2.0.0" | |
}, | |
"engines": { | |
"node": "8.x" | |
}, | |
"repository": { | |
"url": "https://gist.github.com/Diederikjh/dd679f6e54e055f87f9b71670e583364" | |
}, | |
"license": "MIT", | |
"keywords": [ | |
"node", | |
"glitch", | |
"express" | |
] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// server.js | |
// where your node app starts | |
// init project | |
var express = require('express'); | |
var app = express(); | |
var doGithubStuff = async (response) => { | |
var GitHub = require('github-api'); | |
var Twit = require('twit'); | |
const gh = new GitHub({token:process.env.GIT_HUB_TOKEN}); | |
const randomWord = require('random-word'); | |
var retries = 0; | |
do { | |
var searchWord = randomWord(); | |
console.log(searchWord); | |
// Note this differs from docs quite a bit.. :( | |
var searchRequest = gh.search({q:searchWord}); | |
try { | |
var data = await searchRequest.forRepositories(); | |
var repos = data.data; | |
console.log(repos.length); | |
} | |
catch (err){ | |
console.log("error") | |
console.log(err); | |
} | |
retries++; | |
} while (retries < 10 && repos.length == 0) | |
if (repos.length == 0) { | |
response.sendStatus(500); | |
return; | |
} | |
var randoRepo = repos[Math.floor(Math.random()*repos.length)]; | |
var repoFullName = randoRepo.full_name; | |
var repoName = randoRepo.name; | |
var user = randoRepo.owner.login; | |
var repo = gh.getRepo(user, repoName); | |
repo.listCommits().then(function(commits) { | |
var commitsData = commits.data; | |
var commitMessage = commitsData[0].commit.message; | |
var htmlUrl = commitsData[0].html_url; | |
console.log(commitMessage); | |
console.log(htmlUrl); | |
return {commitMessage:commitMessage, url:htmlUrl}; | |
}).then(function(commitData){ | |
/* Be sure to update the .env file with your API keys. See how to get them: https://botwiki.org/tutorials/how-to-create-a-twitter-app */ | |
var twitterConfig = { | |
consumer_key: process.env.TWITTER_CONSUMER_KEY, | |
consumer_secret: process.env.TWITTER_CONSUMER_SECRET, | |
access_token: process.env.TWITTER_ACCESS_TOKEN, | |
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET | |
} | |
var t = new Twit(twitterConfig); | |
t.post('statuses/update', { status: commitData.commitMessage + " " + commitData.url }, function(err, data, twitterResponse) { | |
console.log(data) | |
console.log(err) | |
response.send({random_word:searchWord, commit_message:commitData.commitMessage, commit_url:commitData.url }); | |
}); | |
}).catch(function(err){ | |
console.log(err); | |
response.sendStatus(500); | |
}); | |
} | |
// we've started you off with Express, | |
// but feel free to use whatever libs or frameworks you'd like through `package.json`. | |
// http://expressjs.com/en/starter/static-files.html | |
app.use(express.static('public')); | |
// http://expressjs.com/en/starter/basic-routing.html | |
app.get("/", function (request, response) { | |
response.sendFile(__dirname + '/views/index.html'); | |
}); | |
app.get("/dreams", function (request, response) { | |
response.send(dreams); | |
}); | |
app.get("/" + process.env.POST_ENDPOINT, function (request, response) { | |
doGithubStuff(response) | |
}); | |
// could also use the POST body instead of query string: http://expressjs.com/en/api.html#req.body | |
app.post("/dreams", function (request, response) { | |
dreams.push(request.query.dream); | |
}); | |
// Simple in-memory store for now | |
var dreams = [ | |
"Find and count some sheep", | |
"Climb a really tall mountain", | |
"Wash the dishes" | |
]; | |
// listen for requests :) | |
var listener = app.listen(process.env.PORT, function () { | |
console.log('Your app is listening on port ' + listener.address().port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment