keybase / keybase-bot

exploration with the keybase chat API
264 stars 65 forks source link

listTeamMemberships returns context cancelled #145

Open milklineep opened 5 years ago

milklineep commented 5 years ago

I am trying to write a simple keybase bot. My intention was to auto-subscribe people to a rules channel as soon as they join. But I can't get the list of members. Here is my code:

const Bot = require('keybase-bot')
const bot = new Bot()
const username = 'username-redacted'
const paperkey = 'paperkey-redacted'
bot
  .init(username, paperkey, {verbose: false})
  .then(() => {
    console.log(`Your bot is initialized. It is logged in as ${bot.myInfo().username}`)
    bot.team
        .listTeamMemberships({team: 'team-name-redacted'})
        .then(res => console.log(res))
        .catch(error => {console.error(error)})
    bot.deinit()
  })
  .catch(error => {
    console.error(error)
    bot.deinit()
})

I get the following error when I run it:

Your bot is initialized. It is logged in as username-redacted
Error: API network error: doRetry failed, attempts: 3, timeout 5.324s, last err: context canceled (error 1601)
    at Team._runApiCommand (/home/epsilon/keybase-bot/node_modules/keybase-bot/index.js:565:13)
    at processTicksAndRejections (internal/process/task_queues.js:89:5)
    at async Team.listTeamMemberships (/home/epsilon/keybase-bot/node_modules/keybase-bot/index.js:1283:17)

I have copy-pasted the code from the example so I don't think the error comes from there. Keybase connects to the internet just fine. I don't know where the error comes from.

pzduniak commented 5 years ago

Reproduced, thanks for your bug report!

pzduniak commented 5 years ago

Looks like a bug in keybase/client, list-team-memberships fails straight after the service's startup. As a workaround either use .initFromRunningService(), wait a second or two before making the request or implement a retry mechanism:

const Bot = require('../../index.js')
const bot = new Bot()
const username = process.env.KB_USERNAME
const paperkey = process.env.KB_PAPERKEY

function timeout(time) {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve()
    }, time)
  })
}

bot
  .init(username, paperkey, {verbose: false})
  .then(async () => {
    console.log(`Your bot is initialized. It is logged in as ${bot.myInfo().username}`)

    while (true) {
      try {
        const res = await bot.team.listTeamMemberships({team: process.env.KB_TEAM})
        console.log(res)
        break
      } catch (err) {
        console.log(err)
        await timeout(500)
      }
    }
  })
  .catch(error => {
    console.error(error)
    bot.deinit()
  })
taruti commented 4 years ago

This seems to be working fine with the current master.