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

how to send mesage without onMessage ? #446

Closed
eggcaker opened this Issue Apr 28, 2017 · 5 comments

Comments

Projects
None yet
3 participants
@eggcaker
Copy link

eggcaker commented Apr 28, 2017

how to send message directly to a room ? like with webhook call or some things shell command? what's the best way?

@huan huan added the question label Apr 28, 2017

@huan

This comment has been minimized.

Copy link
Member

huan commented Apr 30, 2017

It's easy.

const r = await Room.find('room name')
r.say('hello room')

See room-bot.ts - Manage Chat Room at https://github.com/Chatie/wechaty/wiki/Example

@eggcaker

This comment has been minimized.

Copy link
Author

eggcaker commented May 2, 2017

so, the only way to send message to room is start timer looper inside onlogin ? is possible to make wechaty handle webhook, for example after ci build successful with webhook call wechaty cansend message to room depend request params?

@huan

This comment has been minimized.

Copy link
Member

huan commented May 2, 2017

You can send a message only after login, but there's others way to do this instead of start timer inside onlogin.

like this:

let isLogined = false
let globalRoom = null

Wechaty.instance()
.on('login', async () => {
  isLogined = true
  initRoom()
})
.init()

function async initRoom() {
  globalRoom = await Room.find('room name')
  if (!globalRoom) {
    console.warn('room not found. retry after 3 seconds')
    setTimeout(initRoom, 3000)
  }
}

function sendToRoom(msg) {
  if (!isLogined) {
    console.error('need login first')
    return
  }

  if (!globalRoom) {
    console.log('room not found(yet), please send msg after Wechaty fully loaded.')
    return
  }

  globalRoom.say(msg)
}

app.get('/room', (req, res, done) => {
  globalRoom.say('received a get request!')
  done()
})

Just write from scratch, hope it could help you.

@lijiarui

This comment has been minimized.

Copy link
Member

lijiarui commented May 2, 2017

@eggcaker

This comment has been minimized.

Copy link
Author

eggcaker commented May 4, 2017

cool! it's works. Thanks!

@eggcaker eggcaker closed this May 4, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment