Skip to content

Commit

Permalink
maybe found the kink in the rtm.connect
Browse files Browse the repository at this point in the history
  • Loading branch information
bsoule committed Sep 9, 2022
1 parent c3ce4ab commit 260309c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 29 deletions.
43 changes: 19 additions & 24 deletions lib/beebot.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,28 @@ var stopBot = function(teamId) {
}

var startBot = function(teamId, rurl=null) {
try {
redis.hgetall("beebot.teamid." + teamId, function(err, obj) {
var rediskey = `beebot.teamid.${teamId}`
redis.hgetall(rediskey, function(err, obj) {
//console.log("DEBUG: obj and err are both null!")
//console.log(`DEBUG: teamId/obj/err = ${teamId} / ${obj} / ${err}`)
console.log("DEBUG: teamId/obj/err", teamId, obj, err)
var RtmClient = require('@slack/client').RtmClient
var MemoryDataStore = require('@slack/client').MemoryDataStore
const RtmClient = require('@slack/client').RtmClient
const CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS
const 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, {
const rtm = new RtmClient(obj.bot_access_token, {
useRtmConnect: true,
dataStore: new MemoryDataStore(),
autoReconnect: true,
})
console.log("DEBUGSTART2")
// RtmClient constructor appears to be broken, not storing the token that
// is passed in the constructor, but expecting it to be around
rtm.token = obj.bot_access_token

rtm.on('message', function(message) {
if (!message.text) { return }
Expand All @@ -95,30 +99,21 @@ var startBot = function(teamId, rurl=null) {
console.log("mysterycrashererror")
})

var CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS
console.log("DEBUGSTART3")

rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, async(rtmStartData) => {
try {
rtm.userId = rtmStartData.self.id
console.log("AUTHENTICATED???")

} catch (error) {
console.log("AUTHENTICATED ERROR?", error, rtmStartData)
}
rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, function(rtmStartData) {
console.log("AUTHENTICATED!", Object.keys(rtmStartData))
rtm.userId = rtmStartData.self.id
})
rtm.on(CLIENT_EVENTS.RTM.RTM_CONNECTION_OPENED, () => {
console.log("CONNEXTION READY?")
console.log("RTM is CONNECTED?", rtm.connected)
})
console.log("DEBUGSTART4")

stopBot(teamId)
console.log("DEBUGSTART5 stop")
rtm.connect()
console.log("DEBUGSTART5 connect")
rtm.start()
console.log("RTM is connected?", rtm.connected)
rtm.teamId = teamId
bots.push(rtm)
})
} catch (error) {
console.log("DEBUG ERROR CATCH:", error)
}
}

var handleCreateBot = function(req, resp) {
Expand Down
1 change: 0 additions & 1 deletion lib/tock.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ if (process.env.REDIS_URL) {
}
})
//redis.auth(rtg.auth.split(":")[1])
console.log("REDIS case 1")
} else {
var redis = require("redis").createClient()
}
Expand Down
36 changes: 32 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,38 @@ app.get('/', (req, resp) => {
"dance with Slack (POST to /bot) it will get back to you as " +
"soon as possible.")
})
app.get('/tryconnect', (req, resp) => {
var bat = req.query.bot_access_token
const Client = require('@slack/client').RtmClient
const CLIENT_EVENTS = require('@slack/client').CLIENT_EVENTS
const MemoryDataStore = require('@slack/client').MemoryDataStore
console.log("TOKENNNNNNNNN", bat)
const rtm = new Client(bat, {
useRtmConnect: true,
dataStore: new MemoryDataStore(),
})
console.log("RTM token?", rtm.token)
rtm.token = bat
console.log("RTM token?", rtm.token)

rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, function(rtmStartData) {
console.log("AUTHENTICATED", Object.keys(rtmStartData))
})
rtm.on(CLIENT_EVENTS.RTM.RTM_CONNECTION_OPENED, () => {
console.log("CONNECTION READY")
console.log("RTM is connected?", rtm.connected)
})

rtm.start()
//.catch(console.error);
console.log("RTM is connected?", rtm.connected)
resp.send("waiting on tryconnect")
})
app.get('/tryStartBot', (req, resp) => {
var bat = req.query.teamid
beebot.startBot(bat)
resp.send("waiting on startbot")
})

app.listen(app.get('port'), () => {
console.log('Beebot app is listening on port', app.get('port'))
Expand All @@ -85,11 +117,7 @@ app.listen(app.get('port'), () => {
for (var i = 0; i < obj.length; i++) {
var teamId = obj[i].split(".").pop()
console.log(`Doing beebot.startBot("${teamId}")`)
try {
beebot.startBot(teamId)
} catch (error) {
console.log("DEBUG LISTEN CATCH", error)
}
}
// I think that whole for loop can be replaced with this:
//obj.forEach(x => beebot.startBot(x.split(".").pop()))
Expand Down

0 comments on commit 260309c

Please sign in to comment.