Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

memberAll() not running in docker after v0.21.27 #1573

Closed
kis87988 opened this issue Aug 29, 2018 · 7 comments
Closed

memberAll() not running in docker after v0.21.27 #1573

kis87988 opened this issue Aug 29, 2018 · 7 comments
Labels

Comments

@kis87988
Copy link
Contributor

kis87988 commented Aug 29, 2018

0. Report Issue Guide

  1. Please search in the issue first, and make sure your problem had not been reported before.
  2. If your issue is related to wechaty-puppet-padchat, please report the issue at https://github.com/lijiarui/wechaty-puppet-padchat/issues/new
  3. If your issue is related to wechaty-puppet-puppeteer, please report the issue at https://github.com/Chatie/wechaty-puppet-puppeteer/issues/new
  4. If your issue is DIRECT related to Wechaty, then please follow the below template and make sure you fill it as required in detail.

1. Expected behavior

Question: What behavior do you expect?

Answer: run the bot

2. Actual behavior

Question: What actual behavior did you experience?

Answer: not pass ts-node --type-check

3. Steps to reproduce your problem (and fixes, if any)

This part is very important: if you can not provide any reproduce steps, then the problem will be very hard to be recognized.

Question: What're the reproducible steps for us to see your problem?
Please check code below with replace room id and token id run in the padchat with docker version after v0.21.27

import
{
  Room,
  Contact,
  Wechaty,
  Message,
  Friendship,
  log,
  qrcodeValueToImageUrl,
} from 'wechaty'
import { PuppetPadchat } from 'wechaty-puppet-padchat';
import { generate } from 'qrcode-terminal'
import { finis } from 'finis'


export const PUPPET = new PuppetPadchat({
  endpoint: 'ws://padchat.botorange.com/wx',
  token: '',    
})
const roomID_NAME = `123@chatroom`

const BOT_NAME: string = "Ponyo_1"
const BOT = new Wechaty({
  name: BOT_NAME,
  puppet: PUPPET,
})
const AVOID_ID_LIST = ["filehelper", "floatbottle", "fmessage",
  "medianote", "weixin"]

let alive_interval_no: NodeJS.Timer
let manager: Contact | undefined = undefined
let quiting = false


/* stop signal listener */
let killChrome: NodeJS.SignalsListener
finis(async (code, signal) =>
{
  log.info('Bot', 'finis(%s, %s)', code, signal)

  if (!BOT.logonoff())
  {
    log.info('Bot', 'finis() bot had been already stopped')
    await doExit(code)
  }

  if (quiting)
  {
    log.warn('Bot', 'finis() already quiting... return and wait...')
    return
  }

  quiting = true
  let done = false
  // let checkNum = 0

  const exitMsg = `Wechaty will exit ${code} because of ${signal} `

  log.info('Bot', 'finis() broadcast quiting message for bot')
  await BOT.say(exitMsg)
    // .then(() => bot.stop())
    .catch(e => log.error('Bot', 'finis() catch rejection: %s', e))
    .then(() => done = true)

  setImmediate(checkForExit)

  function checkForExit()
  {
    // if (checkNum++ % 100 === 0) {
    log.info('Bot', 'finis() checkForExit() checking done: %s', done)
    // }
    if (done)
    {
      log.info('Bot', 'finis() checkForExit() done!')
      setTimeout(() => doExit(code), 1000)  // delay 1 second
      return
    }
    setTimeout(checkForExit, 100)
  }
})
async function doExit(code: number): Promise<void>
{
  log.info('Bot', 'doExit(%d)', code)
  if (killChrome)
  {
    killChrome('SIGINT')
  }
  process.exit(code)
}

/* bot event listener */
BOT
  .on('ready', async () =>
  {
    try
    {
      console.log(`bot1 is ready`)
    }
    catch (e)
    {
      log.error('Bot', 'ready event exception: %s', e.stack)
    }
  })

  .on('logout', async user =>
  {
    log.info('Bot', `${user.name()} logouted`)
    await sleep(10000)
    BOT.reset()
  })

  .on('login', async user =>
  {
    try
    {
      manager = BOT.Contact.load("a38372624")

      log.info('Bot', `${user.name()} login`)

      if(manager)
        await manager.say(`Bot login ${get_now()}`)

      if (alive_interval_no)
        clearInterval(alive_interval_no)

      alive_interval_no = setInterval(async () => 
      {
        try 
        {
          if (!BOT.logonoff())
            await BOT.reset()
        }
        catch (error) 
        {
          log.error(`Bot`, `Bot is dead in the syncing interval`)
          if (!BOT.logonoff())
            await doExit(-2)
        }
      }, 5400000);
    }
    catch (e)
    {
      log.error('Bot', 'login event exception: %s', e.stack)
    }
  })

  .on('scan', async (qrcode, status, data) =>
  {
    generate(qrcode, { small: true })
    if (data)
    {
      console.log(data)
    }

    if (!status)
    {
      const url: string = qrcodeValueToImageUrl(qrcode)
      console.log(qrcode)
      console.log(url)
      console.log('^^^ Online QR Code Image URL ^^^ ')
      console.log(`[${status}] ${qrcode} Scan QR Code above url to log in: `)
    }
    else if (status == 201)
    {
      console.log("201 scaned, wait for confirm")
    }
    else if (status == 200)
    {
      console.log("200 login confirmed")
    }
    else if (status == 408)
    {
      console.log(`status: ${status} wait for scan`)
    }
    else
    {
      console.log(`status: ${status}`)
    }
  })

  /**
 * Global Event: room-join
 */
  .on('room-join', async function (ROOM, INVITEE_LIST, INVITER)
  {
    try
    {
      const ROOM_TOPIC = await ROOM.topic()
      const INVITEE_NAME_LIST = INVITEE_LIST.map(c => c.name()).join(',')
      const INVITER_NAME = INVITER.name()

      log.info('Bot', 'EVENT: room-join - Room "%s" got new member "%s", invited by "%s"',
        ROOM_TOPIC,
        INVITEE_NAME_LIST,
        INVITER_NAME,
      )
    }
    catch (e)
    {
      log.error('Bot', 'room-join event exception: %s', e.stack)
    }
  })

  .on('friendship', async friendship =>
  {
    let logMsg
    try
    {
      switch (friendship.type())
      {
        // 1. New Friend Request
        case Friendship.Type.Receive:
          logMsg = `received \`friendship\` event from ${friendship.contact().name()} with ${friendship.hello()}`
          await sleep(getRandomIntInclusive(10000, 30000))
          await friendship.accept()
          break

        // 2. Friend Ship Confirmed
        case Friendship.Type.Confirm:
          logMsg = 'friend ship confirmed with ' + friendship.contact().name()
          await sleep(getRandomIntInclusive(5000, 15000))
          await friendship.contact().say(`Hi, I'm Ponyo 1`)
          await sleep(10000)
          await friendship.contact().sync()
          break
      }
      log.verbose('Bot', logMsg)
    }
    catch (e)
    {
      log.error('Bot', 'friendship event exception: %s', e.stack)
    }

  })

  .on('message', async (msg:Message) =>
  {
    try
    {
      // await BOT.ready()
      const CONTACT: Contact | null = await msg.from()
      const ORIGINAL_CONTENT: string = msg.text()
      const ROOM: Room | null = msg.room()
      if (msg.self() ||
        !CONTACT ||
        AVOID_ID_LIST.indexOf(CONTACT.id) != -1 ||
        CONTACT.type() == BOT.Contact.Type.Official||
        ROOM)
        return
      if (ORIGINAL_CONTENT == 'putInRoom')
        put_in_room(CONTACT,roomID_NAME)
    }
    catch (e)
    {
      log.error('Bot', 'Message event exception: %s', e.stack)
    }
  })

  .on('error', async e =>
  {
    try
    {
      log.error('Bot', 'error: %s', e)
    }
    catch (e)
    {
      log.error('Bot', 'error event exception: %s', e.stack)
    }
  })

  .on('room-leave', async (ROOM: Room, LEAVER_LIST: Contact[]) =>
  {
    try
    {
      const LEAVER_NAME_LIST = LEAVER_LIST.map(c => c.name()).join(',')
      console.log(`Room ${ROOM.topic()} lost member ${LEAVER_NAME_LIST}`)
    }
    catch (e)
    {
      log.error('Bot', 'room-leave event exception: %s', e.stack)
    }
  })

BOT.start()
  .then(async () =>
  {
    const LISTENER_LIST = process.listeners('SIGINT')
    for (const LISTENER of LISTENER_LIST)
    {
      if (LISTENER.name === 'killChrome')
      {
        process.removeListener('SIGINT', LISTENER)
        killChrome = LISTENER
      }
    }
  })
  .catch(async e =>
  {
    log.error('Bot', 'start() fail: %s', e)
    await BOT.stop()
    process.exit(-1)
  })
  

/* all functions*/
async function put_in_room(contact: Contact, id_or_name: string)
{
  try
  {
    const CONTACT_ALIAS = await contact.alias()
    const IS_BLOCK_CONTACT = (CONTACT_ALIAS && /^BLK-/i.test(CONTACT_ALIAS)) ?
                             true :
                             false
    if (IS_BLOCK_CONTACT)
      return `Sorry, there is some issue that I can not let you in ${id_or_name}`

    let keyroom: Room | null
    if (/.*@chatroom/i.test(id_or_name))
      keyroom = BOT.Room.load(id_or_name)
    else
      keyroom = await BOT.Room.find(id_or_name)
    const CONTACT_NAME = contact.name()
    await sleep(getRandomIntInclusive(1000, 10000))

    if (keyroom)
    {
      await keyroom.ready()
      const ROOM_NAME = await keyroom.topic()
      const ROOM_SIZE = await keyroom.memberAll().then((e: any[]) =>
      {
        return e.length
      }).catch(() =>
      {
        return 0
      })
      log.info('Bot', 'putInRoom(%s, %s)', CONTACT_NAME, ROOM_NAME)
      if (ROOM_SIZE >= 500)
        return `${ROOM_NAME} is super full`
      const HAS = await keyroom.has(contact)
      if (HAS)
        return `You have in the ${ROOM_NAME}, thanks`

      await keyroom.add(contact)
        .catch(e =>
        {
          log.error('Bot', 'room.add() exception: %s', e.stack)
        })
      const ANNOUNCE = await keyroom.announce().catch(e =>
      {
        log.error('Bot', 'room.announce() exception: %s', e.stack)
        return null
      })
      if (ANNOUNCE)
        return `please Follow ${ROOM_NAME} rules:\n${ANNOUNCE}`
      return `Please follow ${ROOM_NAME} announce rules`
    }
    else
      await contact.say(`Ponyo error,can not find ${id_or_name} room`)
  }
  catch (e)
  {
    log.error('Bot', 'putInRoom() exception: %s', e.stack)
  }
  return ""
}

function sleep(ms: number)
{
  return new Promise<void>(function (resolve)
  {
    setTimeout(resolve, ms);
  });
}

function getRandomIntInclusive(min: number, max: number)
{
  min = Math.ceil(min)
  max = Math.floor(max)
  return Math.floor(Math.random() * (max - min + 1)) + min
  //The maximum is inclusive and the minimum is inclusive 
}

function get_now()
{
  let clock: Date = new Date()
  const now: string = clock.toLocaleString('en-US', {
    hour12: false,
    year: "numeric", day: "2-digit", month: "2-digit",
    minute: "2-digit", second: "2-digit", hour: "2-digit"
  })
  return now
}

Answer:

4. Full Output Logs

Show Logs
$ docker create -t -i --name=wechaty_ponyo \
    --mount type=bind,source="$(pwd)",target=/bot \
    -e WECHATY_LOG="silly" \
    -e BROLOG_LEVEL="silly" \
    -e TZ="America/Los_Angeles" \
    zixia/wechaty:latest ponyo_1.ts;
    docker start -a wechaty_ponyo

Question: Paste your FULL(DO NOT ONLY PROVIDE FRAGMENTS) log messages
Answer:
Ponyo_1.ts(329,31): error TS2554: Expected 1 arguments, but got 0.

    at createTSError (/wechaty/node_modules/ts-node/src/index.ts:261:12)
    at getOutput (/wechaty/node_modules/ts-node/src/index.ts:367:40)
    at Object.compile (/wechaty/node_modules/ts-node/src/index.ts:558:11)
    at Module.m._compile (/wechaty/node_modules/ts-node/src/index.ts:439:43)
    at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/wechaty/node_modules/ts-node/src/index.ts:442:12)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
ERROR: Bot exited with code 1
  ____  _   _  ____   ____  _____ ____   ___  ____ _____
 | __ )| | | |/ ___| |  _ \| ____|  _ \ / _ \|  _ \_   _|
 |  _ \| | | | |  _  | |_) |  _| | |_) | | | | |_) || |
 | |_) | |_| | |_| | |  _ <| |___|  __/| |_| |  _ < | |
 |____/ \___/ \____| |_| \_\_____|_|    \___/|_| \_\|_|
@huan
Copy link
Member

huan commented Aug 29, 2018

It was confirmed a bug from wechaty-puppet-ioscat because of it depends on an old version Wechaty which caused we have a wrong Wechaty version in our docker image.

This bug should be fixed after Wechaty v0.22.2

@huan huan added the bug label Aug 29, 2018
@kis87988
Copy link
Contributor Author

nice. I will test it out!

@kis87988
Copy link
Contributor Author

Not sure it is relate to this or not.
This is another code but work in v0.21.25, but not working in v0.22.2. still reproduce more info.

14:17:04 SILL Config WECHATY_LOG set level to silly
14:17:04 INFO Config registering process.on("unhandledRejection") for development/debug
14:17:04 VERB Config constructor()
TypeError: Cannot read property 'kind' of undefined
    at isConstraintPosition (/wechaty/node_modules/typescript/lib/typescript.js:43553:27)
    at getConstraintForLocation (/wechaty/node_modules/typescript/lib/typescript.js:43566:25)
    at getInitialOrAssignedType (/wechaty/node_modules/typescript/lib/typescript.js:42612:20)
    at getTypeAtFlowAssignment (/wechaty/node_modules/typescript/lib/typescript.js:42972:71)
    at getTypeAtFlowNode (/wechaty/node_modules/typescript/lib/typescript.js:42899:32)
    at getFlowTypeOfReference (/wechaty/node_modules/typescript/lib/typescript.js:42852:51)
    at isPropertyInitializedInConstructor (/wechaty/node_modules/typescript/lib/typescript.js:53038:28)
    at checkPropertyInitialization (/wechaty/node_modules/typescript/lib/typescript.js:53021:50)
    at checkClassLikeDeclaration (/wechaty/node_modules/typescript/lib/typescript.js:52848:17)
    at checkClassDeclaration (/wechaty/node_modules/typescript/lib/typescript.js:52744:13)
    at checkSourceElement (/wechaty/node_modules/typescript/lib/typescript.js:53910:28)
    at Object.forEach (/wechaty/node_modules/typescript/lib/typescript.js:209:30)
    at checkSourceFileWorker (/wechaty/node_modules/typescript/lib/typescript.js:54068:20)
    at checkSourceFile (/wechaty/node_modules/typescript/lib/typescript.js:54039:13)
    at getDiagnosticsWorker (/wechaty/node_modules/typescript/lib/typescript.js:54115:17)
    at Object.getDiagnostics (/wechaty/node_modules/typescript/lib/typescript.js:54101:24)
    at /wechaty/node_modules/typescript/lib/typescript.js:83914:85
    at runWithCancellationToken (/wechaty/node_modules/typescript/lib/typescript.js:83880:24)
    at getSemanticDiagnosticsForFileNoCache (/wechaty/node_modules/typescript/lib/typescript.js:83903:20)
    at getAndCacheDiagnostics (/wechaty/node_modules/typescript/lib/typescript.js:84151:26)
    at getSemanticDiagnosticsForFile (/wechaty/node_modules/typescript/lib/typescript.js:83900:20)
    at getDiagnosticsHelper (/wechaty/node_modules/typescript/lib/typescript.js:83842:24)
    at Object.getSemanticDiagnostics (/wechaty/node_modules/typescript/lib/typescript.js:83855:20)
    at emitWorker (/wechaty/node_modules/typescript/lib/typescript.js:83803:216)
    at /wechaty/node_modules/typescript/lib/typescript.js:83788:66
    at runWithCancellationToken (/wechaty/node_modules/typescript/lib/typescript.js:83880:24)
    at Object.emit (/wechaty/node_modules/typescript/lib/typescript.js:83788:20)
    at Object.getFileEmitOutput (/wechaty/node_modules/typescript/lib/typescript.js:85160:34)
    at Object.getEmitOutput (/wechaty/node_modules/typescript/lib/typescript.js:113289:23)
    at getOutput (/wechaty/node_modules/ts-node/src/index.ts:358:30)
    at Object.compile (/wechaty/node_modules/ts-node/src/index.ts:558:11)
    at Module.m._compile (/wechaty/node_modules/ts-node/src/index.ts:439:43)
    at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/wechaty/node_modules/ts-node/src/index.ts:442:12)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/bot/MongoWechaty.ts:1:1)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Module.m._compile (/wechaty/node_modules/ts-node/src/index.ts:439:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/wechaty/node_modules/ts-node/src/index.ts:442:12)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/bot/Ponyo_1.ts:11:1)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Module.m._compile (/wechaty/node_modules/ts-node/src/index.ts:439:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/wechaty/node_modules/ts-node/src/index.ts:442:12)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at Object.<anonymous> (/wechaty/node_modules/ts-node/src/bin.ts:157:12)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at startup (internal/bootstrap/node.js:266:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:596:3)
ERROR: Bot exited with code 1

@huan
Copy link
Member

huan commented Aug 30, 2018

Could you please confirm that does v0.22.2 fix the code in your first post?

@kis87988
Copy link
Contributor Author

Yes, it fixed

@huan
Copy link
Member

huan commented Aug 30, 2018

Great.

So it seems you have another problem related to the version change.

Could you please close this issue, and file a new issue for the new problem?

@kis87988
Copy link
Contributor Author

Sure, I should do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants