Skip to content

Commit

Permalink
debug forever: fix thing with multiple response
Browse files Browse the repository at this point in the history
headers complaint error
more debug: headers error
DEBUG1: starting over where is it triggered?
is this the right way to handle responses
  • Loading branch information
bsoule committed Nov 9, 2023
1 parent 8afc2d8 commit 86955ef
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 53 deletions.
75 changes: 23 additions & 52 deletions lib/beebot.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var bots = []

// handle a message and pass it through to the Beeminder server
var handleMessage = function(rtm, message) {
console.log("handleMessage", message)
console.log("handleMessage: ", message)
// I hope this is safe: the bot was responding to its own
// messages when i use chat.message.. which is no good,
// so skip if it has a subtype of "bot_message"? again, no
Expand Down Expand Up @@ -151,16 +151,13 @@ var startBot = function(teamId, rurl=null) {
console.error(error.message)
return
}

})
}

var handleCreateBot = function(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 },
function(err, obj) {
//console.log("DEBUG2")
startBot(req.body.team_id)
resp.send("OK")
}
Expand All @@ -182,62 +179,36 @@ var handleZeno = (req, res) => {
var webClient = new WebClient(rtm._token)

if (req.body.channel) {
try {
webClient.conversations.list({}, (error, response) => {
if (!response.ok) {
console.error("error in conversations.list: " + response)
console.error("error: ", error)
console.error("response.headers", response.headers())
res.send("error!"); return
} //TODO: alert
for (var i = 0; i < response.channels.length; i++) {
var channel = response.channels[i]
if (channel.name !== req.body.channel.replace('#', '')) { continue }
webClient.chat.postMessage(channel.id, req.body.message)
res.send("ok"); return
}
res.status(404).send("could not find a channel with the name " + req.body.channel)
})
} catch (error) {
console.error("error in conversations.list: " + error.message)
res.status(500).send("error in handleZeno: " + error.message)
return
}
return
}

// else default to a DM
// TODO: when would we have a user here? I don't understand this
var user = rtm.dataStore.getUserById(req.body.user_id)
try {
if (user) {
var dm = rtm.dataStore.getDMByName(user.name)
if (dm) {
rtm.sendMessage(req.body.message, dm.id)
res.send("ok")
return
webClient.conversations.list({}, (error, response) => {
const target = req.body.channel
if (!response.ok) {
console.error("error in conversations.list: " + response)
console.error("DEBUG1: error: ", error)
return
} //TODO: alert
for (var i = 0; i < response.channels.length; i++) {
const toChl = response.channels[i]
if (channel.name !== channel.replace('#', '')) { continue }
webClient.chat.postMessage(channel.id, req.body.message)
}
}
} catch (error) {
console.error("error in datastore user: " + error.message)
res.status(500).send("error in handleZeno: " + error.message)
console.log(`could not find a channel with the name ${req.body.channel}`)
})
res.send("ok");
return
}
} else {

// open the dm channel if we didn't find a user
try {
// open the dm channel if we didn't find a user
webClient.conversations.open({token: rtm._token, users: req.body.user_id},
(error, response) => {
//console.log("webClient conversations.open callback: ", error, response)
if (error !== undefined) {
console.log("DM ERROR:", error)
}
if (!response.ok) {
console.log("DM RESP NOT OK:", response)
}
webClient.chat.postMessage(response.channel.id, req.body.message)
//rtm.sendMessage(req.body.message, response.channel.id)
res.send("ok"); return
}
)
} catch (error) {
console.error("error in webClient conversations.open: " + error.message)
res.status(500).send("error in handleZeno: " + error.message)
return
}

res.send("ok")
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"babel": {},
"scripts": {
"start": "nodemon server.js"
"start": "node server.js"
},
"author": "Beeminder",
"license": "MIT",
Expand Down

0 comments on commit 86955ef

Please sign in to comment.