Skip to content

Commit

Permalink
this seems to fix the dm & channel zenos? maybe?
Browse files Browse the repository at this point in the history
for #29
  • Loading branch information
bsoule committed Sep 9, 2022
1 parent df85151 commit 71e0ac5
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions lib/beebot.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ var bots = []

// handle a message and pass it through to the Beeminder server
var handleMessage = function(rtm, message) {
//console.log(`DEBUG4: handleMessage ${message.text}`)
//console.log("DEBUG4: 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
// idea if this is safe, but it does work in the instance of
// the DM zenoing
if (message.subtype === "bot_message") return
var text = message.text
var regexpString = "<@" + rtm.activeUserId + ">"
if (message.text.match(new RegExp(regexpString))) {
Expand Down Expand Up @@ -100,17 +106,16 @@ var startBot = function(teamId, rurl=null) {
})

rtm.on(CLIENT_EVENTS.RTM.AUTHENTICATED, function(rtmStartData) {
console.log("AUTHENTICATED!", Object.keys(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("CONNEXTION READY?")
console.log("RTM is CONNECTED?", rtm.connected)
})

stopBot(teamId)
rtm.start()
console.log("RTM is connected?", rtm.connected)
rtm.teamId = teamId
bots.push(rtm)
})
Expand All @@ -135,15 +140,13 @@ var handleZeno = (req, res) => {
var webClient = new WebClient(rtm._token)

if (req.body.channel) {
webClient.channels.list({}, (error, response) => {
webClient.conversations.list({}, (error, response) => {
if (!response.ok) { 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 }
rtm.send({ id : 1,
type : "message",
channel : channel.id,
text : req.body.message })
webClient.chat.postMessage(channel.id, req.body.message)
//rtm.sendMessage(req.body.message, channel.id)
res.send("ok")
return
}
Expand All @@ -153,6 +156,7 @@ var handleZeno = (req, res) => {
}

// 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)
if (user) {
var dm = rtm.dataStore.getDMByName(user.name)
Expand All @@ -164,8 +168,11 @@ var handleZeno = (req, res) => {
}

// open the dm channel if we didn't find a user
webClient.dm.open(req.body.user_id, (error, response) => {
rtm.sendMessage(req.body.message, response.channel.id)
//webClient.conversations.open(_token, (error, response) => {
webClient.conversations.open({token: rtm._token, users: req.body.user_id},
(error, response) => {
webClient.chat.postMessage(response.channel.id, req.body.message)
//rtm.sendMessage(req.body.message, response.channel.id)
return
})

Expand Down

0 comments on commit 71e0ac5

Please sign in to comment.