Skip to content

Commit

Permalink
Merge pull request #19 from beeminder/glitch
Browse files Browse the repository at this point in the history
Cleanup, refactoring, better errors
  • Loading branch information
dreeves authored Dec 28, 2017
2 parents efba9b0 + c7cf82d commit 6dc03ec
Show file tree
Hide file tree
Showing 10 changed files with 182 additions and 154 deletions.
4 changes: 4 additions & 0 deletions .config/configstore/update-notifier-npm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"optOut": false,
"lastUpdateCheck": 1514319292993
}
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,18 @@ Ok, this could be a whole startup's worth of work, but someone should totally do
### Contributing

Make a branch off of master and send a pull request!


### Debugging Notes

In case we need to revert to this version of the Slack client for Node.js:
```
"@slack/client": "^3.13.0",
```

To create a bot for a team on Slack, we do a POST to /bot here with `team_id` and `bot_access_token`.
We never do that for the test version of the bot hosted on Glitch.
But it still works. Why?
Maybe that only applies to slash commands that talk to the Beeminder server.

What's the difference between redis.set and redis.hset
22 changes: 17 additions & 5 deletions lib/beebot.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// --------------------------------- 80chars ---------------------------------->
var botils = require('./botils.js')

if (process.env.REDISTOGO_URL) {
var rtg = require("url").parse(process.env.REDISTOGO_URL)
var redis = require("redis").createClient(rtg.port, rtg.hostname)
Expand All @@ -9,10 +11,12 @@ if (process.env.REDISTOGO_URL) {

var https = require('https')
var karmabot = require('./karma.js')
console.log(`DEBUG3: karmabot = ${JSON.stringify(karmabot)}`)
var bots = []

// handle a message and pass it through to the Beeminder server
var handleMessage = (rtm, message) => {
console.log(`DEBUG4: handleMessage ${message.text}`)
var text = message.text
var regexpString = "<@" + rtm.activeUserId + ">"
if (message.text.match(new RegExp(regexpString))) {
Expand All @@ -33,7 +37,7 @@ var handleMessage = (rtm, message) => {
rtm.send({ id: 1,
type: "message",
channel: message.channel,
text: resText
text: resText,
})
})
}).on('error', (e) => { console.error(e) })
Expand All @@ -45,10 +49,18 @@ var stopBot = (teamId) => {
})
}

var startBot = (teamId) => {
var startBot = (teamId, rurl=null) => {
redis.hgetall("beebot.teamid." + teamId, (err, obj) => {
console.log("DEBUG: obj and err are both null!")
console.log(`DEBUG: teamId/obj/err = ${teamId} / ${obj} / ${err}`)
var RtmClient = require('@slack/client').RtmClient
var MemoryDataStore = require('@slack/client').MemoryDataStore
if (obj === null && rurl !== null) {
botils.blurt(rurl, "Sad panda. Can't create a bot to listen here. " +
`redis.hgetall("beebot.teamid.${teamId}") returned null ` +
"so we can't look up the bot access token for the Slack RTM client.")
return
}
var rtm = new RtmClient(obj.bot_access_token, {
dataStore: new MemoryDataStore(),
autoReconnect: true,
Expand All @@ -60,9 +72,7 @@ var startBot = (teamId) => {
if (message.text.match(new RegExp(regexpString))) {
handleMessage(rtm, message)
}
if (message.channel.match(/^D/) &&
message.team &&
!message.command) {
if (message.channel.match(/^D/) && message.team && !message.command) {
handleMessage(rtm, message)
}
if (message.text.match(new RegExp(/\+\+|--/))) {
Expand All @@ -86,9 +96,11 @@ var startBot = (teamId) => {
}

var handleCreateBot = (req, resp) => {
console.log(`DEBUG5: req = ${JSON.stringify(req)}`)
redis.hmset("beebot.teamid." + req.body.team_id,
{ bot_access_token: req.body.bot_access_token },
(err, obj) => {
console.log("DEBUG2")
beebot.startBot(req.body.team_id) // beebot not defined here?
resp.send("OK")
}
Expand Down
25 changes: 12 additions & 13 deletions lib/bid.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const rkey = keyfn("auctions")


if (process.env.REDISTOGO_URL) {
var rtg = require("url").parse(process.env.REDISTOGO_URL);
var redis = require("redis").createClient(rtg.port, rtg.hostname);
redis.auth(rtg.auth.split(":")[1]);
var rtg = require("url").parse(process.env.REDISTOGO_URL)
var redis = require("redis").createClient(rtg.port, rtg.hostname)
redis.auth(rtg.auth.split(":")[1])
} else {
var redis = require("redis").createClient();
var redis = require("redis").createClient()
}

// Returns a hash of usernames (without the @'s) who are @-mentioned in txt
Expand All @@ -39,11 +39,10 @@ var bidSummary = function(bids) {
// "Got bids from {...}, waiting on {...}"
var bidStatus = function(bids) {
return "Got bids from {"
+ Object.keys(bids).filter(function(x) { return bids[x] }).join(", ")
+ "}, waiting on {"
+ Object.keys(bids).filter(function(x) { return !bids[x] }).join(", ")
+ "}"
+ Object.keys(bids).filter(x => bids[x]).join(", ") + "}, waiting on {"
+ Object.keys(bids).filter(x => !bids[x]).join(", ") + "}"
}
// --------------------------------- 80chars ---------------------------------->

// Returns whether any of the bids are missing
var bidMissing = function(bids) {
Expand Down Expand Up @@ -91,7 +90,7 @@ var bidReset = function(chan) {
var bidPay = function() {
var y, n, r = randint(10) // randint(10)==1 is the same as bern(.1)
y = "/roll 10 → 1 ∴ PAY 10X! :money_with_wings: :moneybag: :money_mouth_face:"
n = "/roll 10 → " + r + " not 1 ∴ no payments! :sweat_smile:"
n = "/roll 10 → " + r + " ∴ no payments! :sweat_smile:"
return (r === 1 ? y : n)
}

Expand All @@ -102,7 +101,7 @@ var bidProc = function(res, chan, user, text, rurl) {
redis.hgetall(rkey(chan, "bids"),
function(err, obj) { // obj is now the hash from users to bids
whisp(res, "Got your bid: " + text)
if(bidMissing(obj)) {
if (bidMissing(obj)) {
blurt(rurl, "New bid from " + user + "! " + bidStatus(obj))
} else {
bidReset(chan)
Expand All @@ -126,9 +125,9 @@ var help = function(res) {
}

var handleSlash = function(req, res) {
if(req.body.token != process.env.SLACK_TOKEN) {
whisp(res, "This request didn't come from Slack!");
return;
if(req.body.token !== process.env.SLACK_TOKEN) {
whisp(res, "This request didn't come from Slack!")
return
}
var rurl = req.body.response_url // for delayed responses to slash commands
var chan = req.body.channel_id
Expand Down
13 changes: 10 additions & 3 deletions lib/botils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,16 @@ var holla = (res, txt) => {
var blurt = (rurl, txt) => {
request.post(rurl, { json: {
"response_type": "in_channel", // in_channel vs ephemeral
"text": txt}
}, (error, response, body) => {
console.log("error handling? pshaw.")
"text": txt }
}, (err, resp, body) => {
if (err) {
console.log(`BLURT ERROR sending ${txt} to ${rurl}!\n` +
`${JSON.stringify(err)}\n` +
`${JSON.stringify(resp)}\n` +
`${JSON.stringify(body)}\n`)
} else {
console.log(`Successful blurt(${rurl}, ${txt.substring(0,10)}...)`)
}
})
}

Expand Down
10 changes: 5 additions & 5 deletions lib/charge.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ var checkCharges = function() {
runCharge(charge, null);
var message = "Charged " + charge.user +" $" + charge.amount +" for "+
charge.reason +" :bee:";
var rtm = beebot.rtmForTeam(charge.team_id);
if (!rtm) { res.send("500"); return; }
var rtm = beebot.rtmForTeam(charge.team_id)
if (!rtm) { res.send("500"); return }

var WebClient = require('@slack/client').WebClient;
var webClient = new WebClient(rtm._token);
Expand Down Expand Up @@ -74,7 +74,7 @@ var scheduleCharge = function(res, charge) {
if (charge.reason) { message += " (" + charge.reason + ")"; }
botils.holla(res, message);
}
);
)
}

var runCharge = function(charge, res) {
Expand Down Expand Up @@ -184,11 +184,11 @@ var handleSlash = function(req, res) {
runCharge(charge, res);
}
} else {
help(res);
help(res)
}
}

module.exports = {
handleSlash: handleSlash,
checkCharges: checkCharges
checkCharges: checkCharges,
}
Loading

0 comments on commit 6dc03ec

Please sign in to comment.